diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 24cd41383e9d7..5436685c1f14f 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -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 diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 75d2f4c591053..d2108fb901ca2 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4703,6 +4703,7 @@ def set_axis( ) -> DataFrame | None: ... + @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"]) @Appender( """ Examples diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 8bbfa1f1db680..3e6642d3b1a72 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -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 diff --git a/pandas/core/series.py b/pandas/core/series.py index 686e6966d8e24..5767d7bd1cd44 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -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 diff --git a/pandas/tests/frame/methods/test_set_axis.py b/pandas/tests/frame/methods/test_set_axis.py index ee538e1d9d9ac..3284243ddac48 100644 --- a/pandas/tests/frame/methods/test_set_axis.py +++ b/pandas/tests/frame/methods/test_set_axis.py @@ -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) diff --git a/pandas/tests/reshape/concat/test_categorical.py b/pandas/tests/reshape/concat/test_categorical.py index a81085e083199..d8b5f19c6a745 100644 --- a/pandas/tests/reshape/concat/test_categorical.py +++ b/pandas/tests/reshape/concat/test_categorical.py @@ -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)