Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
12 changes: 12 additions & 0 deletions tests/indexes/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import numpy as np
from numpy import typing as npt
import pandas as pd
from pandas.core.arrays import DatetimeArray
from pandas.core.arrays.categorical import Categorical
from pandas.core.indexes.base import Index
from typing_extensions import (
Expand Down Expand Up @@ -1516,3 +1517,14 @@ 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() -> None:
"""Test that DatetimeIndex.array returns DatetimeArray instead of ExtensionArray."""
# Test with pd.to_datetime().array - this is the main issue reported
arr = pd.to_datetime(["2020-01-01", "2020-01-02"]).array
check(assert_type(arr, DatetimeArray), DatetimeArray)

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