Skip to content

BUG: preserve _id in MultiIndex.copy(deep=False) #47900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1190,20 +1190,23 @@ def copy(
This could be potentially expensive on large MultiIndex objects.
"""
names = self._validate_names(name=name, names=names, deep=deep)
keep_id = not deep
if levels is not None:
warnings.warn(
"parameter levels is deprecated and will be removed in a future "
"version. Use the set_levels method instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
keep_id = False
if codes is not None:
warnings.warn(
"parameter codes is deprecated and will be removed in a future "
"version. Use the set_codes method instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
keep_id = False

if deep:
from copy import deepcopy
Expand All @@ -1225,6 +1228,8 @@ def copy(
)
new_index._cache = self._cache.copy()
new_index._cache.pop("levels", None) # GH32669
if keep_id:
new_index._id = self._id

if dtype:
warnings.warn(
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/indexes/multi/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,15 @@ def test_copy_deprecated_parameters(deep, param_name, param_value):
idx_copy = idx.copy(deep=deep, **{param_name: param_value})

assert [list(i) for i in getattr(idx_copy, param_name)] == param_value


def test_copy_deep_false_retains_id():
# GH#47878
idx = MultiIndex(
levels=[["foo", "bar"], ["fizz", "buzz"]],
codes=[[0, 0, 0, 1], [0, 0, 1, 1]],
names=["first", "second"],
)

res = idx.copy(deep=False)
assert res._id is idx._id