From 354403dc90d2616ce1206a5f2508adcb8897ba09 Mon Sep 17 00:00:00 2001 From: dare Date: Mon, 24 May 2021 22:12:13 +0100 Subject: [PATCH 1/3] made changes for issue 41485, set_codes function --- pandas/core/indexes/multi.py | 2 ++ pandas/tests/indexes/multi/test_get_set.py | 29 ++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 1a3719233a1da..6cbeddd53a29e 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -41,6 +41,7 @@ from pandas.util._decorators import ( Appender, cache_readonly, + deprecate_nonkeyword_arguments, doc, ) @@ -994,6 +995,7 @@ def _set_codes( self._reset_cache() + @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "codes"]) def set_codes(self, codes, level=None, inplace=None, verify_integrity: bool = True): """ Set new codes on MultiIndex. Defaults to returning new index. diff --git a/pandas/tests/indexes/multi/test_get_set.py b/pandas/tests/indexes/multi/test_get_set.py index 0c561395788ad..dd88a35d0bd5e 100644 --- a/pandas/tests/indexes/multi/test_get_set.py +++ b/pandas/tests/indexes/multi/test_get_set.py @@ -405,3 +405,32 @@ def test_set_levels_inplace_deprecated(idx, inplace): with tm.assert_produces_warning(FutureWarning): idx.set_levels(levels=new_level, level=1, inplace=inplace) + + +def test_set_codes_pos_args_depreciation(): + # https://github.com/pandas-dev/pandas/issues/41485 + idx = MultiIndex.from_tuples( + [ + (1, "one"), + (1, "two"), + (2, "one"), + (2, "two"), + ], + names=["foo", "bar"] + ) + msg = ( + r"In a future version of pandas all arguments of MultiIndex.set_codes except" + r"for the argument 'codes' will be keyword-only" + ) + with tm.assert_produces_warning(FutureWarning, match=msg): + result = idx.set_codes([[1, 0, 1, 0], [0, 0, 1, 1]]) + expected = MultiIndex.from_tuples( + [ + (2, "one"), + (1, "one"), + (2, "two"), + (1, "two"), + ], + names=["foo", "bar"] + ) + tm.assert_index_equal(result, expected) \ No newline at end of file From fce77c2fc61293fbc3cb9f9fd7b1b4cdb51b2587 Mon Sep 17 00:00:00 2001 From: dare Date: Mon, 24 May 2021 22:15:34 +0100 Subject: [PATCH 2/3] made changes for issue 41485, set_codes function --- doc/source/whatsnew/v1.3.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 258e391b9220c..415cf8582a1f6 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -677,6 +677,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 as positional (except for ``"codes"``) in :meth:`MultiIndex.codes` (:issue:`41485`) - 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`) From 13793a27753e21e29260abfd26c7d120a3a4b87d Mon Sep 17 00:00:00 2001 From: dare Date: Wed, 26 May 2021 08:47:43 +0100 Subject: [PATCH 3/3] corrected stack level error --- pandas/core/indexes/multi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 6cbeddd53a29e..2475c2a57b6d5 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -1063,7 +1063,7 @@ def set_codes(self, codes, level=None, inplace=None, verify_integrity: bool = Tr warnings.warn( "inplace is deprecated and will be removed in a future version.", FutureWarning, - stacklevel=2, + stacklevel=3, ) else: inplace = False