Skip to content

Commit 9115f68

Browse files
Backport PR #48301 on branch 1.5.x (DEPR: Deprecate positional arguments in pivot) (#48326)
Backport PR #48301: DEPR: Deprecate positional arguments in pivot Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 1196b8d commit 9115f68

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,7 @@ Other Deprecations
911911
- Deprecated :attr:`Timedelta.freq` and :attr:`Timedelta.is_populated` (:issue:`46430`)
912912
- Deprecated :attr:`Timedelta.delta` (:issue:`46476`)
913913
- Deprecated passing arguments as positional in :meth:`DataFrame.any` and :meth:`Series.any` (:issue:`44802`)
914+
- Deprecated passing positional arguments to :meth:`DataFrame.pivot` and :func:`pivot` except ``data`` (:issue:`30228`)
914915
- Deprecated the methods :meth:`DataFrame.mad`, :meth:`Series.mad`, and the corresponding groupby methods (:issue:`11787`)
915916
- Deprecated positional arguments to :meth:`Index.join` except for ``other``, use keyword-only arguments instead of positional arguments (:issue:`46518`)
916917
- Deprecated positional arguments to :meth:`StringMethods.rsplit` and :meth:`StringMethods.split` except for ``pat``, use keyword-only arguments instead of positional arguments (:issue:`47423`)

pandas/core/frame.py

+1
Original file line numberDiff line numberDiff line change
@@ -8579,6 +8579,7 @@ def groupby(
85798579

85808580
@Substitution("")
85818581
@Appender(_shared_docs["pivot"])
8582+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self"])
85828583
def pivot(self, index=None, columns=None, values=None) -> DataFrame:
85838584
from pandas.core.reshape.pivot import pivot
85848585

pandas/core/reshape/pivot.py

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from pandas.util._decorators import (
2020
Appender,
2121
Substitution,
22+
deprecate_nonkeyword_arguments,
2223
)
2324

2425
from pandas.core.dtypes.cast import maybe_downcast_to_dtype
@@ -472,6 +473,7 @@ def _convert_by(by):
472473

473474
@Substitution("\ndata : DataFrame")
474475
@Appender(_shared_docs["pivot"], indents=1)
476+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["data"])
475477
def pivot(
476478
data: DataFrame,
477479
index: IndexLabel | None = None,

pandas/tests/reshape/test_pivot.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -480,9 +480,11 @@ def test_pivot_index_with_nan(self, method):
480480
}
481481
)
482482
if method:
483-
result = df.pivot("a", "b", "c")
483+
with tm.assert_produces_warning(FutureWarning):
484+
result = df.pivot("a", columns="b", values="c")
484485
else:
485-
result = pd.pivot(df, "a", "b", "c")
486+
with tm.assert_produces_warning(FutureWarning):
487+
result = pd.pivot(df, "a", columns="b", values="c")
486488
expected = DataFrame(
487489
[
488490
[nan, nan, 17, nan],
@@ -494,7 +496,7 @@ def test_pivot_index_with_nan(self, method):
494496
columns=Index(["C1", "C2", "C3", "C4"], name="b"),
495497
)
496498
tm.assert_frame_equal(result, expected)
497-
tm.assert_frame_equal(df.pivot("b", "a", "c"), expected.T)
499+
tm.assert_frame_equal(df.pivot(index="b", columns="a", values="c"), expected.T)
498500

499501
@pytest.mark.parametrize("method", [True, False])
500502
def test_pivot_index_with_nan_dates(self, method):
@@ -510,18 +512,18 @@ def test_pivot_index_with_nan_dates(self, method):
510512
df.loc[1, "b"] = df.loc[4, "b"] = np.nan
511513

512514
if method:
513-
pv = df.pivot("a", "b", "c")
515+
pv = df.pivot(index="a", columns="b", values="c")
514516
else:
515-
pv = pd.pivot(df, "a", "b", "c")
517+
pv = pd.pivot(df, index="a", columns="b", values="c")
516518
assert pv.notna().values.sum() == len(df)
517519

518520
for _, row in df.iterrows():
519521
assert pv.loc[row["a"], row["b"]] == row["c"]
520522

521523
if method:
522-
result = df.pivot("b", "a", "c")
524+
result = df.pivot(index="b", columns="a", values="c")
523525
else:
524-
result = pd.pivot(df, "b", "a", "c")
526+
result = pd.pivot(df, index="b", columns="a", values="c")
525527
tm.assert_frame_equal(result, pv.T)
526528

527529
@pytest.mark.filterwarnings("ignore:Timestamp.freq is deprecated:FutureWarning")
@@ -2275,11 +2277,11 @@ def test_pivot_duplicates(self):
22752277
}
22762278
)
22772279
with pytest.raises(ValueError, match="duplicate entries"):
2278-
data.pivot("a", "b", "c")
2280+
data.pivot(index="a", columns="b", values="c")
22792281

22802282
def test_pivot_empty(self):
22812283
df = DataFrame(columns=["a", "b", "c"])
2282-
result = df.pivot("a", "b", "c")
2284+
result = df.pivot(index="a", columns="b", values="c")
22832285
expected = DataFrame()
22842286
tm.assert_frame_equal(result, expected, check_names=False)
22852287

0 commit comments

Comments
 (0)