Skip to content

Commit

Permalink
Deprecate nonkeyword args set axis (#41491)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli authored May 27, 2021
1 parent 22252c9 commit a25c2f1
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 3 deletions.
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -692,10 +692,12 @@ 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`)
- Deprecated passing arguments as positional in :meth:`DataFrame.drop` (other than ``"labels"``) and :meth:`Series.drop` (: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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 = (
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
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

0 comments on commit a25c2f1

Please sign in to comment.