Skip to content

Commit 1204aca

Browse files
MarcoGorelliTLouf
authored andcommitted
Deprecate nonkeyword args set axis (pandas-dev#41491)
1 parent d884071 commit 1204aca

File tree

6 files changed

+30
-3
lines changed

6 files changed

+30
-3
lines changed

doc/source/whatsnew/v1.3.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -692,10 +692,12 @@ Deprecations
692692
- Deprecated passing arguments (apart from ``value``) as positional in :meth:`DataFrame.fillna` and :meth:`Series.fillna` (:issue:`41485`)
693693
- Deprecated passing arguments as positional in :meth:`DataFrame.reset_index` (other than ``"level"``) and :meth:`Series.reset_index` (:issue:`41485`)
694694
- Deprecated construction of :class:`Series` or :class:`DataFrame` with ``DatetimeTZDtype`` data and ``datetime64[ns]`` dtype. Use ``Series(data).dt.tz_localize(None)`` instead (:issue:`41555`,:issue:`33401`)
695+
- Deprecated passing arguments as positional in :meth:`DataFrame.set_axis` and :meth:`Series.set_axis` (other than ``"labels"``) (:issue:`41485`)
695696
- Deprecated passing arguments as positional in :meth:`DataFrame.where` and :meth:`Series.where` (other than ``"cond"`` and ``"other"``) (:issue:`41485`)
696697
- Deprecated passing arguments as positional in :meth:`DataFrame.drop` (other than ``"labels"``) and :meth:`Series.drop` (:issue:`41485`)
697698
-
698699

700+
699701
.. _whatsnew_130.deprecations.nuisance_columns:
700702

701703
Deprecated Dropping Nuisance Columns in DataFrame Reductions and DataFrameGroupBy Operations

pandas/core/frame.py

+1
Original file line numberDiff line numberDiff line change
@@ -4703,6 +4703,7 @@ def set_axis(
47034703
) -> DataFrame | None:
47044704
...
47054705

4706+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"])
47064707
@Appender(
47074708
"""
47084709
Examples

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9221,7 +9221,7 @@ def shift(
92219221
else:
92229222
new_ax = index.shift(periods, freq)
92239223

9224-
result = self.set_axis(new_ax, axis)
9224+
result = self.set_axis(new_ax, axis=axis)
92259225
return result.__finalize__(self, method="shift")
92269226

92279227
@final

pandas/core/series.py

+1
Original file line numberDiff line numberDiff line change
@@ -4483,6 +4483,7 @@ def set_axis(self, labels, *, inplace: Literal[True]) -> None:
44834483
def set_axis(self, labels, axis: Axis = ..., inplace: bool = ...) -> Series | None:
44844484
...
44854485

4486+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"])
44864487
@Appender(
44874488
"""
44884489
Examples

pandas/tests/frame/methods/test_set_axis.py

+23
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,26 @@ class TestSeriesSetAxis(SharedSetAxisTests):
9898
def obj(self):
9999
ser = Series(np.arange(4), index=[1, 3, 5, 7], dtype="int64")
100100
return ser
101+
102+
103+
def test_nonkeyword_arguments_deprecation_warning():
104+
# https://github.com/pandas-dev/pandas/issues/41485
105+
df = DataFrame({"a": [1, 2, 3]})
106+
msg = (
107+
r"In a future version of pandas all arguments of DataFrame\.set_axis "
108+
r"except for the argument 'labels' will be keyword-only"
109+
)
110+
with tm.assert_produces_warning(FutureWarning, match=msg):
111+
result = df.set_axis([1, 2, 4], 0)
112+
expected = DataFrame({"a": [1, 2, 3]}, index=[1, 2, 4])
113+
tm.assert_frame_equal(result, expected)
114+
115+
ser = Series([1, 2, 3])
116+
msg = (
117+
r"In a future version of pandas all arguments of Series\.set_axis "
118+
r"except for the argument 'labels' will be keyword-only"
119+
)
120+
with tm.assert_produces_warning(FutureWarning, match=msg):
121+
result = ser.set_axis([1, 2, 4], 0)
122+
expected = Series([1, 2, 3], index=[1, 2, 4])
123+
tm.assert_series_equal(result, expected)

pandas/tests/reshape/concat/test_categorical.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ def test_categorical_index_preserver(self):
148148
result = pd.concat([df2, df3])
149149
expected = pd.concat(
150150
[
151-
df2.set_axis(df2.index.astype(object), 0),
152-
df3.set_axis(df3.index.astype(object), 0),
151+
df2.set_axis(df2.index.astype(object), axis=0),
152+
df3.set_axis(df3.index.astype(object), axis=0),
153153
]
154154
)
155155
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)