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 unique expr with maintainOrder option #143

Merged
merged 1 commit into from
Dec 5, 2023
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
13 changes: 8 additions & 5 deletions __tests__/expr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -879,14 +879,17 @@ describe("expr", () => {
expect(actual).toFrameEqual(expected);
});
test("unique", () => {
const df = pl.DataFrame({ a: [1, 1, 2, 2, 3, 3, 8, null, 1] });
const expected = pl.DataFrame({
uniques: [1, 2, 3, 8, null],
});
const actual = df.select(
const df = pl.DataFrame({ a: [2, 3, 1, 2, 1, 3, 8, null, 1] });
let expected = pl.DataFrame({ uniques: [1, 2, 3, 8, null] });
let actual = df.select(
col("a").unique().sort({ nullsLast: true }).as("uniques"),
);
expect(actual).toFrameEqual(expected);
expected = pl.DataFrame({ uniques: [2, 3, 1, 8, null] });
actual = df.select(col("a").unique(true).as("uniques"));
expect(actual).toFrameEqual(expected);
actual = df.select(col("a").unique({ maintainOrder: true }).as("uniques"));
expect(actual).toFrameEqual(expected);
});
test("upperBound", () => {
const df = pl.DataFrame([
Expand Down
7 changes: 4 additions & 3 deletions polars/lazy/expr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,8 @@ export interface Expr
* Get the unique values of this expression;
* @param maintainOrder Maintain order of data. This requires more work.
*/
unique(maintainOrder?: boolean | { maintainOrder: boolean }): Expr;
unique(opt: { maintainOrder: boolean }): Expr;
unique(maintainOrder?: boolean): Expr;
/** Returns a unit Series with the highest value possible for the dtype of this expression. */
upperBound(): Expr;
/** Get variance. */
Expand Down Expand Up @@ -1195,8 +1196,8 @@ export const _Expr = (_expr: any): Expr => {
return _Expr(_expr.takeEvery(n));
},
unique(opt?) {
if (opt) {
return wrap("unique_stable");
if (opt || opt?.maintainOrder) {
return wrap("uniqueStable");
}

return wrap("unique");
Expand Down