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

Merged
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ Deprecations
- Deprecated setting :attr:`Categorical._codes`, create a new :class:`Categorical` with the desired codes instead (:issue:`40606`)
- Deprecated behavior of :meth:`DatetimeIndex.union` with mixed timezones; in a future version both will be cast to UTC instead of object dtype (:issue:`39328`)
- Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`)
- Deprecated passing arguments (apart from ``labels``) as positional in :meth:`DataFrame.set_axis` and :meth:`Series.set_axis` (:issue:`41485`)

.. ---------------------------------------------------------------------------

Expand Down
2 changes: 2 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
Appender,
Substitution,
deprecate_kwarg,
deprecate_nonkeyword_arguments,
doc,
rewrite_axis_style_signature,
)
Expand Down Expand Up @@ -4675,6 +4676,7 @@ def set_axis(
) -> DataFrame | None:
...

@deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self", "labels"])
@Appender(
"""
Examples
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9409,7 +9409,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
2 changes: 2 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
from pandas.util._decorators import (
Appender,
Substitution,
deprecate_nonkeyword_arguments,
doc,
)
from pandas.util._validators import (
Expand Down Expand Up @@ -4446,6 +4447,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="2.0", allowed_args=["self", "labels"])
@Appender(
"""
Examples
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/frame/methods/test_set_axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,22 @@ 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"Starting with Pandas version 2\.0 all arguments of set_axis except for the "
r"arguments 'self' and 'labels' will be keyword-only"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
df.set_axis([1, 2, 4], False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
df.set_axis([1, 2, 4], False)
df.set_axis([1, 2, 4], 0)

? (since it's for the axis keyword)

Also the same comment as in one of the other PRs, I would still assert the actual result to ensure we keep this working correctly until it's actually removed


ser = Series([1, 2, 3])
msg = (
MarcoGorelli marked this conversation as resolved.
Show resolved Hide resolved
r"Starting with Pandas version 2\.0 all arguments of set_axis except for the "
r"arguments 'self' and 'labels' will be keyword-only"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
ser.set_axis([1, 2, 4], False)
4 changes: 2 additions & 2 deletions pandas/tests/reshape/concat/test_categorical.py
Original file line number Diff line number Diff line change
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