Skip to content

TST: CoW with df.isetitem() #50692

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 4 commits into from
Jan 12, 2023
Merged
Changes from 2 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
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 @@ -759,3 +759,25 @@ 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_isetitem(using_copy_on_write):
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]})
df_orig = df.copy()
df2 = df.copy(deep=None) # Trigger a CoW
df2.isetitem(1, np.array([-1, -2, -3]))

if using_copy_on_write:
assert np.shares_memory(df["c"].values, df2["c"].values)
assert np.shares_memory(df["a"].values, df2["a"].values)
else:
assert not np.shares_memory(df["c"].values, df2["c"].values)
assert not np.shares_memory(df["a"].values, df2["a"].values)

df2.loc[0, "a"] = 0
tm.assert_series_equal(df["a"], df_orig["a"]) # Original is unchanged
Copy link
Member

Choose a reason for hiding this comment

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

Can we use get_array? A regular getitem sets up references which is what we want to avoid here

Copy link
Member

Choose a reason for hiding this comment

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

Sorry wrong line, in the np.shares_memory and any reason why we don't compare the whole object?

Copy link
Member Author

Choose a reason for hiding this comment

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

Can we use get_array? A regular getitem sets up references which is what we want to avoid here

Changed. Originally, I had put this test in test_setitem.py does the shares_memory checks like this.
Should we change that file over in the future?

Sorry wrong line, in the np.shares_memory and any reason why we don't compare the whole object?

Updated, I had this left over from testing, sorry.

Copy link
Member

Choose a reason for hiding this comment

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

No worries.

Hm I would not change any existing tests as of right now (still early in CoW adaption). In general this is fine if CoW is not activated, but when explicitly testing CoW we should avoid setting up references to avoid side effects


if using_copy_on_write:
assert np.shares_memory(df["c"].values, df2["c"].values)
else:
assert not np.shares_memory(df["c"].values, df2["c"].values)