Skip to content
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

API / CoW: shallow copy of DataFrame/Series (.copy(deep=False)) also returns shallow copy of the index/columns #53722

Merged

Conversation

jorisvandenbossche
Copy link
Member

@jorisvandenbossche jorisvandenbossche commented Jun 19, 2023

Broken off a subset of #53699, only focusing on the behaviour of df.copy(deep=False) (i.e. a "shallow copy"). And address the copy(deep=False) part of #53721.

Currently, a shallow copy of a DataFrame/Series only creates a new DataFrame/Series object, but still shares the same data (with tracking references, to ensure copy-on-write) and Index objects.
Because Index objects have some mutable aspects (not their values, but for example there name), that means that sharing an identical object in the resulting shallow copy can propagate mutations to those mutable parts of the index.

See #53721 for a longer description of the context.

This PR just updates the DataFrame/Series copy() method to also take a shallow copy of the index/columns Index object, in case of deep=False.

And based on the tests I had to update to get this passing, it illustrates some of the consequential changes in other places because of changing the shallow copy.

Copy link
Member

@phofl phofl left a comment

Choose a reason for hiding this comment

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

lgtm

@mroeschke
Copy link
Member

Thanks could use a whatsnew note in 2.1

@github-actions
Copy link
Contributor

github-actions bot commented Aug 4, 2023

This pull request is stale because it has been open for thirty days with no activity. Please update and respond to this comment if you're still interested in working on this.

@github-actions github-actions bot added the Stale label Aug 4, 2023
@mroeschke mroeschke removed the Stale label Aug 4, 2023
@mroeschke mroeschke added this to the 2.1 milestone Aug 4, 2023
@mroeschke mroeschke merged commit 782f438 into pandas-dev:main Aug 4, 2023
@mroeschke
Copy link
Member

Thanks @jorisvandenbossche

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