Skip to content

Commit 8ea5cf3

Browse files
committed
ENH: Improve list of how values
1 parent 579526d commit 8ea5cf3

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

pandas-stubs/_libs/tslibs/period.pyi

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,24 @@ _PeriodAddSub: TypeAlias = Union[
3232
_PeriodFreqHow: TypeAlias = Literal[
3333
"S",
3434
"E",
35-
"Start",
36-
"Finish",
37-
"Begin",
38-
"End",
39-
"s",
40-
"e",
4135
"start",
42-
"finish",
43-
"begin",
4436
"end",
4537
]
4638

39+
_PeriodToTimestampHow: TypeAlias = Union[
40+
_PeriodFreqHow,
41+
Literal[
42+
"Start",
43+
"Finish",
44+
"Begin",
45+
"End",
46+
"s",
47+
"e",
48+
"finish",
49+
"begin",
50+
],
51+
]
52+
4753
class PeriodMixin:
4854
@property
4955
def end_time(self) -> Timestamp: ...
@@ -72,6 +78,7 @@ class Period(PeriodMixin):
7278
def __sub__(self, other: PeriodIndex) -> Index: ...
7379
@overload
7480
def __sub__(self, other: TimedeltaSeries) -> PeriodSeries: ...
81+
def __rsub__(self, other: PeriodIndex) -> Index: ...
7582
@overload
7683
def __add__(self, other: _PeriodAddSub) -> Period: ...
7784
@overload
@@ -168,5 +175,5 @@ class Period(PeriodMixin):
168175
def to_timestamp(
169176
self,
170177
freq: str | BaseOffset | None = ...,
171-
how: _PeriodFreqHow = ...,
178+
how: _PeriodToTimestampHow = ...,
172179
) -> Timestamp: ...

tests/test_scalars.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,8 @@ def test_period() -> None:
117117
check(assert_type(as4 + p, pd.Period), pd.Period)
118118
check(assert_type(as7 + p, "PeriodSeries"), pd.Series)
119119
check(assert_type(p.freq + p, pd.Period), pd.Period)
120-
# TODO: PeriodIndex should have a __sub__ with correct types, this op is valid
121-
# and so the assert_type is skipped
122-
check(as5 - p, pd.Index) # type: ignore[operator]
120+
121+
check(assert_type(as5 - p, pd.Index), pd.Index)
123122

124123
check(assert_type(p.__radd__(as0), pd.Period), pd.Period)
125124
check(assert_type(p.__radd__(as1), pd.Period), pd.Period)
@@ -164,23 +163,20 @@ def test_period() -> None:
164163
p3 = pd.Period("2007-01", freq="M")
165164
check(assert_type(p3.to_timestamp("D", "S"), pd.Timestamp), pd.Timestamp)
166165
check(assert_type(p3.to_timestamp("D", "E"), pd.Timestamp), pd.Timestamp)
166+
check(assert_type(p3.to_timestamp("D", "start"), pd.Timestamp), pd.Timestamp)
167+
check(assert_type(p3.to_timestamp("D", "end"), pd.Timestamp), pd.Timestamp)
167168
check(assert_type(p3.to_timestamp("D", "Finish"), pd.Timestamp), pd.Timestamp)
168-
check(assert_type(p3.to_timestamp("D", "End"), pd.Timestamp), pd.Timestamp)
169169
check(assert_type(p3.to_timestamp("D", "Begin"), pd.Timestamp), pd.Timestamp)
170-
check(assert_type(p3.to_timestamp("D", "Start"), pd.Timestamp), pd.Timestamp)
170+
check(assert_type(p3.to_timestamp("D", "End"), pd.Timestamp), pd.Timestamp)
171+
check(assert_type(p3.to_timestamp("D", "s"), pd.Timestamp), pd.Timestamp)
172+
check(assert_type(p3.to_timestamp("D", "e"), pd.Timestamp), pd.Timestamp)
173+
check(assert_type(p3.to_timestamp("D", "finish"), pd.Timestamp), pd.Timestamp)
174+
check(assert_type(p3.to_timestamp("D", "begin"), pd.Timestamp), pd.Timestamp)
171175

172176
check(assert_type(p3.asfreq("D", "S"), pd.Period), pd.Period)
173177
check(assert_type(p3.asfreq(Day(), "E"), pd.Period), pd.Period)
174-
check(assert_type(p3.asfreq(Day(), "Finish"), pd.Period), pd.Period)
175-
check(assert_type(p3.asfreq(Day(), "Begin"), pd.Period), pd.Period)
176-
check(assert_type(p3.asfreq(Day(), "Start"), pd.Period), pd.Period)
177-
check(assert_type(p3.asfreq(Day(), "End"), pd.Period), pd.Period)
178178
check(assert_type(p3.asfreq(Day(), "end"), pd.Period), pd.Period)
179179
check(assert_type(p3.asfreq(Day(), "start"), pd.Period), pd.Period)
180-
check(assert_type(p3.asfreq(Day(), "begin"), pd.Period), pd.Period)
181-
check(assert_type(p3.asfreq(Day(), "finish"), pd.Period), pd.Period)
182-
check(assert_type(p3.asfreq(Day(), "s"), pd.Period), pd.Period)
183-
check(assert_type(p3.asfreq(Day(), "e"), pd.Period), pd.Period)
184180

185181
check(assert_type(pd.Period.now("D"), pd.Period), pd.Period)
186182
check(assert_type(pd.Period.now(Day()), pd.Period), pd.Period)

0 commit comments

Comments
 (0)