Skip to content

Commit 7388e3f

Browse files
committed
Add variableName and valueName to unpivot
1 parent 48bd3b9 commit 7388e3f

File tree

6 files changed

+103
-16
lines changed

6 files changed

+103
-16
lines changed

__tests__/dataframe.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,28 @@ describe("dataframe", () => {
664664
});
665665
expect(actual).toFrameEqual(expected);
666666
});
667+
test("unpivot renamed", () => {
668+
const df = pl.DataFrame({
669+
id: [1],
670+
asset_key_1: ["123"],
671+
asset_key_2: ["456"],
672+
asset_key_3: ["abc"],
673+
});
674+
const actual = df.unpivot(
675+
"id",
676+
["asset_key_1", "asset_key_2", "asset_key_3"],
677+
{
678+
variableName: "foo",
679+
valueName: "bar"
680+
}
681+
);
682+
const expected = pl.DataFrame({
683+
id: [1, 1, 1],
684+
foo: ["asset_key_1", "asset_key_2", "asset_key_3"],
685+
bar: ["123", "456", "abc"],
686+
});
687+
expect(actual).toFrameEqual(expected);
688+
});
667689
test("min:axis:0", () => {
668690
const actual = pl
669691
.DataFrame({

__tests__/lazyframe.test.ts

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

polars/dataframe.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -924,10 +924,8 @@ export interface DataFrame
924924
*
925925
* @param idVars - Columns to use as identifier variables.
926926
* @param valueVars - Values to use as value variables.
927-
* @param variableName - Name to give to the `variable` column. Defaults to "variable"
928-
* @param valueName - Name to give to the `value` column. Defaults to "value"
929-
* @param streamable - Allow this node to run in the streaming engine.
930-
If this runs in streaming, the output of the unpivot operation will not have a stable ordering.
927+
* @param options.variableName - Name to give to the `variable` column. Defaults to "variable"
928+
* @param options.valueName - Name to give to the `value` column. Defaults to "value"
931929
* @example
932930
* ```
933931
* > const df1 = pl.DataFrame({
@@ -951,7 +949,14 @@ export interface DataFrame
951949
* └─────┴─────────────┴───────┘
952950
* ```
953951
*/
954-
unpivot(idVars: ColumnSelection, valueVars: ColumnSelection): DataFrame;
952+
unpivot(
953+
idVars: ColumnSelection,
954+
valueVars: ColumnSelection,
955+
options?: {
956+
variableName?: string | null,
957+
valueName?: string | null,
958+
}
959+
): DataFrame;
955960
/**
956961
* Aggregate the columns of this DataFrame to their minimum value.
957962
* ___
@@ -1751,7 +1756,7 @@ export interface DataFrame
17511756
Or combine them:
17521757
- "3d12h4m25s" # 3 days, 12 hours, 4 minutes, and 25 seconds
17531758
1754-
By "calendar day", we mean the corresponding time on the next day (which may not be 24 hours, due to daylight savings).
1759+
By "calendar day", we mean the corresponding time on the next day (which may not be 24 hours, due to daylight savings).
17551760
Similarly for "calendar week", "calendar month", "calendar quarter", and "calendar year".
17561761
17571762
Parameters
@@ -2177,8 +2182,19 @@ export const _DataFrame = (_df: any): DataFrame => {
21772182
melt(ids, values) {
21782183
return wrap("unpivot", columnOrColumns(ids), columnOrColumns(values));
21792184
},
2180-
unpivot(ids, values) {
2181-
return wrap("unpivot", columnOrColumns(ids), columnOrColumns(values));
2185+
unpivot(ids, values, options) {
2186+
options = {
2187+
variableName: null,
2188+
valueName: null,
2189+
...options
2190+
}
2191+
return wrap(
2192+
"unpivot",
2193+
columnOrColumns(ids),
2194+
columnOrColumns(values),
2195+
options.variableName,
2196+
options.valueName
2197+
);
21822198
},
21832199
min(axis = 0) {
21842200
if (axis === 1) {

polars/lazy/dataframe.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export interface LazyDataFrame extends Serialize, GroupByOps<LazyGroupBy> {
126126
.. warning::
127127
Streaming mode is considered **unstable**. It may be changed
128128
at any point without it being considered a breaking change.
129-
*
129+
*
130130
*/
131131
fetch(numRows?: number): Promise<DataFrame>;
132132
fetch(numRows: number, opts: LazyOptions): Promise<DataFrame>;
@@ -369,7 +369,20 @@ export interface LazyDataFrame extends Serialize, GroupByOps<LazyGroupBy> {
369369
* @deprecated *since 0.13.0* use {@link unpivot}
370370
*/
371371
melt(idVars: ColumnSelection, valueVars: ColumnSelection): LazyDataFrame;
372-
unpivot(idVars: ColumnSelection, valueVars: ColumnSelection): LazyDataFrame;
372+
/**
373+
* @see {@link DataFrame.unpivot}
374+
* @param options.streamable - Allow this node to run in the streaming engine.
375+
* If this runs in streaming, the output of the unpivot operation will not have a stable ordering.
376+
*/
377+
unpivot(
378+
idVars: ColumnSelection,
379+
valueVars: ColumnSelection,
380+
options?: {
381+
variableName?: string | null,
382+
valueName?: string | null,
383+
streamable?: boolean,
384+
}
385+
): LazyDataFrame;
373386
/**
374387
* @see {@link DataFrame.min}
375388
*/
@@ -982,9 +995,21 @@ export const _LazyDataFrame = (_ldf: any): LazyDataFrame => {
982995
_ldf.unpivot(columnOrColumnsStrict(ids), columnOrColumnsStrict(values)),
983996
);
984997
},
985-
unpivot(ids, values) {
998+
unpivot(ids, values, options) {
999+
options = {
1000+
variableName: null,
1001+
valueName: null,
1002+
streamable: false,
1003+
...options
1004+
}
9861005
return _LazyDataFrame(
987-
_ldf.unpivot(columnOrColumnsStrict(ids), columnOrColumnsStrict(values)),
1006+
_ldf.unpivot(
1007+
columnOrColumnsStrict(ids),
1008+
columnOrColumnsStrict(values),
1009+
options.variableName,
1010+
options.valueName,
1011+
options.streamable
1012+
),
9881013
);
9891014
},
9901015
min() {

src/dataframe.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -925,15 +925,15 @@ impl JsDataFrame {
925925
&self,
926926
id_vars: Vec<String>,
927927
value_vars: Vec<String>,
928-
value_name: Option<String>,
929928
variable_name: Option<String>,
929+
value_name: Option<String>,
930930
streamable: Option<bool>,
931931
) -> napi::Result<JsDataFrame> {
932932
let args = UnpivotArgs {
933933
index: strings_to_smartstrings(id_vars),
934934
on: strings_to_smartstrings(value_vars),
935-
value_name: value_name.map(|s| s.into()),
936935
variable_name: variable_name.map(|s| s.into()),
936+
value_name: value_name.map(|s| s.into()),
937937
streamable: streamable.unwrap_or(false),
938938
};
939939

src/lazy/dataframe.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,15 +504,15 @@ impl JsLazyFrame {
504504
&self,
505505
id_vars: Vec<&str>,
506506
value_vars: Vec<&str>,
507-
value_name: Option<&str>,
508507
variable_name: Option<&str>,
508+
value_name: Option<&str>,
509509
streamable: Option<bool>,
510510
) -> JsLazyFrame {
511511
let args = UnpivotArgs {
512512
index: strings_to_smartstrings(id_vars),
513513
on: strings_to_smartstrings(value_vars),
514-
value_name: value_name.map(|s| s.into()),
515514
variable_name: variable_name.map(|s| s.into()),
515+
value_name: value_name.map(|s| s.into()),
516516
streamable: streamable.unwrap_or(false),
517517
};
518518
let ldf = self.ldf.clone();

0 commit comments

Comments
 (0)