From 334185bd1b3941c4a3deffdbf60ce09be092f4d8 Mon Sep 17 00:00:00 2001 From: Bidek56 Date: Tue, 14 Nov 2023 12:00:33 -0500 Subject: [PATCH] Fixing expr unique with maintainOrder --- __tests__/expr.test.ts | 13 ++++++++----- polars/lazy/expr/index.ts | 7 ++++--- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/__tests__/expr.test.ts b/__tests__/expr.test.ts index 23a827f74..4c09c2730 100644 --- a/__tests__/expr.test.ts +++ b/__tests__/expr.test.ts @@ -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([ diff --git a/polars/lazy/expr/index.ts b/polars/lazy/expr/index.ts index 0f5f78d88..288055349 100644 --- a/polars/lazy/expr/index.ts +++ b/polars/lazy/expr/index.ts @@ -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. */ @@ -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");