Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate nonkeyword args set axis #41491

2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.3.0.rst
Expand Up @@ -692,9 +692,11 @@ Deprecations
- Deprecated passing arguments (apart from ``value``) as positional in :meth:`DataFrame.fillna` and :meth:`Series.fillna` (:issue:`41485`)
- Deprecated passing arguments as positional in :meth:`DataFrame.reset_index` (other than ``"level"``) and :meth:`Series.reset_index` (:issue:`41485`)
- 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`)
- Deprecated passing arguments as positional in :meth:`DataFrame.set_axis` and :meth:`Series.set_axis` (other than ``"labels"``) (:issue:`41485`)
- Deprecated passing arguments as positional in :meth:`DataFrame.where` and :meth:`Series.where` (other than ``"cond"`` and ``"other"``) (:issue:`41485`)
-


.. _whatsnew_130.deprecations.nuisance_columns:

Deprecated Dropping Nuisance Columns in DataFrame Reductions and DataFrameGroupBy Operations
Expand Down
1 change: 1 addition & 0 deletions pandas/core/frame.py
Expand Up @@ -4703,6 +4703,7 @@ def set_axis(
) -> DataFrame | None:
...

@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"])
@Appender(
"""
Examples
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Expand Up @@ -9221,7 +9221,7 @@ def shift(
else:
new_ax = index.shift(periods, freq)

result = self.set_axis(new_ax, axis)
result = self.set_axis(new_ax, axis=axis)
return result.__finalize__(self, method="shift")

@final
Expand Down
1 change: 1 addition & 0 deletions pandas/core/series.py
Expand Up @@ -4483,6 +4483,7 @@ def set_axis(self, labels, *, inplace: Literal[True]) -> None:
def set_axis(self, labels, axis: Axis = ..., inplace: bool = ...) -> Series | None:
...

@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"])
@Appender(
"""
Examples
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/frame/methods/test_set_axis.py
Expand Up @@ -98,3 +98,26 @@ class TestSeriesSetAxis(SharedSetAxisTests):
def obj(self):
ser = Series(np.arange(4), index=[1, 3, 5, 7], dtype="int64")
return ser


def test_nonkeyword_arguments_deprecation_warning():
# https://github.com/pandas-dev/pandas/issues/41485
df = DataFrame({"a": [1, 2, 3]})
msg = (
r"In a future version of pandas all arguments of DataFrame\.set_axis "
r"except for the argument 'labels' will be keyword-only"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
result = df.set_axis([1, 2, 4], 0)
expected = DataFrame({"a": [1, 2, 3]}, index=[1, 2, 4])
tm.assert_frame_equal(result, expected)

ser = Series([1, 2, 3])
msg = (
MarcoGorelli marked this conversation as resolved.
Show resolved Hide resolved
r"In a future version of pandas all arguments of Series\.set_axis "
r"except for the argument 'labels' will be keyword-only"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
result = ser.set_axis([1, 2, 4], 0)
expected = Series([1, 2, 3], index=[1, 2, 4])
tm.assert_series_equal(result, expected)
4 changes: 2 additions & 2 deletions pandas/tests/reshape/concat/test_categorical.py
Expand Up @@ -148,8 +148,8 @@ def test_categorical_index_preserver(self):
result = pd.concat([df2, df3])
expected = pd.concat(
[
df2.set_axis(df2.index.astype(object), 0),
df3.set_axis(df3.index.astype(object), 0),
df2.set_axis(df2.index.astype(object), axis=0),
df3.set_axis(df3.index.astype(object), axis=0),
]
)
tm.assert_frame_equal(result, expected)
Expand Down