Skip to content

Commit 45f48f4

Browse files
authored
docs: Note that maintains_order has no effect (but keep around for backwards-compatibility) (#1643)
1 parent 9f09ea0 commit 45f48f4

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

narwhals/_pandas_like/dataframe.py

-1
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,6 @@ def pivot(
836836
index: str | list[str] | None,
837837
values: str | list[str] | None,
838838
aggregate_function: Any | None,
839-
maintain_order: bool,
840839
sort_columns: bool,
841840
separator: str = "_",
842841
) -> Self:

narwhals/_polars/dataframe.py

-2
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,6 @@ def pivot(
314314
"min", "max", "first", "last", "sum", "mean", "median", "len"
315315
]
316316
| None,
317-
maintain_order: bool,
318317
sort_columns: bool,
319318
separator: str,
320319
) -> Self:
@@ -326,7 +325,6 @@ def pivot(
326325
index=index,
327326
values=values,
328327
aggregate_function=aggregate_function,
329-
maintain_order=maintain_order,
330328
sort_columns=sort_columns,
331329
separator=separator,
332330
)

narwhals/dataframe.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
from typing import Sequence
1212
from typing import TypeVar
1313
from typing import overload
14+
from warnings import warn
1415

1516
from narwhals.dependencies import get_polars
1617
from narwhals.dependencies import is_numpy_array
1718
from narwhals.schema import Schema
1819
from narwhals.translate import to_native
20+
from narwhals.utils import find_stacklevel
1921
from narwhals.utils import flatten
2022
from narwhals.utils import is_sequence_but_not_str
2123
from narwhals.utils import parse_version
@@ -2865,7 +2867,7 @@ def pivot(
28652867
"min", "max", "first", "last", "sum", "mean", "median", "len"
28662868
]
28672869
| None = None,
2868-
maintain_order: bool = True,
2870+
maintain_order: bool | None = None,
28692871
sort_columns: bool = False,
28702872
separator: str = "_",
28712873
) -> Self:
@@ -2881,11 +2883,12 @@ def pivot(
28812883
specified on `on` and `index` will be used. At least one of `index` and
28822884
`values` must be specified.
28832885
aggregate_function: Choose from:
2886+
28842887
- None: no aggregation takes place, will raise error if multiple values
28852888
are in group.
28862889
- A predefined aggregate function string, one of
28872890
{'min', 'max', 'first', 'last', 'sum', 'mean', 'median', 'len'}
2888-
maintain_order: Sort the grouped keys so that the output order is predictable.
2891+
maintain_order: Has no effect and is kept around only for backwards-compatibility.
28892892
sort_columns: Sort the transposed columns by name. Default is by order of
28902893
discovery.
28912894
separator: Used as separator/delimiter in generated column names in case of
@@ -2933,14 +2936,19 @@ def pivot(
29332936
if values is None and index is None:
29342937
msg = "At least one of `values` and `index` must be passed"
29352938
raise ValueError(msg)
2939+
if maintain_order is not None:
2940+
msg = (
2941+
"`maintain_order` has no effect and is only kept around for backwards-compatibility. "
2942+
"You can safely remove this argument."
2943+
)
2944+
warn(message=msg, category=UserWarning, stacklevel=find_stacklevel())
29362945

29372946
return self._from_compliant_dataframe(
29382947
self._compliant_frame.pivot(
29392948
on=on,
29402949
index=index,
29412950
values=values,
29422951
aggregate_function=aggregate_function,
2943-
maintain_order=maintain_order,
29442952
sort_columns=sort_columns,
29452953
separator=separator,
29462954
)

tests/frame/pivot_test.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ def test_pivot_no_index(
266266
# not implemented
267267
request.applymarker(pytest.mark.xfail)
268268
df = nw.from_native(constructor_eager(data_no_dups), eager_only=True)
269-
result = df.pivot(on="col", values="foo").sort("ix", "bar")
269+
with pytest.warns(UserWarning, match="has no effect"):
270+
result = df.pivot(on="col", values="foo", maintain_order=True).sort("ix", "bar")
270271
expected = {
271272
"ix": [1, 1, 2, 2],
272273
"bar": ["x", "y", "w", "z"],

0 commit comments

Comments
 (0)