Skip to content

Cow drop level #50365

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

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 2 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,8 @@ def _set_axis_nocheck(self, labels, axis: Axis, inplace: bool_t, copy: bool_t):
else:
# With copy=False, we create a new object but don't copy the
# underlying data.
obj = self.copy(deep=copy)
if copy:
obj = self.copy(deep=None)
setattr(obj, obj._get_axis_name(axis), labels)
return obj

Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,21 @@ def test_head_tail(method, using_copy_on_write):
df2.iloc[0, 0] = 1
tm.assert_frame_equal(df, df_orig)

def test_droplevel(using_copy_on_write):
# GH 49473
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}).set_index(["a","b"])
df_orig = df.copy()
df2 = df.droplevel(0)

if using_copy_on_write:
assert np.shares_memory(get_array(df2, "c"), get_array(df, "c"))
else:
assert not np.shares_memory(get_array(df2, "c"), get_array(df, "c"))

# mutating df2 triggers a copy-on-write for that column / block
df2.loc["b","c"] = 1



def test_assign(using_copy_on_write):
df = DataFrame({"a": [1, 2, 3]})
Expand Down Expand Up @@ -356,3 +371,6 @@ def test_reorder_levels(using_copy_on_write):
if using_copy_on_write:
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
tm.assert_frame_equal(df, df_orig)

assert not np.shares_memory(get_array(df2, "c"), get_array(df, "c"))
tm.assert_frame_equal(df, df_orig)