diff --git a/__tests__/dataframe.test.ts b/__tests__/dataframe.test.ts index f5a41d6a7..3d29656ac 100644 --- a/__tests__/dataframe.test.ts +++ b/__tests__/dataframe.test.ts @@ -664,6 +664,28 @@ describe("dataframe", () => { }); expect(actual).toFrameEqual(expected); }); + test("unpivot renamed", () => { + const df = pl.DataFrame({ + id: [1], + asset_key_1: ["123"], + asset_key_2: ["456"], + asset_key_3: ["abc"], + }); + const actual = df.unpivot( + "id", + ["asset_key_1", "asset_key_2", "asset_key_3"], + { + variableName: "foo", + valueName: "bar", + }, + ); + const expected = pl.DataFrame({ + id: [1, 1, 1], + foo: ["asset_key_1", "asset_key_2", "asset_key_3"], + bar: ["123", "456", "abc"], + }); + expect(actual).toFrameEqual(expected); + }); test("min:axis:0", () => { const actual = pl .DataFrame({ diff --git a/__tests__/lazyframe.test.ts b/__tests__/lazyframe.test.ts index 71471aa89..778cac397 100644 --- a/__tests__/lazyframe.test.ts +++ b/__tests__/lazyframe.test.ts @@ -1347,4 +1347,29 @@ describe("lazyframe", () => { expect(newDF.sort("foo")).toFrameEqual(actualDf); fs.rmSync("./test.parquet"); }); + test("unpivot renamed", () => { + const ldf = pl + .DataFrame({ + id: [1], + asset_key_1: ["123"], + asset_key_2: ["456"], + asset_key_3: ["abc"], + }) + .lazy(); + const actual = ldf.unpivot( + "id", + ["asset_key_1", "asset_key_2", "asset_key_3"], + { + variableName: "foo", + valueName: "bar", + streamable: true, + }, + ); + const expected = pl.DataFrame({ + id: [1, 1, 1], + foo: ["asset_key_1", "asset_key_2", "asset_key_3"], + bar: ["123", "456", "abc"], + }); + expect(actual.collectSync()).toFrameEqual(expected); + }); }); diff --git a/polars/dataframe.ts b/polars/dataframe.ts index 615fec9d9..9aebdb494 100644 --- a/polars/dataframe.ts +++ b/polars/dataframe.ts @@ -924,10 +924,8 @@ export interface DataFrame * * @param idVars - Columns to use as identifier variables. * @param valueVars - Values to use as value variables. - * @param variableName - Name to give to the `variable` column. Defaults to "variable" - * @param valueName - Name to give to the `value` column. Defaults to "value" - * @param streamable - Allow this node to run in the streaming engine. - If this runs in streaming, the output of the unpivot operation will not have a stable ordering. + * @param options.variableName - Name to give to the `variable` column. Defaults to "variable" + * @param options.valueName - Name to give to the `value` column. Defaults to "value" * @example * ``` * > const df1 = pl.DataFrame({ @@ -951,7 +949,14 @@ export interface DataFrame * └─────┴─────────────┴───────┘ * ``` */ - unpivot(idVars: ColumnSelection, valueVars: ColumnSelection): DataFrame; + unpivot( + idVars: ColumnSelection, + valueVars: ColumnSelection, + options?: { + variableName?: string | null; + valueName?: string | null; + }, + ): DataFrame; /** * Aggregate the columns of this DataFrame to their minimum value. * ___ @@ -1751,7 +1756,7 @@ export interface DataFrame Or combine them: - "3d12h4m25s" # 3 days, 12 hours, 4 minutes, and 25 seconds - By "calendar day", we mean the corresponding time on the next day (which may not be 24 hours, due to daylight savings). + By "calendar day", we mean the corresponding time on the next day (which may not be 24 hours, due to daylight savings). Similarly for "calendar week", "calendar month", "calendar quarter", and "calendar year". Parameters @@ -2177,8 +2182,19 @@ export const _DataFrame = (_df: any): DataFrame => { melt(ids, values) { return wrap("unpivot", columnOrColumns(ids), columnOrColumns(values)); }, - unpivot(ids, values) { - return wrap("unpivot", columnOrColumns(ids), columnOrColumns(values)); + unpivot(ids, values, options) { + options = { + variableName: null, + valueName: null, + ...options, + }; + return wrap( + "unpivot", + columnOrColumns(ids), + columnOrColumns(values), + options.variableName, + options.valueName, + ); }, min(axis = 0) { if (axis === 1) { diff --git a/polars/lazy/dataframe.ts b/polars/lazy/dataframe.ts index 86d663d28..698123753 100644 --- a/polars/lazy/dataframe.ts +++ b/polars/lazy/dataframe.ts @@ -126,7 +126,7 @@ export interface LazyDataFrame extends Serialize, GroupByOps { .. warning:: Streaming mode is considered **unstable**. It may be changed at any point without it being considered a breaking change. - * + * */ fetch(numRows?: number): Promise; fetch(numRows: number, opts: LazyOptions): Promise; @@ -369,7 +369,20 @@ export interface LazyDataFrame extends Serialize, GroupByOps { * @deprecated *since 0.13.0* use {@link unpivot} */ melt(idVars: ColumnSelection, valueVars: ColumnSelection): LazyDataFrame; - unpivot(idVars: ColumnSelection, valueVars: ColumnSelection): LazyDataFrame; + /** + * @see {@link DataFrame.unpivot} + * @param options.streamable - Allow this node to run in the streaming engine. + * If this runs in streaming, the output of the unpivot operation will not have a stable ordering. + */ + unpivot( + idVars: ColumnSelection, + valueVars: ColumnSelection, + options?: { + variableName?: string | null; + valueName?: string | null; + streamable?: boolean; + }, + ): LazyDataFrame; /** * @see {@link DataFrame.min} */ @@ -982,9 +995,21 @@ export const _LazyDataFrame = (_ldf: any): LazyDataFrame => { _ldf.unpivot(columnOrColumnsStrict(ids), columnOrColumnsStrict(values)), ); }, - unpivot(ids, values) { + unpivot(ids, values, options) { + options = { + variableName: null, + valueName: null, + streamable: false, + ...options, + }; return _LazyDataFrame( - _ldf.unpivot(columnOrColumnsStrict(ids), columnOrColumnsStrict(values)), + _ldf.unpivot( + columnOrColumnsStrict(ids), + columnOrColumnsStrict(values), + options.variableName, + options.valueName, + options.streamable, + ), ); }, min() { diff --git a/src/dataframe.rs b/src/dataframe.rs index f44ac4c6e..2e2fc12ee 100644 --- a/src/dataframe.rs +++ b/src/dataframe.rs @@ -925,15 +925,15 @@ impl JsDataFrame { &self, id_vars: Vec, value_vars: Vec, - value_name: Option, variable_name: Option, + value_name: Option, streamable: Option, ) -> napi::Result { let args = UnpivotArgs { index: strings_to_smartstrings(id_vars), on: strings_to_smartstrings(value_vars), - value_name: value_name.map(|s| s.into()), variable_name: variable_name.map(|s| s.into()), + value_name: value_name.map(|s| s.into()), streamable: streamable.unwrap_or(false), }; diff --git a/src/lazy/dataframe.rs b/src/lazy/dataframe.rs index e82f53117..1624321df 100644 --- a/src/lazy/dataframe.rs +++ b/src/lazy/dataframe.rs @@ -504,15 +504,15 @@ impl JsLazyFrame { &self, id_vars: Vec<&str>, value_vars: Vec<&str>, - value_name: Option<&str>, variable_name: Option<&str>, + value_name: Option<&str>, streamable: Option, ) -> JsLazyFrame { let args = UnpivotArgs { index: strings_to_smartstrings(id_vars), on: strings_to_smartstrings(value_vars), - value_name: value_name.map(|s| s.into()), variable_name: variable_name.map(|s| s.into()), + value_name: value_name.map(|s| s.into()), streamable: streamable.unwrap_or(false), }; let ldf = self.ldf.clone();