Skip to content

TST/CoW: copy-on-write tests for df.head and df.tail #49963

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
Dec 1, 2022
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
30 changes: 30 additions & 0 deletions pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,33 @@ def test_set_index(using_copy_on_write):
df2.iloc[0, 1] = 0
assert not np.shares_memory(get_array(df2, "c"), get_array(df, "c"))
tm.assert_frame_equal(df, df_orig)


@pytest.mark.parametrize(
"method",
[
lambda df: df.head(),
lambda df: df.head(2),
lambda df: df.tail(),
lambda df: df.tail(3),
],
)
def test_head_tail(method, using_copy_on_write):
df = DataFrame({"a": [1, 2, 3], "b": [0.1, 0.2, 0.3]})
df_orig = df.copy()
df2 = method(df)
df2._mgr._verify_integrity()

if using_copy_on_write:
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
assert np.shares_memory(get_array(df2, "b"), get_array(df, "b"))

# modify df2 to trigger CoW for that block
df2.iloc[0, 0] = 0
assert np.shares_memory(get_array(df2, "b"), get_array(df, "b"))
if using_copy_on_write:
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
else:
# without CoW enabled, head and tail return views. Mutating df2 also mutates df.
df2.iloc[0, 0] = 1
tm.assert_frame_equal(df, df_orig)