From d9b9b4c90403db258d6b120dc2bb9c5be45ccf79 Mon Sep 17 00:00:00 2001 From: Brian Brown Date: Sat, 10 Aug 2024 14:27:01 -0400 Subject: [PATCH] update pivot on to first pos argument --- __tests__/dataframe.test.ts | 43 +++++++++++++++++++++++++++++++++++++ polars/dataframe.ts | 2 +- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/__tests__/dataframe.test.ts b/__tests__/dataframe.test.ts index f5a41d6a7..5934c84a0 100644 --- a/__tests__/dataframe.test.ts +++ b/__tests__/dataframe.test.ts @@ -1354,6 +1354,49 @@ describe("dataframe", () => { "e|b": [null, "y"], }); expect(actual).toFrameEqual(expected, true); + + df = pl.DataFrame({ + foo: ["A", "A", "B", "B", "C"], + N: [1, 2, 2, 4, 2], + bar: ["k", "l", "m", "n", "o"], + }); + actual = df.pivot(["N"], { + index: "foo", + on: "bar", + aggregateFunc: "first", + }); + expected = pl.DataFrame({ + foo: ["A", "B", "C"], + k: [1, null, null], + l: [2, null, null], + m: [null, 2, null], + n: [null, 4, null], + o: [null, null, 2], + }); + expect(actual).toFrameEqual(expected, true); + + df = pl.DataFrame({ + ix: [1, 1, 2, 2, 1, 2], + col: ["a", "a", "a", "a", "b", "b"], + foo: [0, 1, 2, 2, 7, 1], + bar: [0, 2, 0, 0, 9, 4], + }); + + actual = df.pivot(["foo", "bar"], { + index: "ix", + on: "col", + aggregateFunc: "sum", + separator: "/", + }); + + expected = pl.DataFrame({ + ix: [1, 2], + "foo/a": [1, 4], + "foo/b": [7, 1], + "bar/a": [2, 0], + "bar/b": [9, 4], + }); + expect(actual).toFrameEqual(expected, true); }); }); describe("join", () => { diff --git a/polars/dataframe.ts b/polars/dataframe.ts index 615fec9d9..e2b85dbb6 100644 --- a/polars/dataframe.ts +++ b/polars/dataframe.ts @@ -2237,8 +2237,8 @@ export const _DataFrame = (_df: any): DataFrame => { return _DataFrame( _df.pivotExpr( values, - index, on, + index, fn, maintainOrder, sortColumns,