Skip to content

Commit 52eab76

Browse files
committed
ENH: Futher improvements to Period
1 parent eafe8ac commit 52eab76

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

pandas-stubs/core/indexes/timedeltas.pyi

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ from typing import (
44
overload,
55
)
66

7-
from pandas import DateOffset
7+
from pandas import (
8+
DateOffset,
9+
Period,
10+
)
811
from pandas.core.indexes.accessors import TimedeltaIndexProperties
912
from pandas.core.indexes.datetimelike import DatetimeTimedeltaMixin
1013
from pandas.core.indexes.datetimes import DatetimeIndex
14+
from pandas.core.indexes.period import PeriodIndex
1115
from pandas.core.series import TimedeltaSeries
1216

1317
from pandas._libs import (
@@ -33,6 +37,8 @@ class TimedeltaIndex(DatetimeTimedeltaMixin, TimedeltaIndexProperties):
3337
# various ignores needed for mypy, as we do want to restrict what can be used in
3438
# arithmetic for these types
3539
@overload # type: ignore[override]
40+
def __add__(self, other: Period) -> PeriodIndex: ...
41+
@overload
3642
def __add__(self, other: DatetimeIndex) -> DatetimeIndex: ...
3743
@overload
3844
def __add__(self, other: Timedelta | TimedeltaIndex) -> TimedeltaIndex: ...

pandas-stubs/core/series.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,6 +1737,10 @@ class TimestampSeries(Series[Timestamp]):
17371737

17381738
class TimedeltaSeries(Series[Timedelta]):
17391739
# ignores needed because of mypy
1740+
@overload # type: ignore[override]
1741+
def __add__(self, other: Period) -> PeriodSeries: ...
1742+
@overload
1743+
def __add__(self, other: Timestamp | DatetimeIndex) -> TimestampSeries: ...
17401744
def __radd__(self, pther: Timestamp | TimestampSeries) -> TimestampSeries: ... # type: ignore[override]
17411745
def __mul__(self, other: num) -> TimedeltaSeries: ... # type: ignore[override]
17421746
def __sub__( # type: ignore[override]

tests/test_scalars.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def test_period_properties() -> None:
9797
check(assert_type(p2.freq, BaseOffset), Day)
9898

9999

100-
def test_periof_add_subtract() -> None:
100+
def test_period_add_subtract() -> None:
101101
p = pd.Period("2012-1-1", freq="D")
102102

103103
as_pd_td = pd.Timedelta(1, "D")
@@ -143,27 +143,36 @@ def test_periof_add_subtract() -> None:
143143
check(assert_type(p - as_nat, NaTType), NaTType)
144144
check(assert_type(p - p.freq, pd.Period), pd.Period)
145145

146+
# The __radd__ and __rsub__ methods are included to
147+
# establish the location of the concrete implementation
148+
# Those missing are using the __add__ of the other class
146149
check(assert_type(as_pd_td + p, pd.Period), pd.Period)
150+
check(assert_type(p.__radd__(as_pd_td), pd.Period), pd.Period)
151+
147152
check(assert_type(as_dt_td + p, pd.Period), pd.Period)
153+
check(assert_type(p.__radd__(as_dt_td), pd.Period), pd.Period)
154+
148155
check(assert_type(as_np_td + p, pd.Period), pd.Period)
156+
check(assert_type(p.__radd__(as_np_td), pd.Period), pd.Period)
157+
149158
check(assert_type(as_np_i64 + p, pd.Period), pd.Period)
159+
check(assert_type(p.__radd__(as_np_i64), pd.Period), pd.Period)
160+
150161
check(assert_type(as_int + p, pd.Period), pd.Period)
162+
check(assert_type(p.__radd__(as_int), pd.Period), pd.Period)
163+
151164
check(assert_type(as_td_series + p, PeriodSeries), pd.Series, pd.Period)
152-
# TODO: Improve Index to not handle __add__(period)
153-
check(assert_type(as_timedelta_idx + p, pd.Index), pd.PeriodIndex)
154-
check(assert_type(as_nat + p, NaTType), NaTType)
155-
check(assert_type(p.freq + p, pd.Period), pd.Period)
156165

157-
check(assert_type(as_period_index - p, pd.Index), pd.Index)
166+
check(assert_type(as_timedelta_idx + p, pd.PeriodIndex), pd.PeriodIndex)
158167

159-
check(assert_type(p.__radd__(as_pd_td), pd.Period), pd.Period)
160-
check(assert_type(p.__radd__(as_dt_td), pd.Period), pd.Period)
161-
check(assert_type(p.__radd__(as_np_td), pd.Period), pd.Period)
162-
check(assert_type(p.__radd__(as_np_i64), pd.Period), pd.Period)
163-
check(assert_type(p.__radd__(as_int), pd.Period), pd.Period)
168+
check(assert_type(as_nat + p, NaTType), NaTType)
164169
check(assert_type(p.__radd__(as_nat), NaTType), NaTType)
170+
171+
check(assert_type(p.freq + p, pd.Period), pd.Period)
165172
check(assert_type(p.__radd__(p.freq), pd.Period), pd.Period)
166173

174+
check(assert_type(as_period_index - p, pd.Index), pd.Index)
175+
167176

168177
def test_period_cmp() -> None:
169178
p = pd.Period("2012-1-1", freq="D")

0 commit comments

Comments
 (0)