Skip to content

Commit d413f0e

Browse files
authored
FIX: DatetimeIndex.array should return DatetimeArray instead of ExtensionArray (#1373)
* FIX: DatetimeIndex.array should return DatetimeArray instead of ExtensionArray - 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 * Fix test formatting and following project development work flow - establish the current poetry environment and run a complete test suite - Use the check (assrt_Type (...)) pattern required by project standards - Fix import order and code formatting through preset hook - All tests and type of chips now passionally pass
1 parent 5768214 commit d413f0e

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

pandas-stubs/core/indexes/datetimes.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ from pandas import (
2020
TimedeltaIndex,
2121
Timestamp,
2222
)
23+
from pandas.core.arrays import DatetimeArray
2324
from pandas.core.indexes.accessors import DatetimeIndexProperties
2425
from pandas.core.indexes.datetimelike import DatetimeTimedeltaMixin
2526
from pandas.core.series import (
@@ -61,6 +62,11 @@ class DatetimeIndex(
6162
name: Hashable = ...,
6263
) -> Self: ...
6364
def __reduce__(self): ...
65+
66+
# Override the array property to return DatetimeArray instead of ExtensionArray
67+
@property
68+
def array(self) -> DatetimeArray: ...
69+
6470
# various ignores needed for mypy, as we do want to restrict what can be used in
6571
# arithmetic for these types
6672
@overload # type: ignore[override]

tests/indexes/test_indexes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import numpy as np
1313
from numpy import typing as npt
1414
import pandas as pd
15+
from pandas.core.arrays import DatetimeArray
1516
from pandas.core.arrays.categorical import Categorical
1617
from pandas.core.indexes.base import Index
1718
from typing_extensions import (
@@ -1516,3 +1517,14 @@ def test_period_index_asof_locs() -> None:
15161517
assert_type(idx.asof_locs(where, mask), np_1darray[np.intp]),
15171518
np_1darray[np.intp],
15181519
)
1520+
1521+
1522+
def test_datetime_index_array_property() -> None:
1523+
"""Test that DatetimeIndex.array returns DatetimeArray instead of ExtensionArray."""
1524+
# Test with pd.to_datetime().array - this is the main issue reported
1525+
arr = pd.to_datetime(["2020-01-01", "2020-01-02"]).array
1526+
check(assert_type(arr, DatetimeArray), DatetimeArray)
1527+
1528+
# Test with DatetimeIndex constructor directly
1529+
dt_index = pd.DatetimeIndex(["2020-01-01", "2020-01-02"])
1530+
check(assert_type(dt_index.array, DatetimeArray), DatetimeArray)

0 commit comments

Comments
 (0)