FIX: DatetimeIndex.array should return DatetimeArray instead of ExtensionArray #1373
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.
Description
This PR fixes the type annotation issue where
DatetimeIndex.arrayandpd.to_datetime().arraywere incorrectly typed as returningExtensionArrayinstead of the specificDatetimeArray.Related Issue
Fixes #1371 -
to_datetime().array gives the generic ExtensionArray, but it turns out to be DatetimeArrayProblem
The current type stubs incorrectly specify that
DatetimeIndex.arrayreturns the genericExtensionArraytype, when in reality it returns the specificDatetimeArraytype at runtime. This caused type checkers (mypy and pyright) to incorrectly identify the return type.Solution
DatetimeArrayinpandas-stubs/core/indexes/datetimes.pyi@property def array(self) -> DatetimeArray: ...to theDatetimeIndexclasstest_datetime_index_array_property()intests/indexes/test_indexes.pyto verify the correct type is returnedChanges Made
pandas-stubs/core/indexes/datetimes.pyi- Added proper type annotationtests/indexes/test_indexes.py- Ensures the fix works correctlyVerification
DatetimeArrayas the return typeTesting
The fix has been verified with:
python -m pytest tests/indexes/test_indexes.py::test_datetime_index_array_property -v- Test passespd.to_datetime().arrayandpd.DatetimeIndex().arraywork correctly