From f90896e16b1927a13114c0095fc0678a8f3ca588 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Sat, 15 May 2021 20:53:14 +0100 Subject: [PATCH 1/2] deprecate default args as positional in set_index --- doc/source/whatsnew/v1.3.0.rst | 1 + pandas/core/frame.py | 2 ++ pandas/tests/frame/methods/test_set_index.py | 10 ++++++++++ 3 files changed, 13 insertions(+) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 622029adf357f..6594f3956898c 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 ``keys``) as positional in :meth:`DataFrame.set_index` (:issue:`41485`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 2941b6ac01904..18247b8bac95e 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, ) @@ -5301,6 +5302,7 @@ def shift( periods=periods, freq=freq, axis=axis, fill_value=fill_value ) + @deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self", "keys"]) def set_index( self, keys, diff --git a/pandas/tests/frame/methods/test_set_index.py b/pandas/tests/frame/methods/test_set_index.py index 62dc400f8de9f..468a043e7da3d 100644 --- a/pandas/tests/frame/methods/test_set_index.py +++ b/pandas/tests/frame/methods/test_set_index.py @@ -705,3 +705,13 @@ def test_set_index_periodindex(self): tm.assert_index_equal(df.index, idx1) df = df.set_index(idx2) tm.assert_index_equal(df.index, idx2) + + def test_drop_pos_args_deprecation(self): + # 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_index except for " + r"the arguments 'self' and 'keys' will be keyword-only" + ) + with tm.assert_produces_warning(FutureWarning, match=msg): + df.set_index("a", True) From f4da1843e10905b3a067cb87fbd63b6fe2eea76f Mon Sep 17 00:00:00 2001 From: MarcoGorelli Date: Wed, 19 May 2021 21:02:55 +0100 Subject: [PATCH 2/2] assert result in test --- doc/source/whatsnew/v1.3.0.rst | 1 - pandas/core/frame.py | 2 +- pandas/tests/frame/methods/test_set_index.py | 8 +++++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index ec8ad0d6f6402..2e785488d72f6 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -650,7 +650,6 @@ Deprecations - Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`) - Deprecated passing arguments as positional in (:issue:`41485`) : - :meth:`DataFrame.interpolate` (other than ``"method"``) and :meth:`Series.interpolate` - - :meth:`DataFrame.sort_values` (other than ``"by"``) and :meth:`Series.sort_values` - :meth:`DataFrame.set_index` (other than ``"keys"``) .. --------------------------------------------------------------------------- diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 5dc6296b25a87..25b94f095f10f 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5352,7 +5352,7 @@ def shift( periods=periods, freq=freq, axis=axis, fill_value=fill_value ) - @deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self", "keys"]) + @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "keys"]) def set_index( self, keys, diff --git a/pandas/tests/frame/methods/test_set_index.py b/pandas/tests/frame/methods/test_set_index.py index 40ccc4d12bb0a..1b3db10ec6158 100644 --- a/pandas/tests/frame/methods/test_set_index.py +++ b/pandas/tests/frame/methods/test_set_index.py @@ -709,8 +709,10 @@ def test_drop_pos_args_deprecation(self): # 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_index except for " - r"the arguments 'self' and 'keys' will be keyword-only" + r"In a future version of pandas all arguments of DataFrame\.set_index " + r"except for the argument 'keys' will be keyword-only" ) with tm.assert_produces_warning(FutureWarning, match=msg): - df.set_index("a", True) + result = df.set_index("a", True) + expected = DataFrame(index=Index([1, 2, 3], name="a")) + tm.assert_frame_equal(result, expected)