Skip to content

GH1189/GH1181 Timestamp subtraction and index shift #1191

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

Merged
merged 2 commits into from
Apr 19, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions pandas-stubs/_libs/tslibs/timestamps.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ class Timestamp(datetime, SupportsIndex):
@overload
def __sub__(self, other: TimedeltaSeries) -> TimestampSeries: ...
@overload
def __sub__(self, other: TimestampSeries) -> TimedeltaSeries: ...
@overload
def __sub__(
self, other: npt.NDArray[np.timedelta64]
) -> npt.NDArray[np.datetime64]: ...
Expand Down
2 changes: 1 addition & 1 deletion pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ class Index(IndexOpsMixin[S1]):
def asof_locs(self, where, mask): ...
def sort_values(self, return_indexer: bool = ..., ascending: bool = ...): ...
def sort(self, *args, **kwargs) -> None: ...
def shift(self, periods: int = ..., freq=...) -> None: ...
def shift(self, periods: int = ..., freq=...) -> Self: ...
Copy link
Member Author

Choose a reason for hiding this comment

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

One small question here, pd.Index.shift is technically not implemented so wondering if it should be NoReturn here and Self for pd.DatetimeIndex, pd.TimedeltaIndex and pd.PeriodIndex or if this is fine as is (technically it would fail at runtime anyway so the typing would not help much to spot the bug).

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should take shift() out of here and only include it in the stubs for those 3 types.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed! Thanks for the direction

def argsort(self, *args, **kwargs): ...
def get_indexer_non_unique(self, target): ...
def get_indexer_for(self, target, **kwargs): ...
Expand Down
15 changes: 15 additions & 0 deletions tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1272,3 +1272,18 @@ def test_datetime_index_max_min_reductions() -> None:
check(assert_type(dtidx.argmin(), np.int64), np.int64)
check(assert_type(dtidx.max(), pd.Timestamp), pd.Timestamp)
check(assert_type(dtidx.min(), pd.Timestamp), pd.Timestamp)


def test_periodindex_shift() -> None:
ind = pd.period_range(start="2022-06-01", periods=10)
check(assert_type(ind.shift(1), pd.PeriodIndex), pd.PeriodIndex)


def test_datetimeindex_shift() -> None:
ind = pd.date_range("2023-01-01", "2023-02-01")
check(assert_type(ind.shift(1), pd.DatetimeIndex), pd.DatetimeIndex)


def test_timedeltaindex_shift() -> None:
ind = pd.date_range("1/1/2021", "1/5/2021") - pd.Timestamp("1/3/2019")
check(assert_type(ind.shift(1), pd.TimedeltaIndex), pd.TimedeltaIndex)
8 changes: 8 additions & 0 deletions tests/test_timefuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1474,3 +1474,11 @@ def test_DatetimeIndex_sub_timedelta() -> None:
def test_to_offset() -> None:
check(assert_type(to_offset(None), None), type(None))
check(assert_type(to_offset("1D"), DateOffset), DateOffset)


def test_timestamp_sub_series() -> None:
"""Test subtracting Series[Timestamp] from Timestamp (see GH1189)."""
ts1 = pd.to_datetime(pd.Series(["2022-03-05", "2022-03-06"]))
one_ts = ts1.iloc[0]
check(assert_type(ts1.iloc[0], pd.Timestamp), pd.Timestamp)
check(assert_type(one_ts - ts1, "TimedeltaSeries"), pd.Series, pd.Timedelta)