Skip to content

TST: Test squeeze with CoW #50590

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
Jan 6, 2023
Merged
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
19 changes: 19 additions & 0 deletions pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,3 +634,22 @@ def test_droplevel(using_copy_on_write):

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


def test_squeeze(using_copy_on_write):
df = DataFrame({"a": [1, 2, 3]})
df_orig = df.copy()
series = df.squeeze()

# Should share memory regardless of CoW since squeeze is just an iloc
assert np.shares_memory(series.values, get_array(df, "a"))

# mutating squeezed df triggers a copy-on-write for that column/block
series.iloc[0] = 0
if using_copy_on_write:
assert not np.shares_memory(series.values, get_array(df, "a"))
tm.assert_frame_equal(df, df_orig)
else:
# Without CoW the original will be modified
assert np.shares_memory(series.values, get_array(df, "a"))
assert df.loc[0, "a"] == 0