diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index d357e4a633347..532af3c65bc1e 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -677,6 +677,7 @@ Deprecations - Deprecated special treatment of lists with first element a Categorical in the :class:`DataFrame` constructor; pass as ``pd.DataFrame({col: categorical, ...})`` instead (:issue:`38845`) - Deprecated passing arguments as positional (except for ``"method"``) in :meth:`DataFrame.interpolate` and :meth:`Series.interpolate` (:issue:`41485`) - 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`) .. _whatsnew_130.deprecations.nuisance_columns: diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 18ee1ad9bcd96..67c405c792c34 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5619,6 +5619,7 @@ def reset_index( ) -> DataFrame | None: ... + @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "level"]) def reset_index( self, level: Hashable | Sequence[Hashable] | None = None, diff --git a/pandas/core/series.py b/pandas/core/series.py index d8b7876028839..506943ce00860 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1308,6 +1308,7 @@ def repeat(self, repeats, axis=None) -> Series: self, method="repeat" ) + @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "level"]) def reset_index(self, level=None, drop=False, name=None, inplace=False): """ Generate a new DataFrame or Series with the index reset. diff --git a/pandas/tests/frame/methods/test_reset_index.py b/pandas/tests/frame/methods/test_reset_index.py index f50b2c179ad9a..91f354fecca63 100644 --- a/pandas/tests/frame/methods/test_reset_index.py +++ b/pandas/tests/frame/methods/test_reset_index.py @@ -671,3 +671,16 @@ def test_reset_index_multiindex_nat(): index=pd.DatetimeIndex(["2015-07-01", "2015-07-02", "NaT"], name="tstamp"), ) tm.assert_frame_equal(result, expected) + + +def test_drop_pos_args_deprecation(): + # https://github.com/pandas-dev/pandas/issues/41485 + df = DataFrame({"a": [1, 2, 3]}).set_index("a") + msg = ( + r"In a future version of pandas all arguments of DataFrame\.reset_index " + r"except for the argument 'level' will be keyword-only" + ) + with tm.assert_produces_warning(FutureWarning, match=msg): + result = df.reset_index("a", False) + expected = DataFrame({"a": [1, 2, 3]}) + tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/series/methods/test_reset_index.py b/pandas/tests/series/methods/test_reset_index.py index 70b9c9c9dc7d7..b159317bf813b 100644 --- a/pandas/tests/series/methods/test_reset_index.py +++ b/pandas/tests/series/methods/test_reset_index.py @@ -148,6 +148,18 @@ def test_reset_index_with_drop(self, series_with_multilevel_index): assert isinstance(deleveled, Series) assert deleveled.index.name == ser.index.name + def test_drop_pos_args_deprecation(self): + # https://github.com/pandas-dev/pandas/issues/41485 + ser = Series([1, 2, 3], index=Index([1, 2, 3], name="a")) + msg = ( + r"In a future version of pandas all arguments of Series\.reset_index " + r"except for the argument 'level' will be keyword-only" + ) + with tm.assert_produces_warning(FutureWarning, match=msg): + result = ser.reset_index("a", False) + expected = DataFrame({"a": [1, 2, 3], 0: [1, 2, 3]}) + tm.assert_frame_equal(result, expected) + @pytest.mark.parametrize( "array, dtype",