Skip to content

Commit a56595c

Browse files
committed
DEPR: Deprecate params levels & codes in MultiIndex.copy
1 parent 664c8fa commit a56595c

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

doc/source/whatsnew/v1.2.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ Deprecations
210210
~~~~~~~~~~~~
211211
- Deprecated parameter ``inplace`` in :meth:`MultiIndex.set_codes` and :meth:`MultiIndex.set_levels` (:issue:`35626`)
212212
- Deprecated parameter ``dtype`` in :meth:`~Index.copy` on method all index classes. Use the :meth:`~Index.astype` method instead for changing dtype (:issue:`35853`)
213+
- Deprecated parameters ``levels`` and ``codes`` in :meth:`~MultiIndex.copy`. Use the :meth:`~MultiIndex.set_levels` and :meth:`~MultiIndex.set_codes` methods instead (:issue:`xxxxx`)
213214
- Date parser functions :func:`~pandas.io.date_converters.parse_date_time`, :func:`~pandas.io.date_converters.parse_date_fields`, :func:`~pandas.io.date_converters.parse_all_fields` and :func:`~pandas.io.date_converters.generic_parser` from ``pandas.io.date_converters`` are deprecated and will be removed in a future version; use :func:`to_datetime` instead (:issue:`35741`)
214215
- :meth:`DataFrame.lookup` is deprecated and will be removed in a future version, use :meth:`DataFrame.melt` and :meth:`DataFrame.loc` instead (:issue:`18682`)
215216
- The :meth:`Index.to_native_types` is deprecated. Use ``.astype(str)`` instead (:issue:`28867`)

pandas/core/indexes/multi.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,11 @@ def copy(
11321132
11331133
.. deprecated:: 1.2.0
11341134
levels : sequence, optional
1135+
1136+
.. deprecated:: 1.2.0
11351137
codes : sequence, optional
1138+
1139+
.. deprecated:: 1.2.0
11361140
deep : bool, default False
11371141
name : Label
11381142
Kept for compatibility with 1-dimensional Index. Should not be used.
@@ -1148,6 +1152,21 @@ def copy(
11481152
This could be potentially expensive on large MultiIndex objects.
11491153
"""
11501154
names = self._validate_names(name=name, names=names, deep=deep)
1155+
if levels is not None:
1156+
warnings.warn(
1157+
"parameter levels is deprecated and will be removed in a future "
1158+
"version. Use the set_levels method instead.",
1159+
FutureWarning,
1160+
stacklevel=2,
1161+
)
1162+
if codes is not None:
1163+
warnings.warn(
1164+
"parameter codes is deprecated and will be removed in a future "
1165+
"version. Use the set_codes method instead.",
1166+
FutureWarning,
1167+
stacklevel=2,
1168+
)
1169+
11511170
if deep:
11521171
from copy import deepcopy
11531172

@@ -1575,7 +1594,7 @@ def dropna(self, how="any"):
15751594
raise ValueError(f"invalid how option: {how}")
15761595

15771596
new_codes = [level_codes[~indexer] for level_codes in self.codes]
1578-
return self.copy(codes=new_codes, deep=True)
1597+
return self.copy(deep=True).set_codes(codes=new_codes)
15791598

15801599
def _get_level_values(self, level, unique=False):
15811600
"""

pandas/tests/indexes/multi/test_copy.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ def test_copy_method(deep):
6969
"kwarg, value",
7070
[
7171
("names", ["third", "fourth"]),
72-
("levels", [["foo2", "bar2"], ["fizz2", "buzz2"]]),
73-
("codes", [[1, 0, 0, 0], [1, 1, 0, 0]]),
7472
],
7573
)
7674
def test_copy_method_kwargs(deep, kwarg, value):
@@ -85,3 +83,24 @@ def test_copy_method_kwargs(deep, kwarg, value):
8583
assert getattr(idx_copy, kwarg) == value
8684
else:
8785
assert [list(i) for i in getattr(idx_copy, kwarg)] == value
86+
87+
88+
@pytest.mark.parametrize("deep", [True, False])
89+
@pytest.mark.parametrize(
90+
"param_name, param_value",
91+
[
92+
("levels", [["foo2", "bar2"], ["fizz2", "buzz2"]]),
93+
("codes", [[1, 0, 0, 0], [1, 1, 0, 0]]),
94+
],
95+
)
96+
def test_copy_deprecated_parameters(deep, param_name, param_value):
97+
# gh-xxxxx
98+
idx = MultiIndex(
99+
levels=[["foo", "bar"], ["fizz", "buzz"]],
100+
codes=[[0, 0, 0, 1], [0, 0, 1, 1]],
101+
names=["first", "second"],
102+
)
103+
with tm.assert_produces_warning(FutureWarning):
104+
idx_copy = idx.copy(deep=deep, **{param_name: param_value})
105+
106+
assert [list(i) for i in getattr(idx_copy, param_name)] == param_value

0 commit comments

Comments
 (0)