Skip to content

TST: Test CoW with DataFrame.items() #50595

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 8 commits into from
Jan 30, 2023
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 @@ -721,3 +721,22 @@ def test_squeeze(using_copy_on_write):
# Without CoW the original will be modified
assert np.shares_memory(series.values, get_array(df, "a"))
assert df.loc[0, "a"] == 0


def test_items(using_copy_on_write):
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]})
df_orig = df.copy()
for name, vals in df.items():
ser = Series(vals)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does vals need to be rewrapped in Series? vals is already a Series

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks updated.


assert np.shares_memory(ser.values, get_array(df, name))

# mutating df2 triggers a copy-on-write for that column / block
ser.iloc[0] = 0

if using_copy_on_write:
assert not np.shares_memory(ser.values, get_array(df, name))
tm.assert_frame_equal(df, df_orig)
else:
# Original frame will be modified
assert df.loc[0, name] == 0