Skip to content

TST: Add test for CoW in pop #50569

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 1 commit into from
Jan 5, 2023
Merged
Changes from all 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
21 changes: 21 additions & 0 deletions pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,27 @@ def test_select_dtypes(using_copy_on_write):
tm.assert_frame_equal(df, df_orig)


def test_pop(using_copy_on_write):
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]})
df_orig = df.copy()
view_original = df[:]
result = df.pop("a")

assert np.shares_memory(result.values, get_array(view_original, "a"))
assert np.shares_memory(get_array(df, "b"), get_array(view_original, "b"))

if using_copy_on_write:
result.iloc[0] = 0
assert not np.shares_memory(result.values, get_array(view_original, "a"))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert not np.shares_memory(result.values, get_array(view_original, "a"))
assert not np.shares_memory(result.values, get_array(view_original, "a"))
assert df.iloc[0, 0] == 1

To ensure the original wasn't modified?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, no, nevermind. pop of course removed that column from df, so we don't have to check it there, and that's the wholeponit of having this view_original (which is already checked below that it is still equal to the original dataframe)

df.iloc[0, 0] = 0
if using_copy_on_write:
assert not np.shares_memory(get_array(df, "b"), get_array(view_original, "b"))
tm.assert_frame_equal(view_original, df_orig)
else:
expected = DataFrame({"a": [1, 2, 3], "b": [0, 5, 6], "c": [0.1, 0.2, 0.3]})
tm.assert_frame_equal(view_original, expected)


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