Skip to content

Commit 84f8d36

Browse files
authored
Index: ensure Index(DatetimeIndex) returns DatetimeIndex (fixes pandas-dev#1440) (pandas-dev#1456)
* Index: ensure Index(DatetimeIndex) returns DatetimeIndex (fixes pandas-dev#1440) * Add isolated test for Index(DatetimeIndex) typing (GH#1440) * Fix: Index(DatetimeIndex) now returns DatetimeIndex; consolidated test (fixes pandas-dev#1440) * Remove redundant test_datetime_index_1440.py and run pre-commit
1 parent 1f5d9d7 commit 84f8d36

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

pandas-stubs/core/indexes/base.pyi

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
189189
@overload
190190
def __new__(
191191
cls,
192-
data: Sequence[np.datetime64 | datetime] | IndexOpsMixin[datetime],
192+
data: (
193+
Sequence[np.datetime64 | datetime] | IndexOpsMixin[datetime] | DatetimeIndex
194+
),
193195
*,
194196
dtype: TimestampDtypeArg = ...,
195197
copy: bool = ...,
@@ -276,6 +278,16 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
276278
name: Hashable = ...,
277279
tupleize_cols: bool = ...,
278280
) -> IntervalIndex[Interval[Any]]: ...
281+
@overload
282+
def __new__(
283+
cls,
284+
data: DatetimeIndex,
285+
*,
286+
dtype: TimestampDtypeArg | None = ...,
287+
copy: bool = ...,
288+
name: Hashable = ...,
289+
tupleize_cols: bool = ...,
290+
) -> DatetimeIndex: ...
279291
# generic overloads
280292
@overload
281293
def __new__(

tests/indexes/test_datetime_index.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def test_index_relops() -> None:
2424
)
2525
x = pd.Timestamp("2022-01-17")
2626
idx = check(
27-
assert_type(pd.Index(data, name="date"), "pd.Index[pd.Timestamp]"), pd.Index
27+
assert_type(pd.Index(data, name="date"), pd.DatetimeIndex), pd.DatetimeIndex
2828
)
2929
check(assert_type(data[x <= idx], pd.DatetimeIndex), pd.DatetimeIndex)
3030
check(assert_type(data[x < idx], pd.DatetimeIndex), pd.DatetimeIndex)

0 commit comments

Comments
 (0)