Skip to content

Commit ae1d9c9

Browse files
authored
TST: Test CoW with DataFrame.items() (#50595)
1 parent d6afc86 commit ae1d9c9

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

pandas/tests/copy_view/test_methods.py

+22
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,28 @@ def test_squeeze(using_copy_on_write):
10991099
assert df.loc[0, "a"] == 0
11001100

11011101

1102+
def test_items(using_copy_on_write):
1103+
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]})
1104+
df_orig = df.copy()
1105+
1106+
# Test this twice, since the second time, the item cache will be
1107+
# triggered, and we want to make sure it still works then.
1108+
for i in range(2):
1109+
for name, ser in df.items():
1110+
1111+
assert np.shares_memory(get_array(ser, name), get_array(df, name))
1112+
1113+
# mutating df triggers a copy-on-write for that column / block
1114+
ser.iloc[0] = 0
1115+
1116+
if using_copy_on_write:
1117+
assert not np.shares_memory(get_array(ser, name), get_array(df, name))
1118+
tm.assert_frame_equal(df, df_orig)
1119+
else:
1120+
# Original frame will be modified
1121+
assert df.loc[0, name] == 0
1122+
1123+
11021124
@pytest.mark.parametrize(
11031125
"replace_kwargs",
11041126
[

0 commit comments

Comments
 (0)