-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
BUG: ensure we still honor copy=True in Series constructor in all cases #63342
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
base: main
Are you sure you want to change the base?
BUG: ensure we still honor copy=True in Series constructor in all cases #63342
Conversation
|
|
||
| refs = data._references | ||
| copy = False | ||
| if not copy: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just confirming, does this impact the behavior/address #63306?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, because the default for Index input is copy=False. The only thing I am doing here is for the case when the user explicitly did copy=True, to in that case actually honor that and do a copy, instead of overriding it always to False and tracking the reference (as was done here in the removed code)
| if copy is not False: | ||
| if dtype is None or astype_is_view(data.dtype, pandas_dtype(dtype)): | ||
| data = data.copy() | ||
| copy = False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid another copy later
| Pandas4Warning, | ||
| stacklevel=2, | ||
| ) | ||
| allow_mgr = True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid raising this warning a second time below in some specific corner case
| copy = False | ||
| data = data._mgr | ||
| if data._has_no_reference(0): | ||
| copy = False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reindex could in theory return a shallow copy instead of full copy (if the passed index is identical to data's index), so the change here is to ensure that we only override copy to False if that did not happen (i.e. when there is no reference between the original and reindexed data).
That is to ensure that if the user specified copy=True, it will actually get a copy, and not a shallow copy, even in this case of reindexing.
Expanding the
Series(..)constructor tests to cover all cases for copy=True vs False vs default None, and ensure we still honorcopy=Truewhen passing a Series or Index.Triggered by checking the exact behaviour to describe that in the docstring for #63341