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

feat: Support nulls_last option in DataFrame#sort #234

Merged
merged 3 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 22 additions & 0 deletions __tests__/dataframe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,28 @@ describe("dataframe", () => {
});
expect(actual).toFrameEqual(expected);
});
test("sort:nulls_last:false", () => {
const actual = pl
.DataFrame({
foo: [1, null, 2, 3],
})
.sort({ by: "foo", nulls_last: false });
const expected = pl.DataFrame({
foo: [null, 1, 2, 3],
});
expect(actual).toFrameEqual(expected);
});
test("sort:nulls_last:true", () => {
const actual = pl
.DataFrame({
foo: [1, null, 2, 3],
})
.sort({ by: "foo", nulls_last: true });
const expected = pl.DataFrame({
foo: [1, 2, 3, null],
});
expect(actual).toFrameEqual(expected);
});
test("std", () => {
const actual = pl
.DataFrame({
Expand Down
26 changes: 26 additions & 0 deletions __tests__/lazyframe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,32 @@ describe("lazyframe", () => {
});
expect(actual).toFrameEqual(expected);
});
test("sort:nulls_last:false", () => {
const actual = pl
.DataFrame({
foo: [1, null, 2, 3],
})
.lazy()
.sort({ by: "foo", nulls_last: false })
.collectSync();
const expected = pl.DataFrame({
foo: [null, 1, 2, 3],
});
expect(actual).toFrameEqual(expected);
});
test("sort:nulls_last:true", () => {
const actual = pl
.DataFrame({
foo: [1, null, 2, 3],
})
.lazy()
.sort({ by: "foo", nulls_last: true })
.collectSync();
const expected = pl.DataFrame({
foo: [1, 2, 3, null],
});
expect(actual).toFrameEqual(expected);
});
test("sum", () => {
const actual = pl
.DataFrame({
Expand Down
15 changes: 11 additions & 4 deletions polars/dataframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,7 @@ export interface DataFrame
sort(
by: ColumnsOrExpr,
descending?: boolean,
nulls_last?: boolean,
maintain_order?: boolean,
): DataFrame;
sort({
Expand All @@ -1345,6 +1346,7 @@ export interface DataFrame
}: {
by: ColumnsOrExpr;
descending?: boolean;
nulls_last?: boolean;
maintain_order?: boolean;
}): DataFrame;
/**
Expand Down Expand Up @@ -2332,17 +2334,22 @@ export const _DataFrame = (_df: any): DataFrame => {
}
return wrap("slice", opts.offset, opts.length);
},
sort(arg, descending = false, maintain_order = false) {
sort(arg, descending = false, nulls_last = false, maintain_order = false) {
if (arg?.by !== undefined) {
return this.sort(arg.by, arg.descending);
return this.sort(
arg.by,
arg.descending,
arg.nulls_last,
arg.maintain_order,
);
}
if (Array.isArray(arg) || Expr.isExpr(arg)) {
return _DataFrame(_df)
.lazy()
.sort(arg, descending, maintain_order)
.sort(arg, descending, nulls_last, maintain_order)
.collectSync({ noOptimization: true, stringCache: false });
}
return wrap("sort", arg, descending, true, maintain_order);
return wrap("sort", arg, descending, nulls_last, maintain_order);
},
std() {
return this.lazy().std().collectSync();
Expand Down
15 changes: 11 additions & 4 deletions polars/lazy/dataframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,11 +401,13 @@ export interface LazyDataFrame extends Serialize, GroupByOps<LazyGroupBy> {
sort(
by: ColumnsOrExpr,
descending?: ValueOrArray<boolean>,
nulls_last?: boolean,
maintain_order?: boolean,
): LazyDataFrame;
sort(opts: {
by: ColumnsOrExpr;
descending?: ValueOrArray<boolean>;
nulls_last?: boolean;
maintain_order?: boolean;
}): LazyDataFrame;
/**
Expand Down Expand Up @@ -965,15 +967,20 @@ export const _LazyDataFrame = (_ldf: any): LazyDataFrame => {
}
return _LazyDataFrame(_ldf.slice(opt, len));
},
sort(arg, descending = false, maintain_order = false) {
sort(arg, descending = false, nulls_last = false, maintain_order = false) {
if (arg?.by !== undefined) {
return this.sort(arg.by, arg.descending, arg.maintain_order);
return this.sort(
arg.by,
arg.descending,
arg.nulls_last,
arg.maintain_order,
);
}
if (typeof arg === "string") {
return wrap("sort", arg, descending, true, maintain_order);
return wrap("sort", arg, descending, nulls_last, maintain_order);
}
const by = selectionToExprList(arg, false);
return wrap("sortByExprs", by, descending, true, maintain_order);
return wrap("sortByExprs", by, descending, nulls_last, maintain_order);
},
std() {
return _LazyDataFrame(_ldf.std());
Expand Down