Skip to content

Commit 137a2d9

Browse files
authored
Adding pivot separator option (#181)
* Adding pivot separator option * Fixing documentation
1 parent 307c034 commit 137a2d9

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

__tests__/dataframe.test.ts

+27-3
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,7 @@ describe("dataframe", () => {
12431243
expect(actual).toFrameEqual(expected);
12441244
});
12451245
test("pivot", () => {
1246-
const df = pl.DataFrame({
1246+
let df = pl.DataFrame({
12471247
a: pl.Series([1, 2, 3]).cast(pl.Int32),
12481248
b: pl
12491249
.Series([
@@ -1254,7 +1254,7 @@ describe("dataframe", () => {
12541254
.cast(pl.List(pl.Int32)),
12551255
});
12561256

1257-
const expected = pl
1257+
let expected = pl
12581258
.DataFrame({
12591259
a: pl.Series([1, 2, 3]).cast(pl.Int32),
12601260
"1": pl.Series([[1, 1], null, null]).cast(pl.List(pl.Int32)),
@@ -1263,14 +1263,38 @@ describe("dataframe", () => {
12631263
})
12641264
.select("a", "1", "2", "3");
12651265

1266-
const actual = df.pivot("b", {
1266+
let actual = df.pivot("b", {
12671267
index: "a",
12681268
columns: "a",
12691269
aggregateFunc: "first",
12701270
sortColumns: true,
12711271
});
12721272

12731273
expect(actual).toFrameEqual(expected, true);
1274+
1275+
df = pl.DataFrame({
1276+
a: ["beep", "bop"],
1277+
b: ["a", "b"],
1278+
c: ["s", "f"],
1279+
d: [7, 8],
1280+
e: ["x", "y"],
1281+
});
1282+
actual = df.pivot(["a", "e"], {
1283+
index: "b",
1284+
columns: ["c"],
1285+
aggregateFunc: "first",
1286+
separator: "|",
1287+
maintainOrder: true,
1288+
});
1289+
1290+
expected = pl.DataFrame({
1291+
b: ["a", "b"],
1292+
"a|c|s": ["beep", null],
1293+
"a|c|f": [null, "bop"],
1294+
"e|c|s": ["x", null],
1295+
"e|c|f": [null, "y"],
1296+
});
1297+
expect(actual).toFrameEqual(expected, true);
12741298
});
12751299
});
12761300
describe("join", () => {

bun.lockb

-130 KB
Binary file not shown.

polars/dataframe.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,7 @@ export interface DataFrame
10081008
* Defaults to "first"
10091009
* @param options.maintainOrder Sort the grouped keys so that the output order is predictable.
10101010
* @param options.sortColumns Sort the transposed columns by name. Default is by order of discovery.
1011+
* @param options.separator Used as separator/delimiter in generated column names.
10111012
* @example
10121013
* ```
10131014
* > const df = pl.DataFrame(
@@ -1017,12 +1018,12 @@ export interface DataFrame
10171018
* ... "baz": [1, 2, 3, 4, 5, 6],
10181019
* ... }
10191020
* ... );
1020-
* > df.pivot({values:"baz", index:"foo", columns:"bar"});
1021+
* > df.pivot(values:"baz", {index:"foo", columns:"bar"});
10211022
* shape: (2, 4)
10221023
* ┌─────┬─────┬─────┬─────┐
10231024
* │ foo ┆ A ┆ B ┆ C │
10241025
* │ --- ┆ --- ┆ --- ┆ --- │
1025-
* │ str ┆ i64i64i64
1026+
* │ str ┆ f64f64f64
10261027
* ╞═════╪═════╪═════╪═════╡
10271028
* │ one ┆ 1 ┆ 2 ┆ 3 │
10281029
* ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
@@ -1047,6 +1048,7 @@ export interface DataFrame
10471048
| Expr;
10481049
maintainOrder?: boolean;
10491050
sortColumns?: boolean;
1051+
separator?: string;
10501052
},
10511053
): DataFrame;
10521054
pivot(options: {
@@ -1065,6 +1067,7 @@ export interface DataFrame
10651067
| Expr;
10661068
maintainOrder?: boolean;
10671069
sortColumns?: boolean;
1070+
separator?: string;
10681071
}): DataFrame;
10691072
// TODO!
10701073
// /**
@@ -2188,6 +2191,7 @@ export const _DataFrame = (_df: any): DataFrame => {
21882191
maintainOrder = true,
21892192
sortColumns = false,
21902193
aggregateFunc = "first",
2194+
separator,
21912195
} = options;
21922196
values = values_ ?? values;
21932197
values = typeof values === "string" ? [values] : values;
@@ -2216,7 +2220,15 @@ export const _DataFrame = (_df: any): DataFrame => {
22162220
}
22172221

22182222
return _DataFrame(
2219-
_df.pivotExpr(values, index, columns, fn, maintainOrder, sortColumns),
2223+
_df.pivotExpr(
2224+
values,
2225+
index,
2226+
columns,
2227+
fn,
2228+
maintainOrder,
2229+
sortColumns,
2230+
separator,
2231+
),
22202232
);
22212233
},
22222234
quantile(quantile) {

0 commit comments

Comments
 (0)