Skip to content

Conversation

@jorisvandenbossche
Copy link
Member

Expanding the Series(..) constructor tests to cover all cases for copy=True vs False vs default None, and ensure we still honor copy=True when passing a Series or Index.

Triggered by checking the exact behaviour to describe that in the docstring for #63341

@jorisvandenbossche jorisvandenbossche added Series Series data structure Constructors Series/DataFrame/Index/pd.array Constructors Copy / view semantics labels Dec 12, 2025
if copy is not False:
if dtype is None or astype_is_view(data.dtype, pandas_dtype(dtype)):
data = data.copy()
copy = False
Copy link
Member Author

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
Copy link
Member Author

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

Comment on lines -457 to +464
copy = False
data = data._mgr
if data._has_no_reference(0):
copy = False
Copy link
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Constructors Series/DataFrame/Index/pd.array Constructors Copy / view semantics Series Series data structure

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants