From 41f7f808f0a7b4beeef222dd281211cce032366d Mon Sep 17 00:00:00 2001 From: Gamal Osama Date: Wed, 17 Sep 2025 22:29:38 +0300 Subject: [PATCH 1/2] 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 --- pandas-stubs/core/indexes/datetimes.pyi | 6 ++++++ tests/indexes/test_indexes.py | 14 ++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/pandas-stubs/core/indexes/datetimes.pyi b/pandas-stubs/core/indexes/datetimes.pyi index acbaeae5e..05a15c69b 100644 --- a/pandas-stubs/core/indexes/datetimes.pyi +++ b/pandas-stubs/core/indexes/datetimes.pyi @@ -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 ( @@ -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] diff --git a/tests/indexes/test_indexes.py b/tests/indexes/test_indexes.py index ba7686876..b42d1781a 100644 --- a/tests/indexes/test_indexes.py +++ b/tests/indexes/test_indexes.py @@ -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, @@ -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) + \ No newline at end of file From 63a966fc4386d55cf690e87ea0cc8e26bbc7df8e Mon Sep 17 00:00:00 2001 From: Gamal Osama Date: Thu, 18 Sep 2025 01:31:37 +0300 Subject: [PATCH 2/2] 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 --- pandas-stubs/core/indexes/datetimes.pyi | 2 +- tests/indexes/test_indexes.py | 16 +++++++--------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/pandas-stubs/core/indexes/datetimes.pyi b/pandas-stubs/core/indexes/datetimes.pyi index 05a15c69b..4c93bf4ad 100644 --- a/pandas-stubs/core/indexes/datetimes.pyi +++ b/pandas-stubs/core/indexes/datetimes.pyi @@ -66,7 +66,7 @@ class DatetimeIndex( # 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] diff --git a/tests/indexes/test_indexes.py b/tests/indexes/test_indexes.py index b42d1781a..eb6a790b4 100644 --- a/tests/indexes/test_indexes.py +++ b/tests/indexes/test_indexes.py @@ -12,10 +12,9 @@ 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 pandas.core.arrays import DatetimeArray -from typing import assert_type from typing_extensions import ( Never, assert_type, @@ -1520,13 +1519,12 @@ def test_period_index_asof_locs() -> None: ) -def test_datetime_index_array_property(): +def test_datetime_index_array_property() -> None: """Test that DatetimeIndex.array returns DatetimeArray instead of ExtensionArray.""" - # Test with to_datetime + # Test with pd.to_datetime().array - this is the main issue reported arr = pd.to_datetime(["2020-01-01", "2020-01-02"]).array - assert_type(arr, DatetimeArray) - - # Test with DatetimeIndex directly + check(assert_type(arr, DatetimeArray), DatetimeArray) + + # Test with DatetimeIndex constructor directly dt_index = pd.DatetimeIndex(["2020-01-01", "2020-01-02"]) - assert_type(dt_index.array, DatetimeArray) - \ No newline at end of file + check(assert_type(dt_index.array, DatetimeArray), DatetimeArray)