diff --git a/crates/polars-core/src/frame/explode.rs b/crates/polars-core/src/frame/explode.rs index 047497aa8a11..0f0d7a86d282 100644 --- a/crates/polars-core/src/frame/explode.rs +++ b/crates/polars-core/src/frame/explode.rs @@ -244,11 +244,11 @@ impl DataFrame { I: IntoVec, J: IntoVec, { - let id_vars = index.into_vec(); - let value_vars = on.into_vec(); + let index = index.into_vec(); + let on = on.into_vec(); self.unpivot2(UnpivotArgs { - index: id_vars, - on: value_vars, + index, + on, ..Default::default() }) } diff --git a/crates/polars-lazy/src/frame/pivot.rs b/crates/polars-lazy/src/frame/pivot.rs index 8c73c61201cb..f6fc6851739e 100644 --- a/crates/polars-lazy/src/frame/pivot.rs +++ b/crates/polars-lazy/src/frame/pivot.rs @@ -32,8 +32,8 @@ impl PhysicalAggExpr for PivotExpr { pub fn pivot( df: &DataFrame, - index: I0, on: I1, + index: I0, values: Option, sort_columns: bool, agg_expr: Option, @@ -58,8 +58,8 @@ where pub fn pivot_stable( df: &DataFrame, - index: I0, - on: I1, + on: I0, + index: I1, values: Option, sort_columns: bool, agg_expr: Option, diff --git a/crates/polars-ops/src/frame/pivot/mod.rs b/crates/polars-ops/src/frame/pivot/mod.rs index 688524662037..194f3a2c5d69 100644 --- a/crates/polars-ops/src/frame/pivot/mod.rs +++ b/crates/polars-ops/src/frame/pivot/mod.rs @@ -84,8 +84,8 @@ fn restore_logical_type(s: &Series, logical_type: &DataType) -> Series { /// If you have a relatively large table, consider using a group_by over a pivot. pub fn pivot( pivot_df: &DataFrame, - index: I0, - on: I1, + on: I0, + index: I1, values: Option, sort_columns: bool, agg_fn: Option, @@ -127,8 +127,8 @@ where /// If you have a relatively large table, consider using a group_by over a pivot. pub fn pivot_stable( pivot_df: &DataFrame, - index: I0, - on: I1, + on: I0, + index: I1, values: Option, sort_columns: bool, agg_fn: Option, diff --git a/py-polars/polars/dataframe/frame.py b/py-polars/polars/dataframe/frame.py index f18fd1ab23f0..9cc63ceb9590 100644 --- a/py-polars/polars/dataframe/frame.py +++ b/py-polars/polars/dataframe/frame.py @@ -7589,9 +7589,9 @@ def explode( @deprecate_renamed_parameter("columns", "on", version="1.0.0") def pivot( self, + on: ColumnNameOrSelector | Sequence[ColumnNameOrSelector], *, - on: ColumnNameOrSelector | Sequence[ColumnNameOrSelector] | None, - index: ColumnNameOrSelector | Sequence[ColumnNameOrSelector] | None, + index: ColumnNameOrSelector | Sequence[ColumnNameOrSelector], values: ColumnNameOrSelector | Sequence[ColumnNameOrSelector] | None, aggregate_function: PivotAgg | Expr | None = None, maintain_order: bool = True, @@ -7606,13 +7606,13 @@ def pivot( Parameters ---------- + on + Name of the column(s) whose values will be used as the header of the output + DataFrame. values Column values to aggregate. If None, all remaining columns will be used. index One or multiple keys to group by. - on - Name of the column(s) whose values will be used as the header of the output - DataFrame. aggregate_function Choose from: @@ -7659,8 +7659,8 @@ def pivot( >>> import polars.selectors as cs >>> df.pivot( + ... cs.string(), ... index=cs.string(), - ... on=cs.string(), ... values=cs.numeric(), ... aggregate_function="sum", ... sort_columns=True, @@ -7689,8 +7689,8 @@ def pivot( ... } ... ) >>> df.pivot( + ... "col2", ... index="col1", - ... on="col2", ... values="col3", ... aggregate_function=pl.element().tanh().mean(), ... ) @@ -7738,8 +7738,8 @@ def pivot( ... } ... ) >>> df.pivot( + ... "col", ... index="ix", - ... on="col", ... values=["foo", "bar"], ... aggregate_function="sum", ... separator="/", @@ -7793,8 +7793,8 @@ def pivot( return self._from_pydf( self._df.pivot_expr( - index, on, + index, values, maintain_order, sort_columns, @@ -7805,8 +7805,8 @@ def pivot( def unpivot( self, - *, on: ColumnNameOrSelector | Sequence[ColumnNameOrSelector] | None = None, + *, index: ColumnNameOrSelector | Sequence[ColumnNameOrSelector] | None = None, variable_name: str | None = None, value_name: str | None = None, @@ -7867,7 +7867,7 @@ def unpivot( on = [] if on is None else _expand_selectors(self, on) index = [] if index is None else _expand_selectors(self, index) - return self._from_pydf(self._df.unpivot(index, on, value_name, variable_name)) + return self._from_pydf(self._df.unpivot(on, index, value_name, variable_name)) @unstable() def unstack( diff --git a/py-polars/polars/lazyframe/frame.py b/py-polars/polars/lazyframe/frame.py index 5e0728a937cd..fd7d0fb6e4be 100644 --- a/py-polars/polars/lazyframe/frame.py +++ b/py-polars/polars/lazyframe/frame.py @@ -5773,7 +5773,7 @@ def unpivot( index = [] if index is None else _expand_selectors(self, index) return self._from_pyldf( - self._ldf.unpivot(index, on, value_name, variable_name, streamable) + self._ldf.unpivot(on, index, value_name, variable_name, streamable) ) def map_batches( diff --git a/py-polars/src/dataframe/general.rs b/py-polars/src/dataframe/general.rs index 71031c9aabc7..421afe0c5995 100644 --- a/py-polars/src/dataframe/general.rs +++ b/py-polars/src/dataframe/general.rs @@ -399,8 +399,8 @@ impl PyDataFrame { pub fn unpivot( &self, - index: Vec, on: Vec, + index: Vec, value_name: Option<&str>, variable_name: Option<&str>, ) -> PyResult { @@ -417,11 +417,11 @@ impl PyDataFrame { } #[cfg(feature = "pivot")] - #[pyo3(signature = (index, on, values, maintain_order, sort_columns, aggregate_expr, separator))] + #[pyo3(signature = (on, index, values, maintain_order, sort_columns, aggregate_expr, separator))] pub fn pivot_expr( &self, - index: Vec, on: Vec, + index: Vec, values: Option>, maintain_order: bool, sort_columns: bool, diff --git a/py-polars/src/lazyframe/mod.rs b/py-polars/src/lazyframe/mod.rs index 022114db4e42..277a2c1980a7 100644 --- a/py-polars/src/lazyframe/mod.rs +++ b/py-polars/src/lazyframe/mod.rs @@ -1111,18 +1111,18 @@ impl PyLazyFrame { ldf.tail(n).into() } - #[pyo3(signature = (id_vars, value_vars, value_name, variable_name, streamable))] + #[pyo3(signature = (on, index, value_name, variable_name, streamable))] fn unpivot( &self, - id_vars: Vec, - value_vars: Vec, + on: Vec, + index: Vec, value_name: Option, variable_name: Option, streamable: bool, ) -> Self { let args = UnpivotArgs { - index: strings_to_smartstrings(id_vars), - on: strings_to_smartstrings(value_vars), + index: strings_to_smartstrings(index), + on: strings_to_smartstrings(on), value_name: value_name.map(|s| s.into()), variable_name: variable_name.map(|s| s.into()), streamable,