Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing series fold #271

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions __tests__/dataframe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,24 +338,30 @@ describe("dataframe", () => {
const actual = df.fold((a, b) => a.concat(b));
expect(actual).toSeriesEqual(expected);
});
// test("fold", () => {
// const s1 = pl.Series([1, 2, 3]);
// const s2 = pl.Series([4, 5, 6]);
// const s3 = pl.Series([7, 8, 1]);
// const expected = pl.Series("foo", [true, true, false]);
// const df = pl.DataFrame([s1, s2, s3]);
// const actual = df.fold((a, b) => a.lessThan(b)).alias("foo");
// expect(actual).toSeriesEqual(expected);
// });
// test("fold-again", () => {
// const s1 = pl.Series([1, 2, 3]);
// const s2 = pl.Series([4, 5, 6]);
// const s3 = pl.Series([7, 8, 1]);
// const expected = pl.Series("foo", [12, 15, 10]);
// const df = pl.DataFrame([s1, s2, s3]);
// const actual = df.fold((a, b) => a.plus(b)).alias("foo");
// expect(actual).toSeriesEqual(expected);
// });
it.each`
name | actual | expected
${"fold:lessThan"} | ${df.fold((a, b) => a.lessThan(b)).alias("foo")} | ${pl.Series("foo", [true, false, false])}
${"fold:lt"} | ${df.fold((a, b) => a.lt(b)).alias("foo")} | ${pl.Series("foo", [true, false, false])}
${"fold:lessThanEquals"} | ${df.fold((a, b) => a.lessThanEquals(b)).alias("foo")} | ${pl.Series("foo", [true, true, false])}
${"fold:ltEq"} | ${df.fold((a, b) => a.ltEq(b)).alias("foo")} | ${pl.Series("foo", [true, true, false])}
${"fold:neq"} | ${df.fold((a, b) => a.neq(b)).alias("foo")} | ${pl.Series("foo", [true, false, true])}
${"fold:plus"} | ${df.fold((a, b) => a.plus(b)).alias("foo")} | ${pl.Series("foo", [7, 4, 17])}
${"fold:minus"} | ${df.fold((a, b) => a.minus(b)).alias("foo")} | ${pl.Series("foo", [-5, 0, 1])}
${"fold:mul"} | ${df.fold((a, b) => a.mul(b)).alias("foo")} | ${pl.Series("foo", [6, 4, 72])}
`("$# $name expected matches actual", ({ expected, actual }) => {
expect(expected).toSeriesEqual(actual);
});
test("fold:lt", () => {
const s1 = pl.Series([1, 2, 3]);
const s2 = pl.Series([4, 5, 6]);
const s3 = pl.Series([7, 8, 1]);
const df = pl.DataFrame([s1, s2, s3]);
const expected = pl.Series("foo", [true, true, false]);
let actual = df.fold((a, b) => a.lessThan(b)).alias("foo");
expect(actual).toSeriesEqual(expected);
actual = df.fold((a, b) => a.lt(b)).alias("foo");
expect(actual).toSeriesEqual(expected);
});
test("frameEqual:true", () => {
const df = pl.DataFrame({
foo: [1, 2, 3],
Expand Down
27 changes: 19 additions & 8 deletions polars/series/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,9 @@ export function _Series(_s: any): Series {
const wrap = (method, ...args): Series => {
return _Series(unwrap(method, ...args));
};
const wraps = (method, args: any): Series => {
return _Series(_s[method as any](args._s));
};
const dtypeWrap = (method: string, ...args: any[]) => {
const dtype = _s.dtype;

Expand Down Expand Up @@ -1528,16 +1531,20 @@ export function _Series(_s: any): Series {
return this.length;
},
lt(field) {
return dtypeWrap("Lt", field);
if (typeof field === "number") return dtypeWrap("Lt", field);
return wraps("lt", field);
Bidek56 marked this conversation as resolved.
Show resolved Hide resolved
},
lessThan(field) {
return dtypeWrap("Lt", field);
if (typeof field === "number") return dtypeWrap("Lt", field);
return wraps("lt", field);
},
ltEq(field) {
return dtypeWrap("LtEq", field);
if (typeof field === "number") return dtypeWrap("LtEq", field);
return wraps("ltEq", field);
},
lessThanEquals(field) {
return dtypeWrap("LtEq", field);
if (typeof field === "number") return dtypeWrap("LtEq", field);
return wraps("ltEq", field);
},
limit(n = 10) {
return wrap("limit", n);
Expand All @@ -1558,16 +1565,19 @@ export function _Series(_s: any): Series {
return wrap("mode");
},
minus(other) {
return dtypeWrap("Sub", other);
if (typeof other === "number") return dtypeWrap("Sub", other);
return wraps("sub", other);
},
mul(other) {
return dtypeWrap("Mul", other);
if (typeof other === "number") return dtypeWrap("Mul", other);
return wraps("mul", other);
},
nChunks() {
return _s.nChunks();
},
neq(other) {
return dtypeWrap("Neq", other);
if (typeof other === "number") return dtypeWrap("Neq", other);
return wraps("neq", other);
},
notEquals(other) {
return this.neq(other);
Expand All @@ -1585,7 +1595,8 @@ export function _Series(_s: any): Series {
return expr_op("peakMin");
},
plus(other) {
return dtypeWrap("Add", other);
if (typeof other === "number") return dtypeWrap("Add", other);
return wraps("add", other);
},
quantile(quantile, interpolation = "nearest") {
return _s.quantile(quantile, interpolation);
Expand Down