Skip to content

Commit eafe8ac

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

File tree

4 files changed

+42
-16
lines changed

4 files changed

+42
-16
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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
194194
name: Hashable | None = ...,
195195
copy: bool = ...,
196196
fastpath: bool = ...,
197-
) -> Series[Period]: ...
197+
) -> PeriodSeries: ...
198198
@overload
199199
def __new__(
200200
cls,
@@ -1749,3 +1749,10 @@ class PeriodSeries(Series[Period]):
17491749
# ignore needed because of mypy
17501750
@property
17511751
def dt(self) -> PeriodProperties: ... # type: ignore[override]
1752+
def __sub__(self, other: PeriodSeries) -> OffsetSeries: ... # type: ignore[override]
1753+
1754+
class OffsetSeries(Series):
1755+
@overload # type: ignore[override]
1756+
def __radd__(self, other: Period) -> PeriodSeries: ...
1757+
@overload
1758+
def __radd__(self, other: BaseOffset) -> OffsetSeries: ...

tests/test_scalars.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
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,
@@ -18,13 +17,20 @@
1817

1918
if TYPE_CHECKING:
2019
from pandas.core.series import (
20+
OffsetSeries,
2121
PeriodSeries,
2222
TimedeltaSeries,
2323
)
2424

2525
from pandas._typing import np_ndarray_bool
2626
else:
27-
PeriodSeries = TimedeltaSeries = np_ndarray_bool = Any
27+
import numpy.typing as npt
28+
29+
np_ndarray_bool = npt.NDArray[np.bool_]
30+
PeriodSeries: TypeAlias = pd.Series
31+
TimedeltaSeries: TypeAlias = pd.Series
32+
OffsetSeries: TypeAlias = pd.Series
33+
2834

2935
from tests import check
3036

@@ -100,11 +106,13 @@ def test_periof_add_subtract() -> None:
100106
as_np_i64 = np.int64(1)
101107
as_int = int(1)
102108
as_period_index = pd.period_range("2012-1-1", periods=10, freq="D")
109+
check(assert_type(as_period_index, pd.PeriodIndex), pd.PeriodIndex)
103110
as_period = pd.Period("2012-1-1", freq="D")
104111
scale = 24 * 60 * 60 * 10**9
105112
as_td_series = pd.Series(pd.timedelta_range(scale, scale, freq="D"))
106113
check(assert_type(as_td_series, TimedeltaSeries), pd.Series)
107114
as_period_series = pd.Series(as_period_index)
115+
check(assert_type(as_period_series, PeriodSeries), pd.Series)
108116
as_timedelta_idx = pd.timedelta_range(scale, scale, freq="D")
109117
as_nat = pd.NaT
110118

@@ -114,18 +122,21 @@ def test_periof_add_subtract() -> None:
114122
check(assert_type(p + as_np_i64, pd.Period), pd.Period)
115123
check(assert_type(p + as_int, pd.Period), pd.Period)
116124
check(assert_type(p + p.freq, pd.Period), pd.Period)
117-
check(assert_type(p + (p - as_period_index), pd.PeriodIndex), pd.PeriodIndex)
125+
# offset_index is tested below
126+
offset_index = p - as_period_index
127+
check(assert_type(p + offset_index, pd.PeriodIndex), pd.PeriodIndex)
118128
check(assert_type(p + as_td_series, PeriodSeries), pd.Series, pd.Period)
119129
check(assert_type(p + as_timedelta_idx, pd.PeriodIndex), pd.PeriodIndex)
120130
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)
131+
offset_series = as_period_series - as_period_series
132+
check(assert_type(offset_series, OffsetSeries), pd.Series)
133+
check(assert_type(p + offset_series, PeriodSeries), pd.Series, pd.Period)
123134
check(assert_type(p - as_pd_td, pd.Period), pd.Period)
124135
check(assert_type(p - as_dt_td, pd.Period), pd.Period)
125136
check(assert_type(p - as_np_td, pd.Period), pd.Period)
126137
check(assert_type(p - as_np_i64, pd.Period), pd.Period)
127138
check(assert_type(p - as_int, pd.Period), pd.Period)
128-
check(assert_type(p - as_period_index, pd.Index), pd.Index)
139+
check(assert_type(offset_index, pd.Index), pd.Index)
129140
check(assert_type(p - as_period, BaseOffset), Day)
130141
check(assert_type(p - as_td_series, PeriodSeries), pd.Series, pd.Period)
131142
check(assert_type(p - as_timedelta_idx, pd.PeriodIndex), pd.PeriodIndex)

0 commit comments

Comments
 (0)