Skip to content

Commit

Permalink
on positional, values and index keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Jun 21, 2024
1 parent 9de041c commit 3ba6382
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 31 deletions.
8 changes: 4 additions & 4 deletions crates/polars-core/src/frame/explode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,11 @@ impl DataFrame {
I: IntoVec<SmartString>,
J: IntoVec<SmartString>,
{
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()
})
}
Expand Down
6 changes: 3 additions & 3 deletions crates/polars-lazy/src/frame/pivot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ impl PhysicalAggExpr for PivotExpr {

pub fn pivot<I0, I1, I2, S0, S1, S2>(
df: &DataFrame,
index: I0,
on: I1,
index: I0,
values: Option<I2>,
sort_columns: bool,
agg_expr: Option<Expr>,
Expand All @@ -58,8 +58,8 @@ where

pub fn pivot_stable<I0, I1, I2, S0, S1, S2>(
df: &DataFrame,
index: I0,
on: I1,
on: I0,
index: I1,
values: Option<I2>,
sort_columns: bool,
agg_expr: Option<Expr>,
Expand Down
8 changes: 4 additions & 4 deletions crates/polars-ops/src/frame/pivot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<I0, I1, I2, S0, S1, S2>(
pivot_df: &DataFrame,
index: I0,
on: I1,
on: I0,
index: I1,
values: Option<I2>,
sort_columns: bool,
agg_fn: Option<PivotAgg>,
Expand Down Expand Up @@ -127,8 +127,8 @@ where
/// If you have a relatively large table, consider using a group_by over a pivot.
pub fn pivot_stable<I0, I1, I2, S0, S1, S2>(
pivot_df: &DataFrame,
index: I0,
on: I1,
on: I0,
index: I1,
values: Option<I2>,
sort_columns: bool,
agg_fn: Option<PivotAgg>,
Expand Down
22 changes: 11 additions & 11 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -7689,8 +7689,8 @@ def pivot(
... }
... )
>>> df.pivot(
... "col2",
... index="col1",
... on="col2",
... values="col3",
... aggregate_function=pl.element().tanh().mean(),
... )
Expand Down Expand Up @@ -7738,8 +7738,8 @@ def pivot(
... }
... )
>>> df.pivot(
... "col",
... index="ix",
... on="col",
... values=["foo", "bar"],
... aggregate_function="sum",
... separator="/",
Expand Down Expand Up @@ -7793,8 +7793,8 @@ def pivot(

return self._from_pydf(
self._df.pivot_expr(
index,
on,
index,
values,
maintain_order,
sort_columns,
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion py-polars/polars/lazyframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions py-polars/src/dataframe/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,8 @@ impl PyDataFrame {

pub fn unpivot(
&self,
index: Vec<PyBackedStr>,
on: Vec<PyBackedStr>,
index: Vec<PyBackedStr>,
value_name: Option<&str>,
variable_name: Option<&str>,
) -> PyResult<Self> {
Expand All @@ -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<String>,
on: Vec<String>,
index: Vec<String>,
values: Option<Vec<String>>,
maintain_order: bool,
sort_columns: bool,
Expand Down
10 changes: 5 additions & 5 deletions py-polars/src/lazyframe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
value_vars: Vec<String>,
on: Vec<String>,
index: Vec<String>,
value_name: Option<String>,
variable_name: Option<String>,
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,
Expand Down

0 comments on commit 3ba6382

Please sign in to comment.