Skip to content

Commit d1686a6

Browse files
Bidek56sezanzeb
andauthored
Add variableName and valueName to unpivot (#340)
Add variableName and valueName to unpivot to close #246 --------- Co-authored-by: sezanzeb <[email protected]> Co-authored-by: Bidek56 <[email protected]>
1 parent 487fc13 commit d1686a6

File tree

4 files changed

+100
-5
lines changed

4 files changed

+100
-5
lines changed

__tests__/dataframe.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,28 @@ describe("dataframe", () => {
673673
});
674674
expect(actual).toFrameEqual(expected);
675675
});
676+
test("unpivot renamed", () => {
677+
const df = pl.DataFrame({
678+
id: [1],
679+
asset_key_1: ["123"],
680+
asset_key_2: ["456"],
681+
asset_key_3: ["abc"],
682+
});
683+
const actual = df.unpivot(
684+
"id",
685+
["asset_key_1", "asset_key_2", "asset_key_3"],
686+
{
687+
variableName: "foo",
688+
valueName: "bar",
689+
},
690+
);
691+
const expected = pl.DataFrame({
692+
id: [1, 1, 1],
693+
foo: ["asset_key_1", "asset_key_2", "asset_key_3"],
694+
bar: ["123", "456", "abc"],
695+
});
696+
expect(actual).toFrameEqual(expected);
697+
});
676698
test("min:axis:0", () => {
677699
const actual = pl
678700
.DataFrame({

__tests__/lazyframe.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,4 +1351,28 @@ describe("lazyframe", () => {
13511351
expect(newDF.sort("foo")).toFrameEqual(actualDf);
13521352
fs.rmSync("./test.parquet");
13531353
});
1354+
test("unpivot renamed", () => {
1355+
const ldf = pl
1356+
.DataFrame({
1357+
id: [1],
1358+
asset_key_1: ["123"],
1359+
asset_key_2: ["456"],
1360+
asset_key_3: ["abc"],
1361+
})
1362+
.lazy();
1363+
const actual = ldf.unpivot(
1364+
"id",
1365+
["asset_key_1", "asset_key_2", "asset_key_3"],
1366+
{
1367+
variableName: "foo",
1368+
valueName: "bar",
1369+
},
1370+
);
1371+
const expected = pl.DataFrame({
1372+
id: [1, 1, 1],
1373+
foo: ["asset_key_1", "asset_key_2", "asset_key_3"],
1374+
bar: ["123", "456", "abc"],
1375+
});
1376+
expect(actual.collectSync()).toFrameEqual(expected);
1377+
});
13541378
});

polars/dataframe.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,8 @@ export interface DataFrame<T extends Record<string, Series> = any>
10001000
*
10011001
* @param idVars - Columns to use as identifier variables.
10021002
* @param valueVars - Values to use as value variables.
1003+
* @param options.variableName - Name to give to the `variable` column. Defaults to "variable"
1004+
* @param options.valueName - Name to give to the `value` column. Defaults to "value"
10031005
* @example
10041006
* ```
10051007
* > const df1 = pl.DataFrame({
@@ -1023,7 +1025,14 @@ export interface DataFrame<T extends Record<string, Series> = any>
10231025
* └─────┴─────────────┴───────┘
10241026
* ```
10251027
*/
1026-
unpivot(idVars: ColumnSelection, valueVars: ColumnSelection): DataFrame;
1028+
unpivot(
1029+
idVars: ColumnSelection,
1030+
valueVars: ColumnSelection,
1031+
options?: {
1032+
variableName?: string | null;
1033+
valueName?: string | null;
1034+
},
1035+
): DataFrame;
10271036
/**
10281037
* Aggregate the columns of this DataFrame to their minimum value.
10291038
* ___
@@ -2309,9 +2318,23 @@ export const _DataFrame = (_df: any): DataFrame => {
23092318
median() {
23102319
return this.lazy().median().collectSync();
23112320
},
2312-
unpivot(ids, values) {
2321+
melt(ids, values) {
23132322
return wrap("unpivot", columnOrColumns(ids), columnOrColumns(values));
23142323
},
2324+
unpivot(ids, values, options) {
2325+
options = {
2326+
variableName: null,
2327+
valueName: null,
2328+
...options,
2329+
};
2330+
return wrap(
2331+
"unpivot",
2332+
columnOrColumns(ids),
2333+
columnOrColumns(values),
2334+
options.variableName,
2335+
options.valueName,
2336+
);
2337+
},
23152338
min(axis = 0) {
23162339
if (axis === 1) {
23172340
return _Series(_df.hmin() as any) as any;

polars/lazy/dataframe.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export interface LazyDataFrame extends Serialize, GroupByOps<LazyGroupBy> {
107107
.. warning::
108108
Streaming mode is considered **unstable**. It may be changed
109109
at any point without it being considered a breaking change.
110-
*
110+
*
111111
*/
112112
fetch(numRows: number, opts: LazyOptions): Promise<DataFrame>;
113113
fetch(numRows?: number): Promise<DataFrame>;
@@ -351,7 +351,18 @@ export interface LazyDataFrame extends Serialize, GroupByOps<LazyGroupBy> {
351351
/**
352352
* @see {@link DataFrame.unpivot}
353353
*/
354-
unpivot(idVars: ColumnSelection, valueVars: ColumnSelection): LazyDataFrame;
354+
melt(idVars: ColumnSelection, valueVars: ColumnSelection): LazyDataFrame;
355+
/**
356+
* @see {@link DataFrame.unpivot}
357+
*/
358+
unpivot(
359+
idVars: ColumnSelection,
360+
valueVars: ColumnSelection,
361+
options?: {
362+
variableName?: string | null;
363+
valueName?: string | null;
364+
},
365+
): LazyDataFrame;
355366
/**
356367
* @see {@link DataFrame.min}
357368
*/
@@ -947,11 +958,26 @@ export const _LazyDataFrame = (_ldf: any): LazyDataFrame => {
947958
median() {
948959
return _LazyDataFrame(_ldf.median());
949960
},
950-
unpivot(ids, values) {
961+
melt(ids, values) {
951962
return _LazyDataFrame(
952963
_ldf.unpivot(columnOrColumnsStrict(ids), columnOrColumnsStrict(values)),
953964
);
954965
},
966+
unpivot(ids, values, options) {
967+
options = {
968+
variableName: null,
969+
valueName: null,
970+
...options,
971+
};
972+
return _LazyDataFrame(
973+
_ldf.unpivot(
974+
columnOrColumnsStrict(ids),
975+
columnOrColumnsStrict(values),
976+
options.variableName,
977+
options.valueName,
978+
),
979+
);
980+
},
955981
min() {
956982
return _LazyDataFrame(_ldf.min());
957983
},

0 commit comments

Comments
 (0)