Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 13 additions & 1 deletion pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
@overload
def __new__(
cls,
data: Sequence[np.datetime64 | datetime] | IndexOpsMixin[datetime],
data: (
Sequence[np.datetime64 | datetime] | IndexOpsMixin[datetime] | DatetimeIndex
),
*,
dtype: TimestampDtypeArg = ...,
copy: bool = ...,
Expand Down Expand Up @@ -274,6 +276,16 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
name: Hashable = ...,
tupleize_cols: bool = ...,
) -> IntervalIndex[Interval[Any]]: ...
@overload
def __new__(
cls,
data: DatetimeIndex,
*,
dtype: TimestampDtypeArg | None = ...,
copy: bool = ...,
name: Hashable = ...,
tupleize_cols: bool = ...,
) -> DatetimeIndex: ...
# generic overloads
@overload
def __new__(
Expand Down
2 changes: 1 addition & 1 deletion tests/indexes/test_datetime_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_index_relops() -> None:
)
x = pd.Timestamp("2022-01-17")
idx = check(
assert_type(pd.Index(data, name="date"), "pd.Index[pd.Timestamp]"), pd.Index
assert_type(pd.Index(data, name="date"), pd.DatetimeIndex), pd.DatetimeIndex
)
check(assert_type(data[x <= idx], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[x < idx], pd.DatetimeIndex), pd.DatetimeIndex)
Expand Down
8 changes: 8 additions & 0 deletions tests/indexes/test_datetime_index_1440.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from typing import assert_type
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there is already a test file for datetime index it is probably cleaner to consolidate your tests there (if we made one test file by issue it would quickly become overwhelming).
Another point is that you’d need to add check(assert_type(…), pd.DatetimeIndex) in top of the assertion you have already added here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yusufcank34 Can you remove that file since it is already covered by the other test you modified? And run pre-commit before you push again, I will merge after it.

import pandas as pd

# GH#1440 – constructing Index from DatetimeIndex should return DatetimeIndex
data = pd.date_range("2022-01-01", "2022-01-03", freq="D")
idx = pd.Index(data, name="date")

assert_type(idx, pd.DatetimeIndex)
Loading