Skip to content

TYP: Add DatetimeIndex.intersection() method and test #745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions pandas-stubs/core/indexes/datetimes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class DatetimeIndex( # type: ignore[misc]
def tzinfo(self) -> tzinfo | None: ...
@property
def dtype(self) -> np.dtype | DatetimeTZDtype: ...
def intersection(self, other: DatetimeIndex | list) -> DatetimeIndex: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

list is too broad. I think Sequence[DatetimeLike] will work where you import DatetimeLike from pandas._typing


def date_range(
start: str | DateAndDatetimeLike | None = ...,
Expand Down
9 changes: 9 additions & 0 deletions tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,3 +978,12 @@ def test_index_constructors():
# to specify all the possible dtype options. For right now, we will leave the
# test here as a reminder that we would like this to be seen as incorrect usage.
pd.Index(flist, dtype=np.float16)


def test_datetimeindex_intersection():
idx1 = pd.DatetimeIndex(["2022-01-01", "2022-01-02"])
idx2 = pd.DatetimeIndex(["2022-01-02", "2022-01-03"])

result = idx1.intersection(idx2)

check(assert_type(result, pd.DatetimeIndex), pd.DatetimeIndex)
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you include the list[DatetimeLike] as I suggested, then you should include tests that do intersection() with lists of those 3 different types. E.g., idx1.intersection([pd.Timestamp(x) for x in idx2]) and something similar for dt.datetime and np.datetime64