Skip to content

Commit a4615d5

Browse files
GH969 Fix timestamp series and offset additions (#1004)
* GH969 Fix timestamp series and offset additions * Update datetimes.pyi * PR feedback
1 parent 5177d67 commit a4615d5

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

pandas-stubs/core/indexes/datetimes.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ class DatetimeIndex(DatetimeTimedeltaMixin[Timestamp], DatetimeIndexProperties):
5757
def __add__(self, other: TimedeltaSeries) -> TimestampSeries: ...
5858
@overload
5959
def __add__(
60-
self, other: timedelta | Timedelta | TimedeltaIndex
60+
self, other: timedelta | Timedelta | TimedeltaIndex | BaseOffset
6161
) -> DatetimeIndex: ...
6262
@overload
6363
def __sub__(self, other: TimedeltaSeries) -> TimestampSeries: ...
6464
@overload
6565
def __sub__(
66-
self, other: timedelta | Timedelta | TimedeltaIndex
66+
self, other: timedelta | Timedelta | TimedeltaIndex | BaseOffset
6767
) -> DatetimeIndex: ...
6868
@overload
6969
def __sub__(

pandas-stubs/core/series.pyi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,7 +2073,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
20732073
class TimestampSeries(Series[Timestamp]):
20742074
@property
20752075
def dt(self) -> TimestampProperties: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
2076-
def __add__(self, other: TimedeltaSeries | np.timedelta64 | timedelta) -> TimestampSeries: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
2076+
def __add__(self, other: TimedeltaSeries | np.timedelta64 | timedelta | BaseOffset) -> TimestampSeries: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
20772077
def __radd__(self, other: TimedeltaSeries | np.timedelta64 | timedelta) -> TimestampSeries: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
20782078
@overload # type: ignore[override]
20792079
def __sub__(
@@ -2082,7 +2082,9 @@ class TimestampSeries(Series[Timestamp]):
20822082
@overload
20832083
def __sub__( # pyright: ignore[reportIncompatibleMethodOverride]
20842084
self,
2085-
other: timedelta | TimedeltaSeries | TimedeltaIndex | np.timedelta64,
2085+
other: (
2086+
timedelta | TimedeltaSeries | TimedeltaIndex | np.timedelta64 | BaseOffset
2087+
),
20862088
) -> TimestampSeries: ...
20872089
def __mul__(self, other: float | Series[int] | Series[float] | Sequence[float]) -> TimestampSeries: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
20882090
def __truediv__(self, other: float | Series[int] | Series[float] | Sequence[float]) -> TimestampSeries: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]

tests/test_series.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
from pandas._libs.missing import NAType
4242
from pandas._libs.tslibs import BaseOffset
43+
from pandas._libs.tslibs.offsets import YearEnd
4344
from pandas._typing import (
4445
DtypeObj,
4546
Scalar,
@@ -3030,6 +3031,20 @@ def test_timedeltaseries_operators() -> None:
30303031
)
30313032

30323033

3034+
def test_timestamp_series() -> None:
3035+
series = pd.Series([pd.Timestamp(2024, 4, 4)])
3036+
check(
3037+
assert_type(series + YearEnd(0), TimestampSeries),
3038+
TimestampSeries,
3039+
pd.Timestamp,
3040+
)
3041+
check(
3042+
assert_type(series - YearEnd(0), TimestampSeries),
3043+
TimestampSeries,
3044+
pd.Timestamp,
3045+
)
3046+
3047+
30333048
def test_pipe() -> None:
30343049
ser = pd.Series(range(10))
30353050

tests/test_timefuncs.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,13 @@ def test_some_offsets() -> None:
750750
)
751751

752752

753+
def test_timestampseries_offset() -> None:
754+
"""Test that adding an offset to a timestamp series works."""
755+
vv = pd.bdate_range("2024-09-01", "2024-09-10")
756+
shifted_vv = vv + pd.tseries.offsets.YearEnd(0)
757+
check(assert_type(shifted_vv, pd.DatetimeIndex), pd.DatetimeIndex)
758+
759+
753760
def test_types_to_numpy() -> None:
754761
td_s = pd.to_timedelta(pd.Series([10, 20]), "minutes")
755762
check(assert_type(td_s.to_numpy(), np.ndarray), np.ndarray)

0 commit comments

Comments
 (0)