Skip to content

Commit 399a0ba

Browse files
authored
TST: Test squeeze with CoW (#50590)
1 parent 92e458d commit 399a0ba

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

pandas/tests/copy_view/test_methods.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,3 +634,22 @@ def test_droplevel(using_copy_on_write):
634634

635635
assert not np.shares_memory(get_array(df2, "c"), get_array(df, "c"))
636636
tm.assert_frame_equal(df, df_orig)
637+
638+
639+
def test_squeeze(using_copy_on_write):
640+
df = DataFrame({"a": [1, 2, 3]})
641+
df_orig = df.copy()
642+
series = df.squeeze()
643+
644+
# Should share memory regardless of CoW since squeeze is just an iloc
645+
assert np.shares_memory(series.values, get_array(df, "a"))
646+
647+
# mutating squeezed df triggers a copy-on-write for that column/block
648+
series.iloc[0] = 0
649+
if using_copy_on_write:
650+
assert not np.shares_memory(series.values, get_array(df, "a"))
651+
tm.assert_frame_equal(df, df_orig)
652+
else:
653+
# Without CoW the original will be modified
654+
assert np.shares_memory(series.values, get_array(df, "a"))
655+
assert df.loc[0, "a"] == 0

0 commit comments

Comments
 (0)