Skip to content

Commit 7af641f

Browse files
committed
use PeriodFrequency
1 parent a47ead6 commit 7af641f

File tree

10 files changed

+54
-27
lines changed

10 files changed

+54
-27
lines changed

pandas-stubs/_libs/tslibs/period.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ from typing_extensions import TypeAlias
1717
from pandas._libs.tslibs import NaTType
1818
from pandas._libs.tslibs.offsets import (
1919
BaseOffset,
20-
DateOffset,
2120
)
2221
from pandas._libs.tslibs.timestamps import Timestamp
2322
from pandas._typing import (
23+
PeriodFrequency,
2424
ShapeT,
2525
np_1darray,
2626
np_ndarray,
@@ -65,7 +65,7 @@ class Period(PeriodMixin):
6565
value: (
6666
Period | str | datetime.datetime | datetime.date | Timestamp | None
6767
) = ...,
68-
freq: str | DateOffset | None = ...,
68+
freq: PeriodFrequency | None = None,
6969
ordinal: int | None = ...,
7070
year: int | None = ...,
7171
month: int | None = ...,
@@ -229,12 +229,12 @@ class Period(PeriodMixin):
229229
def day_of_year(self) -> int: ...
230230
@property
231231
def day_of_week(self) -> int: ...
232-
def asfreq(self, freq: str | DateOffset, how: _PeriodFreqHow = "end") -> Period: ...
232+
def asfreq(self, freq: PeriodFrequency, how: _PeriodFreqHow = "end") -> Period: ...
233233
@classmethod
234-
def now(cls, freq: str | DateOffset | None = None) -> Period: ...
234+
def now(cls, freq: PeriodFrequency | None = None) -> Period: ...
235235
def strftime(self, fmt: str) -> str: ...
236236
def to_timestamp(
237237
self,
238-
freq: str | DateOffset | None = ...,
238+
freq: PeriodFrequency | None = None,
239239
how: _PeriodToTimestampHow = "S",
240240
) -> Timestamp: ...

pandas-stubs/_libs/tslibs/timestamps.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ from pandas._libs.tslibs import (
3434
Tick,
3535
Timedelta,
3636
)
37-
from pandas._libs.tslibs.offsets import DateOffset
3837
from pandas._typing import (
38+
PeriodFrequency,
3939
ShapeT,
4040
TimestampNonexistent,
4141
TimeUnit,
@@ -282,7 +282,7 @@ class Timestamp(datetime, SupportsIndex):
282282
def is_year_end(self) -> bool: ...
283283
def to_pydatetime(self, warn: bool = ...) -> datetime: ...
284284
def to_datetime64(self) -> np.datetime64: ...
285-
def to_period(self, freq: DateOffset | str | None = ...) -> Period: ...
285+
def to_period(self, freq: PeriodFrequency | None = ...) -> Period: ...
286286
def to_julian_date(self) -> np.float64: ...
287287
@property
288288
def asm8(self) -> np.datetime64: ...

pandas-stubs/_typing.pyi

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ from pandas.core.dtypes.dtypes import (
5656
)
5757

5858
from pandas.io.formats.format import EngFormatter
59+
from pandas.tseries.offsets import (
60+
Day,
61+
Hour,
62+
Micro,
63+
Milli,
64+
Minute,
65+
MonthEnd,
66+
Nano,
67+
QuarterEnd,
68+
Second,
69+
Week,
70+
YearEnd,
71+
)
5972

6073
P = ParamSpec("P")
6174

@@ -162,6 +175,20 @@ Suffixes: TypeAlias = tuple[str | None, str | None] | list[str | None]
162175
Ordered: TypeAlias = bool | None
163176
JSONSerializable: TypeAlias = PythonScalar | list | dict
164177
Frequency: TypeAlias = str | BaseOffset
178+
PeriodFrequency: TypeAlias = (
179+
str
180+
| Day
181+
| Hour
182+
| Minute
183+
| Second
184+
| Milli
185+
| Micro
186+
| Nano
187+
| YearEnd
188+
| QuarterEnd
189+
| MonthEnd
190+
| Week
191+
)
165192
Axes: TypeAlias = ListLike
166193

167194
RandomState: TypeAlias = (

pandas-stubs/core/arrays/period.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ from pandas.core.arrays.datetimelike import (
66
)
77

88
from pandas._libs.tslibs import Timestamp
9-
from pandas._libs.tslibs.offsets import DateOffset
109
from pandas._libs.tslibs.period import Period
10+
from pandas._typing import PeriodFrequency
1111

1212
class PeriodArray(DatetimeLikeArrayMixin, DatelikeOps):
1313
__array_priority__: int = ...
@@ -39,7 +39,7 @@ class PeriodArray(DatetimeLikeArrayMixin, DatelikeOps):
3939
@property
4040
def end_time(self) -> Timestamp: ...
4141
def to_timestamp(
42-
self, freq: str | DateOffset | None = None, how: str = ...
42+
self, freq: PeriodFrequency | None = None, how: str = ...
4343
) -> Timestamp: ...
4444
def asfreq(self, freq: str | None = ..., how: str = "E") -> Period: ...
4545
def astype(self, dtype, copy: bool = True): ...

pandas-stubs/core/frame.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ from pandas._libs.lib import _NoDefaultDoNotUse
8181
from pandas._libs.missing import NAType
8282
from pandas._libs.tslibs import BaseOffset
8383
from pandas._libs.tslibs.nattype import NaTType
84-
from pandas._libs.tslibs.offsets import DateOffset
8584
from pandas._typing import (
8685
S2,
8786
AggFuncTypeBase,
@@ -136,6 +135,7 @@ from pandas._typing import (
136135
NDFrameT,
137136
NsmallestNlargestKeep,
138137
ParquetEngine,
138+
PeriodFrequency,
139139
QuantileInterpolation,
140140
RandomState,
141141
ReadBuffer,
@@ -1684,14 +1684,14 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
16841684
) -> Self: ...
16851685
def to_timestamp(
16861686
self,
1687-
freq: str | DateOffset | None = None,
1687+
freq: PeriodFrequency | None = None,
16881688
how: ToTimestampHow = ...,
16891689
axis: Axis = 0,
16901690
copy: _bool = True,
16911691
) -> Self: ...
16921692
def to_period(
16931693
self,
1694-
freq: str | DateOffset | None = None,
1694+
freq: PeriodFrequency | None = None,
16951695
axis: Axis = 0,
16961696
copy: _bool = True,
16971697
) -> Self: ...
@@ -2225,7 +2225,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
22252225
self,
22262226
periods: int = 1,
22272227
fill_method: None = None,
2228-
freq: DateOffset | dt.timedelta | _str | None = ...,
2228+
freq: Frequency | dt.timedelta | None = ...,
22292229
fill_value: Scalar | NAType | None = ...,
22302230
) -> Self: ...
22312231
def pop(self, item: _str) -> Series: ...

pandas-stubs/core/indexes/accessors.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ from typing_extensions import Never
3434

3535
from pandas._libs.interval import Interval
3636
from pandas._libs.tslibs import BaseOffset
37-
from pandas._libs.tslibs.offsets import DateOffset
3837
from pandas._libs.tslibs.period import Period
3938
from pandas._libs.tslibs.timedeltas import Timedelta
4039
from pandas._libs.tslibs.timestamps import Timestamp
4140
from pandas._typing import (
4241
Frequency,
42+
PeriodFrequency,
4343
TimeAmbiguous,
4444
TimeNonexistent,
4545
TimestampConvention,
@@ -226,7 +226,7 @@ class _DatetimeLikeNoTZMethods(
226226
],
227227
):
228228
def to_period(
229-
self, freq: str | DateOffset | None = ...
229+
self, freq: PeriodFrequency | None = None
230230
) -> _DTToPeriodReturnType: ...
231231
def tz_localize(
232232
self,
@@ -358,12 +358,12 @@ class _PeriodProperties(
358358
def strftime(self, date_format: str) -> _PeriodStrReturnTypes: ...
359359
def to_timestamp(
360360
self,
361-
freq: str | DateOffset | None = ...,
361+
freq: PeriodFrequency | None = None,
362362
how: TimestampConvention = ...,
363363
) -> _PeriodDTAReturnTypes: ...
364364
def asfreq(
365365
self,
366-
freq: str | DateOffset | None = ...,
366+
freq: PeriodFrequency | None = None,
367367
how: Literal["E", "END", "FINISH", "S", "START", "BEGIN"] = ...,
368368
) -> _PeriodPAReturnTypes: ...
369369

pandas-stubs/core/series.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ from pandas._libs.lib import _NoDefaultDoNotUse
108108
from pandas._libs.missing import NAType
109109
from pandas._libs.tslibs import BaseOffset
110110
from pandas._libs.tslibs.nattype import NaTType
111-
from pandas._libs.tslibs.offsets import DateOffset
112111
from pandas._typing import (
113112
S1,
114113
S1_CO,
@@ -167,6 +166,7 @@ from pandas._typing import (
167166
NaPosition,
168167
NsmallestNlargestKeep,
169168
ObjectDtypeArg,
169+
PeriodFrequency,
170170
QuantileInterpolation,
171171
RandomState,
172172
ReindexMethod,
@@ -1237,12 +1237,12 @@ class Series(IndexOpsMixin[S1], NDFrame):
12371237
) -> Series[S1]: ...
12381238
def to_timestamp(
12391239
self,
1240-
freq: str | DateOffset | None = None,
1240+
freq: PeriodFrequency | None = None,
12411241
how: ToTimestampHow = "start",
12421242
copy: _bool = True,
12431243
) -> Series[S1]: ...
12441244
def to_period(
1245-
self, freq: _str | DateOffset | None = None, copy: _bool = True
1245+
self, freq: PeriodFrequency | None = None, copy: _bool = True
12461246
) -> DataFrame: ...
12471247
@property
12481248
def str(
@@ -1496,7 +1496,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
14961496
@final
14971497
def asfreq(
14981498
self,
1499-
freq: DateOffset | _str,
1499+
freq: Frequency,
15001500
method: FillnaOptions | None = None,
15011501
how: Literal["start", "end"] | None = None,
15021502
normalize: _bool = False,
@@ -1646,7 +1646,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
16461646
self,
16471647
periods: int = 1,
16481648
fill_method: None = None,
1649-
freq: DateOffset | timedelta | _str | None = None,
1649+
freq: Frequency | timedelta | None = None,
16501650
) -> Series[float]: ...
16511651
@final
16521652
def first_valid_index(self) -> Scalar: ...

pandas-stubs/tseries/frequencies.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ from pandas import (
88

99
from pandas._typing import Frequency
1010

11-
from pandas.tseries.offsets import DateOffset
11+
from pandas.tseries.offsets import BaseOffset
1212

1313
@overload
1414
def to_offset(freq: None, is_period: bool = ...) -> None: ...
1515
@overload
16-
def to_offset(freq: Frequency, is_period: bool = ...) -> DateOffset: ...
16+
def to_offset(freq: Frequency, is_period: bool = ...) -> BaseOffset: ...
1717
def infer_freq(index: Series | DatetimeIndex | TimedeltaIndex) -> str | None: ...

tests/test_timefuncs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1911,7 +1911,7 @@ def test_DatetimeIndex_sub_timedelta() -> None:
19111911

19121912
def test_to_offset() -> None:
19131913
check(assert_type(to_offset(None), None), type(None))
1914-
check(assert_type(to_offset("1D"), DateOffset), DateOffset)
1914+
check(assert_type(to_offset("1D"), BaseOffset), BaseOffset)
19151915

19161916

19171917
def test_timestamp_sub_series() -> None:

tests/test_windowing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
)
1515
from typing_extensions import assert_type
1616

17-
from pandas._libs.tslibs.offsets import DateOffset
17+
from pandas._libs.tslibs.offsets import BaseOffset
1818

1919
from tests import (
2020
PD_LTE_23,
@@ -58,7 +58,7 @@ def test_rolling_basic_math() -> None:
5858

5959
def test_rolling_datetime_index() -> None:
6060
offset_1d = to_offset("1D")
61-
check(assert_type(offset_1d, DateOffset), DateOffset)
61+
check(assert_type(offset_1d, BaseOffset), BaseOffset)
6262

6363
check(assert_type(DF_DTI.rolling("1D"), "Rolling[DataFrame]"), Rolling, DataFrame)
6464
check(

0 commit comments

Comments
 (0)