Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
FIX: DatetimeIndex.array should return DatetimeArray instead of Exten…
…sionArray

- Override array property in DatetimeIndex to return DatetimeArray
- Add test case to verify the correct type is returned
- Fixes issue where type checkers incorrectly identified return type
  • Loading branch information
gamal1osama committed Sep 17, 2025
commit 41f7f808f0a7b4beeef222dd281211cce032366d
6 changes: 6 additions & 0 deletions pandas-stubs/core/indexes/datetimes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ from pandas import (
TimedeltaIndex,
Timestamp,
)
from pandas.core.arrays import DatetimeArray
from pandas.core.indexes.accessors import DatetimeIndexProperties
from pandas.core.indexes.datetimelike import DatetimeTimedeltaMixin
from pandas.core.series import (
Expand Down Expand Up @@ -61,6 +62,11 @@ class DatetimeIndex(
name: Hashable = ...,
) -> Self: ...
def __reduce__(self): ...

# Override the array property to return DatetimeArray instead of ExtensionArray
@property
def array(self) -> DatetimeArray: ...

# various ignores needed for mypy, as we do want to restrict what can be used in
# arithmetic for these types
@overload # type: ignore[override]
Expand Down
14 changes: 14 additions & 0 deletions tests/indexes/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import pandas as pd
from pandas.core.arrays.categorical import Categorical
from pandas.core.indexes.base import Index
from pandas.core.arrays import DatetimeArray
from typing import assert_type
from typing_extensions import (
Never,
assert_type,
Expand Down Expand Up @@ -1516,3 +1518,15 @@ def test_period_index_asof_locs() -> None:
assert_type(idx.asof_locs(where, mask), np_1darray[np.intp]),
np_1darray[np.intp],
)


def test_datetime_index_array_property():
"""Test that DatetimeIndex.array returns DatetimeArray instead of ExtensionArray."""
# Test with to_datetime
arr = pd.to_datetime(["2020-01-01", "2020-01-02"]).array
assert_type(arr, DatetimeArray)

# Test with DatetimeIndex directly
dt_index = pd.DatetimeIndex(["2020-01-01", "2020-01-02"])
assert_type(dt_index.array, DatetimeArray)