Skip to content

API/CoW: Return copies for head and tail #54011

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 5 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Copy-on-Write improvements
- The :class:`DataFrame` constructor, when constructing a DataFrame from a dictionary
of Index objects and specifying ``copy=False``, will now use a lazy copy
of those Index objects for the columns of the DataFrame (:issue:`52947`)
- :meth:`DataFrame.head` and :meth:`DataFrame.tail` will now return deep copies (:issue:`54011`)
- Add lazy copy mechanism to :meth:`DataFrame.eval` (:issue:`53746`)

- Trying to operate inplace on a temporary column selection
Expand Down
6 changes: 6 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5786,6 +5786,8 @@ def head(self, n: int = 5) -> Self:
4 monkey
5 parrot
"""
if using_copy_on_write():
return self.iloc[:n].copy()
return self.iloc[:n]

@final
Expand Down Expand Up @@ -5861,6 +5863,10 @@ def tail(self, n: int = 5) -> Self:
7 whale
8 zebra
"""
if using_copy_on_write():
if n == 0:
return self.iloc[0:0].copy()
return self.iloc[-n:].copy()
if n == 0:
return self.iloc[0:0]
return self.iloc[-n:]
Expand Down
9 changes: 6 additions & 3 deletions pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,16 +895,19 @@ def test_head_tail(method, using_copy_on_write):
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"))
# We are explicitly deviating for CoW here to make an eager copy (avoids
# tracking references for very cheap ops)
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a comment here to note we explicitly deviate in head/tail methods to do an eager copy (as it now contrasts with most other tests in this file)

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense

assert not 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, "b"), get_array(df, "b"))
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.
assert np.shares_memory(get_array(df2, "b"), get_array(df, "b"))
df2.iloc[0, 0] = 1
tm.assert_frame_equal(df, df_orig)

Expand Down