From 0d494527617019ea8fe3e296f685308a70b0e9db Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Sat, 15 May 2021 19:15:28 +0100 Subject: [PATCH 1/4] deprecate default args as positional in set_axis --- pandas/core/frame.py | 2 ++ pandas/core/generic.py | 2 +- pandas/core/series.py | 2 ++ pandas/tests/frame/methods/test_set_axis.py | 11 +++++++++++ pandas/tests/reshape/concat/test_categorical.py | 4 ++-- 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 2941b6ac01904..e58642741c39f 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -77,6 +77,7 @@ Appender, Substitution, deprecate_kwarg, + deprecate_nonkeyword_arguments, doc, rewrite_axis_style_signature, ) @@ -4675,6 +4676,7 @@ def set_axis( ) -> DataFrame | None: ... + @deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self", "labels"]) @Appender( """ Examples diff --git a/pandas/core/generic.py b/pandas/core/generic.py index a09cc0a6324c0..d17e6c6ff2c7f 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -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 diff --git a/pandas/core/series.py b/pandas/core/series.py index c8e9898f9462a..b6eaa23e731bc 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -51,6 +51,7 @@ from pandas.util._decorators import ( Appender, Substitution, + deprecate_nonkeyword_arguments, doc, ) from pandas.util._validators import ( @@ -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 diff --git a/pandas/tests/frame/methods/test_set_axis.py b/pandas/tests/frame/methods/test_set_axis.py index ee538e1d9d9ac..fedfba2c365ed 100644 --- a/pandas/tests/frame/methods/test_set_axis.py +++ b/pandas/tests/frame/methods/test_set_axis.py @@ -98,3 +98,14 @@ 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) 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) From 1082e62dd4ba39619a01a7172cb6fc1c5b072e0d Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Sat, 15 May 2021 19:16:47 +0100 Subject: [PATCH 2/4] deprecate default args as positional in set_axis --- doc/source/whatsnew/v1.3.0.rst | 1 + pandas/tests/frame/methods/test_set_axis.py | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 622029adf357f..f0e0435610392 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -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`) .. --------------------------------------------------------------------------- diff --git a/pandas/tests/frame/methods/test_set_axis.py b/pandas/tests/frame/methods/test_set_axis.py index fedfba2c365ed..f71802bcda7c7 100644 --- a/pandas/tests/frame/methods/test_set_axis.py +++ b/pandas/tests/frame/methods/test_set_axis.py @@ -109,3 +109,11 @@ def test_nonkeyword_arguments_deprecation_warning(): ) with tm.assert_produces_warning(FutureWarning, match=msg): df.set_axis([1, 2, 4], False) + + ser = Series([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): + ser.set_axis([1, 2, 4], False) From 279ca64727e45e0949aa907f3b7ba04bef0b5fb0 Mon Sep 17 00:00:00 2001 From: MarcoGorelli Date: Wed, 19 May 2021 21:13:28 +0100 Subject: [PATCH 3/4] assertion about test --- pandas/core/frame.py | 2 +- pandas/core/series.py | 2 +- pandas/tests/frame/methods/test_set_axis.py | 16 ++++++++++------ 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 84560090fe81e..c3161d8ad36e0 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4726,7 +4726,7 @@ def set_axis( ) -> DataFrame | None: ... - @deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self", "labels"]) + @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"]) @Appender( """ Examples diff --git a/pandas/core/series.py b/pandas/core/series.py index c312cbc6f1f36..6ecc509f6ab77 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -4447,7 +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"]) + @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 f71802bcda7c7..3284243ddac48 100644 --- a/pandas/tests/frame/methods/test_set_axis.py +++ b/pandas/tests/frame/methods/test_set_axis.py @@ -104,16 +104,20 @@ 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" + 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): - df.set_axis([1, 2, 4], False) + 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"Starting with Pandas version 2\.0 all arguments of set_axis except for the " - r"arguments 'self' and 'labels' will be keyword-only" + 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): - ser.set_axis([1, 2, 4], False) + result = ser.set_axis([1, 2, 4], 0) + expected = Series([1, 2, 3], index=[1, 2, 4]) + tm.assert_series_equal(result, expected) From 5fa5f4d92fd74c5474711fe538e16bb75a65a58f Mon Sep 17 00:00:00 2001 From: Marco Edward Gorelli Date: Thu, 27 May 2021 11:40:18 +0100 Subject: [PATCH 4/4] Update v1.3.0.rst --- doc/source/whatsnew/v1.3.0.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 28da187b69d44..5436685c1f14f 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -691,11 +691,12 @@ Deprecations - Deprecated passing arguments as positional in :meth:`DataFrame.drop_duplicates` (except for ``subset``), :meth:`Series.drop_duplicates`, :meth:`Index.drop_duplicates` and :meth:`MultiIndex.drop_duplicates`(: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`) -- Deprecatedconstruction 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 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