Skip to content

Commit a6674ed

Browse files
committed
Attempt to introduce OffsetSeries
1 parent 643b1cb commit a6674ed

File tree

4 files changed

+41
-15
lines changed

4 files changed

+41
-15
lines changed

pandas-stubs/_libs/tslibs/period.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ from pandas import (
1313
TimedeltaIndex,
1414
)
1515
from pandas.core.series import (
16+
OffsetSeries,
1617
PeriodSeries,
1718
TimedeltaSeries,
1819
)
@@ -91,7 +92,7 @@ class Period(PeriodMixin):
9192
@overload
9293
def __add__(self, other: Index) -> PeriodIndex: ...
9394
@overload
94-
def __add__(self, other: TimedeltaSeries) -> PeriodSeries: ...
95+
def __add__(self, other: OffsetSeries | TimedeltaSeries) -> PeriodSeries: ...
9596
@overload # type: ignore[override]
9697
def __eq__(self, other: Period) -> bool: ...
9798
@overload

pandas-stubs/core/indexes/period.pyi

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
from typing import Hashable
1+
from typing import (
2+
Hashable,
3+
overload,
4+
)
25

36
import numpy as np
47
import pandas as pd
@@ -7,6 +10,7 @@ from pandas.core.indexes.datetimelike import (
710
DatetimeIndexOpsMixin as DatetimeIndexOpsMixin,
811
)
912
from pandas.core.indexes.numeric import Int64Index
13+
from pandas.core.series import OffsetSeries
1014

1115
from pandas._libs.tslibs import (
1216
BaseOffset,
@@ -28,8 +32,11 @@ class PeriodIndex(DatetimeIndexOpsMixin, Int64Index):
2832
@property
2933
def values(self): ...
3034
def __contains__(self, key) -> bool: ...
31-
# Override due to supertpye incompatibility which has it for NumericIndex or complex.
32-
def __sub__(self, other: Period) -> Index: ... # type: ignore[override]
35+
# Override due to supertype incompatibility which has it for NumericIndex or complex.
36+
@overload # type: ignore[override]
37+
def __sub__(self, other: Period) -> Index: ...
38+
@overload
39+
def __sub__(self, other: PeriodIndex) -> OffsetSeries: ...
3340
def __array__(self, dtype=...) -> np.ndarray: ...
3441
def __array_wrap__(self, result, context=...): ...
3542
def asof_locs(self, where, mask): ...

pandas-stubs/core/series.pyi

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,8 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
13061306
self, other: Timedelta | TimedeltaSeries | TimedeltaIndex
13071307
) -> TimestampSeries: ...
13081308
@overload
1309+
def __sub__(self: Series[Period], other: Series[Period]) -> OffsetSeries: ...
1310+
@overload
13091311
def __sub__(self, other: num | _ListLike | Series) -> Series: ...
13101312
@overload
13111313
def __truediv__(
@@ -1749,3 +1751,10 @@ class PeriodSeries(Series[Period]):
17491751
# ignore needed because of mypy
17501752
@property
17511753
def dt(self) -> PeriodProperties: ... # type: ignore[override]
1754+
def __sub__(self, other: PeriodSeries) -> OffsetSeries: ... # type: ignore[override]
1755+
1756+
class OffsetSeries(Series):
1757+
@overload # type: ignore[override]
1758+
def __radd__(self, other: Period) -> PeriodSeries: ...
1759+
@overload
1760+
def __radd__(self, other: BaseOffset) -> OffsetSeries: ...

tests/test_scalars.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
11
from __future__ import annotations
22

33
import datetime as dt
4-
from typing import (
5-
TYPE_CHECKING,
6-
Any,
7-
cast,
8-
)
4+
from typing import TYPE_CHECKING
95

106
import numpy as np
117
import pandas as pd
12-
from typing_extensions import assert_type
8+
from typing_extensions import (
9+
TypeAlias,
10+
assert_type,
11+
)
1312

1413
from pandas._libs.tslibs import (
1514
BaseOffset,
1615
NaTType,
1716
)
17+
from pandas._typing import npt
1818

1919
if TYPE_CHECKING:
2020
from pandas.core.series import (
21+
OffsetSeries,
2122
PeriodSeries,
2223
TimedeltaSeries,
2324
)
2425

2526
from pandas._typing import np_ndarray_bool
2627
else:
27-
PeriodSeries = TimedeltaSeries = np_ndarray_bool = Any
28+
np_ndarray_bool = npt.NDArray[np.bool_]
29+
PeriodSeries: TypeAlias = pd.Series
30+
TimedeltaSeries: TypeAlias = pd.Series
31+
OffsetSeries: TypeAlias = pd.Series
32+
2833

2934
from tests import check
3035

@@ -114,18 +119,22 @@ def test_periof_add_subtract() -> None:
114119
check(assert_type(p + as_np_i64, pd.Period), pd.Period)
115120
check(assert_type(p + as_int, pd.Period), pd.Period)
116121
check(assert_type(p + p.freq, pd.Period), pd.Period)
117-
check(assert_type(p + (p - as_period_index), pd.PeriodIndex), pd.PeriodIndex)
122+
# offset_index is tested below
123+
offset_index = p - as_period_index
124+
check(assert_type(p + offset_index, pd.PeriodIndex), pd.PeriodIndex)
118125
check(assert_type(p + as_td_series, PeriodSeries), pd.Series, pd.Period)
119126
check(assert_type(p + as_timedelta_idx, pd.PeriodIndex), pd.PeriodIndex)
120127
check(assert_type(p + as_nat, NaTType), NaTType)
121-
das8 = cast(TimedeltaSeries, (as_period_series - as_period_series))
122-
check(assert_type(p + das8, PeriodSeries), pd.Series, pd.Period)
128+
offset_series = as_period_series - as_period_series
129+
# offset_series = cast(OffsetSeries, offset_series)
130+
check(assert_type(offset_series, OffsetSeries), pd.Series)
131+
check(assert_type(p + offset_series, PeriodSeries), pd.Series, pd.Period)
123132
check(assert_type(p - as_pd_td, pd.Period), pd.Period)
124133
check(assert_type(p - as_dt_td, pd.Period), pd.Period)
125134
check(assert_type(p - as_np_td, pd.Period), pd.Period)
126135
check(assert_type(p - as_np_i64, pd.Period), pd.Period)
127136
check(assert_type(p - as_int, pd.Period), pd.Period)
128-
check(assert_type(p - as_period_index, pd.Index), pd.Index)
137+
check(assert_type(offset_index, pd.Index), pd.Index)
129138
check(assert_type(p - as_period, BaseOffset), Day)
130139
check(assert_type(p - as_td_series, PeriodSeries), pd.Series, pd.Period)
131140
check(assert_type(p - as_timedelta_idx, pd.PeriodIndex), pd.PeriodIndex)

0 commit comments

Comments
 (0)