Skip to content

Commit

Permalink
Fixing expr unique with maintainOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
Bidek56 committed Nov 14, 2023
1 parent b4b5339 commit 334185b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
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

0 comments on commit 334185b

Please sign in to comment.