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
22 changes: 22 additions & 0 deletions pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,28 @@ def test_squeeze(using_copy_on_write):
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()

# Test this twice, since the second time, the item cache will be
# triggered, and we want to make sure it still works then.
for i in range(2):
for name, ser in df.items():

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

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

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


@pytest.mark.parametrize(
"replace_kwargs",
[
Expand Down