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 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
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
1 change: 0 additions & 1 deletion pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,6 @@ 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 argsort(self, *args, **kwargs): ...
def get_indexer_non_unique(self, target): ...
def get_indexer_for(self, target, **kwargs): ...
Expand Down
2 changes: 2 additions & 0 deletions pandas-stubs/core/indexes/datetimes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ from pandas.core.series import (
TimedeltaSeries,
TimestampSeries,
)
from typing_extensions import Self

from pandas._typing import (
AnyArrayLike,
Expand Down Expand Up @@ -90,6 +91,7 @@ class DatetimeIndex(DatetimeTimedeltaMixin[Timestamp], DatetimeIndexProperties):
def tzinfo(self) -> tzinfo | None: ...
@property
def dtype(self) -> np.dtype | DatetimeTZDtype: ...
def shift(self, periods: int = ..., freq=...) -> Self: ...

def date_range(
start: str | DateAndDatetimeLike | None = ...,
Expand Down
1 change: 1 addition & 0 deletions pandas-stubs/core/indexes/period.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class PeriodIndex(DatetimeIndexOpsMixin[pd.Period], PeriodIndexFieldOps):
def memory_usage(self, deep: bool = ...): ...
@property
def freqstr(self) -> str: ...
def shift(self, periods: int = ..., freq=...) -> Self: ...

def period_range(
start: (
Expand Down
1 change: 1 addition & 0 deletions pandas-stubs/core/indexes/timedeltas.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class TimedeltaIndex(DatetimeTimedeltaMixin[Timedelta], TimedeltaIndexProperties
def inferred_type(self) -> str: ...
def insert(self, loc, item): ...
def to_series(self, index=..., name: Hashable = ...) -> TimedeltaSeries: ...
def shift(self, periods: int = ..., freq=...) -> Self: ...

def timedelta_range(
start: TimedeltaConvertibleTypes = ...,
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)