Skip to content

Commit 82e1ec0

Browse files
lithomas1phofl
authored andcommitted
TST: CoW with df.isetitem() (pandas-dev#50692)
1 parent e685f2e commit 82e1ec0

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
@@ -780,3 +780,25 @@ def test_squeeze(using_copy_on_write):
780780
# Without CoW the original will be modified
781781
assert np.shares_memory(series.values, get_array(df, "a"))
782782
assert df.loc[0, "a"] == 0
783+
784+
785+
def test_isetitem(using_copy_on_write):
786+
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]})
787+
df_orig = df.copy()
788+
df2 = df.copy(deep=None) # Trigger a CoW
789+
df2.isetitem(1, np.array([-1, -2, -3])) # This is inplace
790+
791+
if using_copy_on_write:
792+
assert np.shares_memory(get_array(df, "c"), get_array(df2, "c"))
793+
assert np.shares_memory(get_array(df, "a"), get_array(df2, "a"))
794+
else:
795+
assert not np.shares_memory(get_array(df, "c"), get_array(df2, "c"))
796+
assert not np.shares_memory(get_array(df, "a"), get_array(df2, "a"))
797+
798+
df2.loc[0, "a"] = 0
799+
tm.assert_frame_equal(df, df_orig) # Original is unchanged
800+
801+
if using_copy_on_write:
802+
assert np.shares_memory(get_array(df, "c"), get_array(df2, "c"))
803+
else:
804+
assert not np.shares_memory(get_array(df, "c"), get_array(df2, "c"))

0 commit comments

Comments
 (0)