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.array
andpd.to_datetime().array
were incorrectly typed as returningExtensionArray
instead of the specificDatetimeArray
.Related Issue
Fixes #1371 -
to_datetime().array gives the generic ExtensionArray, but it turns out to be DatetimeArray
Problem
The current type stubs incorrectly specify that
DatetimeIndex.array
returns the genericExtensionArray
type, when in reality it returns the specificDatetimeArray
type at runtime. This caused type checkers (mypy and pyright) to incorrectly identify the return type.Solution
DatetimeArray
inpandas-stubs/core/indexes/datetimes.pyi
@property def array(self) -> DatetimeArray: ...
to theDatetimeIndex
classtest_datetime_index_array_property()
intests/indexes/test_indexes.py
to 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
DatetimeArray
as 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().array
andpd.DatetimeIndex().array
work correctly