-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
API: Copy inputs in Index subclass constructors by default (GH#63388) #63398
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
Open
zacharym-collins
wants to merge
23
commits into
pandas-dev:main
Choose a base branch
from
zacharym-collins:api-copy-index-subclasses
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
API: Copy inputs in Index subclass constructors by default (GH#63388) #63398
zacharym-collins
wants to merge
23
commits into
pandas-dev:main
from
zacharym-collins:api-copy-index-subclasses
+182
−19
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Contributor
Author
|
All checks are passing now. I've updated the PR to reflect the latest changes. |
mroeschke
requested changes
Dec 17, 2025
pandas/core/indexes/datetimes.py
Outdated
Comment on lines
692
to
700
| if isinstance(data, (ExtensionArray, np.ndarray)): | ||
| # GH 63388 | ||
| if copy is not False: | ||
| if dtype is None or astype_is_view( | ||
| data.dtype, | ||
| pandas_dtype(dtype), | ||
| ): | ||
| data = data.copy() | ||
| copy = False |
Member
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.
Can you create a @classmethod (place it in indexes/base.py near _raise_scalar_data_error), that encapsulates this logic and can be called here instead?
Extracts the copy/view validation logic into a shared classmethod on the base Index class, as requested during review.
Updates DatetimeIndex, TimedeltaIndex, PeriodIndex, and IntervalIndex to use the shared validation logic, reducing code duplication.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
AGENTS.md.Description:
This PR aligns the behavior of
DatetimeIndex,TimedeltaIndex,PeriodIndex, andIntervalIndexwith the baseIndexclass (andSeries) regarding thecopyparameter.Before, these constructors would ignore
copy=Noneand default to not copying the input data.Now,
copy=Nonedefaults toTruefornp.ndarrayandExtensionArrayinputs, ensuring that the resulting Index does not share memory with the mutable input array by default.Changes:
__new__signatures indatetimes.py,timedeltas.py,period.py, andinterval.pyto acceptcopy: bool | None.copyisNone(orTrue) and the input is an array/EA.Indexdocstring for thecopyparameter.dtypearguments (e.g."datetime64[ns]") are converted toDtypeObjbefore the view checks, preventingAttributeError.test_constructor_int64_nocopyandtest_array_tz) to explicitly passcopy=False. Since the default behavior changed tocopy=True, these tests required an update to continue verifying that the view based construction is still possible when requested.