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

Conversation

phofl
Copy link
Member

@phofl phofl commented Jul 5, 2023

  • closes #xxxx (Replace xxxx with the GitHub issue number)
  • Tests added and passed if fixing a bug or adding a new feature
  • All code checks passed.
  • Added type annotations to new arguments/methods/functions.
  • Added an entry in the latest doc/source/whatsnew/vX.X.X.rst file if fixing a bug or adding a new feature.

this is something we talked about to avoid messing up references in Jupyter environments (xref #48998)

@phofl phofl requested a review from jorisvandenbossche July 5, 2023 20:55
Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

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

Thanks!

Would there be a usecase for adding a keyword to let you not make the eager copy? That would probably only be useful if you would use head/tail for non-exploration cases, in which case you can use iloc instead (although people might prefer a method in case of method chaining)

@@ -25,6 +25,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 copies (:issue:`54011`)
Copy link
Member

Choose a reason for hiding this comment

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

Should we clarify this as "deep copy"? With CoW, the methods already do return an object that behaves as a copy / is a shallow copy.

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, thx

@@ -895,16 +895,17 @@ 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"))
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

@phofl
Copy link
Member Author

phofl commented Jul 13, 2023

I would not add a keyword, I am not convinced that there is a proper use-case that would require such a keyword

@phofl phofl merged commit 3005094 into pandas-dev:main Jul 17, 2023
@phofl phofl deleted the head_tail branch July 17, 2023 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants