Skip to content

Commit

Permalink
docs: Note that maintains_order has no effect (but keep around for …
Browse files Browse the repository at this point in the history
…backwards-compatibility) (#1643)
  • Loading branch information
MarcoGorelli authored Dec 22, 2024
1 parent 9f09ea0 commit 45f48f4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
1 change: 0 additions & 1 deletion narwhals/_pandas_like/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,6 @@ def pivot(
index: str | list[str] | None,
values: str | list[str] | None,
aggregate_function: Any | None,
maintain_order: bool,
sort_columns: bool,
separator: str = "_",
) -> Self:
Expand Down
2 changes: 0 additions & 2 deletions narwhals/_polars/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ def pivot(
"min", "max", "first", "last", "sum", "mean", "median", "len"
]
| None,
maintain_order: bool,
sort_columns: bool,
separator: str,
) -> Self:
Expand All @@ -326,7 +325,6 @@ def pivot(
index=index,
values=values,
aggregate_function=aggregate_function,
maintain_order=maintain_order,
sort_columns=sort_columns,
separator=separator,
)
Expand Down
14 changes: 11 additions & 3 deletions narwhals/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
from typing import Sequence
from typing import TypeVar
from typing import overload
from warnings import warn

from narwhals.dependencies import get_polars
from narwhals.dependencies import is_numpy_array
from narwhals.schema import Schema
from narwhals.translate import to_native
from narwhals.utils import find_stacklevel
from narwhals.utils import flatten
from narwhals.utils import is_sequence_but_not_str
from narwhals.utils import parse_version
Expand Down Expand Up @@ -2865,7 +2867,7 @@ def pivot(
"min", "max", "first", "last", "sum", "mean", "median", "len"
]
| None = None,
maintain_order: bool = True,
maintain_order: bool | None = None,
sort_columns: bool = False,
separator: str = "_",
) -> Self:
Expand All @@ -2881,11 +2883,12 @@ def pivot(
specified on `on` and `index` will be used. At least one of `index` and
`values` must be specified.
aggregate_function: Choose from:
- None: no aggregation takes place, will raise error if multiple values
are in group.
- A predefined aggregate function string, one of
{'min', 'max', 'first', 'last', 'sum', 'mean', 'median', 'len'}
maintain_order: Sort the grouped keys so that the output order is predictable.
maintain_order: Has no effect and is kept around only for backwards-compatibility.
sort_columns: Sort the transposed columns by name. Default is by order of
discovery.
separator: Used as separator/delimiter in generated column names in case of
Expand Down Expand Up @@ -2933,14 +2936,19 @@ def pivot(
if values is None and index is None:
msg = "At least one of `values` and `index` must be passed"
raise ValueError(msg)
if maintain_order is not None:
msg = (
"`maintain_order` has no effect and is only kept around for backwards-compatibility. "
"You can safely remove this argument."
)
warn(message=msg, category=UserWarning, stacklevel=find_stacklevel())

return self._from_compliant_dataframe(
self._compliant_frame.pivot(
on=on,
index=index,
values=values,
aggregate_function=aggregate_function,
maintain_order=maintain_order,
sort_columns=sort_columns,
separator=separator,
)
Expand Down
3 changes: 2 additions & 1 deletion tests/frame/pivot_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ def test_pivot_no_index(
# not implemented
request.applymarker(pytest.mark.xfail)
df = nw.from_native(constructor_eager(data_no_dups), eager_only=True)
result = df.pivot(on="col", values="foo").sort("ix", "bar")
with pytest.warns(UserWarning, match="has no effect"):
result = df.pivot(on="col", values="foo", maintain_order=True).sort("ix", "bar")
expected = {
"ix": [1, 1, 2, 2],
"bar": ["x", "y", "w", "z"],
Expand Down

0 comments on commit 45f48f4

Please sign in to comment.