Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions pandas-stubs/_libs/tslibs/period.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ from pandas._libs.tslibs import NaTType
from pandas._libs.tslibs.offsets import BaseOffset
from pandas._libs.tslibs.timestamps import Timestamp
from pandas._typing import (
Frequency,
ShapeT,
np_1darray,
np_ndarray,
Expand Down Expand Up @@ -65,7 +66,7 @@ class Period(PeriodMixin):
value: (
Period | str | datetime.datetime | datetime.date | Timestamp | None
) = ...,
freq: str | BaseOffset | None = ...,
freq: Frequency | None = ...,
ordinal: int | None = ...,
year: int | None = ...,
month: int | None = ...,
Expand Down Expand Up @@ -229,12 +230,12 @@ class Period(PeriodMixin):
def day_of_year(self) -> int: ...
@property
def day_of_week(self) -> int: ...
def asfreq(self, freq: str | BaseOffset, how: _PeriodFreqHow = "end") -> Period: ...
def asfreq(self, freq: Frequency, how: _PeriodFreqHow = "end") -> Period: ...
@classmethod
def now(cls, freq: str | BaseOffset = ...) -> Period: ...
def now(cls, freq: Frequency = ...) -> Period: ...
def strftime(self, fmt: str) -> str: ...
def to_timestamp(
self,
freq: str | BaseOffset | None = ...,
freq: Frequency | None = ...,
how: _PeriodToTimestampHow = "S",
) -> Timestamp: ...
8 changes: 4 additions & 4 deletions pandas-stubs/_libs/tslibs/timedeltas.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ from typing_extensions import (
)

from pandas._libs.tslibs import (
BaseOffset,
NaTType,
)
from pandas._libs.tslibs.period import Period
from pandas._libs.tslibs.timestamps import Timestamp
from pandas._typing import (
Frequency,
ShapeT,
TimeUnit,
np_1darray,
Expand Down Expand Up @@ -130,9 +130,9 @@ class Timedelta(timedelta):
@property
def asm8(self) -> np.timedelta64: ...
# TODO: round/floor/ceil could return NaT?
def round(self, freq: str | BaseOffset) -> Self: ...
def floor(self, freq: str | BaseOffset) -> Self: ...
def ceil(self, freq: str | BaseOffset) -> Self: ...
def round(self, freq: Frequency) -> Self: ...
def floor(self, freq: Frequency) -> Self: ...
def ceil(self, freq: Frequency) -> Self: ...
Comment on lines -131 to +135
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs say str but offset objects are also accepted, so I think it's ok to keep the current annotation

In [11]: pd.Timedelta(minutes=37).round(pd.tseries.offsets.Hour())
Out[11]: Timedelta('0 days 01:00:00')

@property
def resolution_string(self) -> str: ...
# Override due to more types supported than dt.timedelta
Expand Down
5 changes: 3 additions & 2 deletions pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ from pandas._typing import (
FillnaOptions,
FloatFormatType,
FormattersType,
Frequency,
GroupByObjectNonScalar,
HashableT,
HashableT1,
Expand Down Expand Up @@ -2341,7 +2342,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
@overload
def rolling(
self,
window: int | str | dt.timedelta | BaseOffset | BaseIndexer,
window: int | Frequency | dt.timedelta | BaseIndexer,
min_periods: int | None = ...,
center: _bool = ...,
on: Hashable | None = ...,
Expand All @@ -2355,7 +2356,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
@overload
def rolling(
self,
window: int | str | dt.timedelta | BaseOffset | BaseIndexer,
window: int | Frequency | dt.timedelta | BaseIndexer,
min_periods: int | None = ...,
center: _bool = ...,
on: Hashable | None = ...,
Expand Down
22 changes: 10 additions & 12 deletions pandas-stubs/core/indexes/accessors.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ from pandas.core.series import (
from typing_extensions import Never

from pandas._libs.tslibs import BaseOffset
from pandas._libs.tslibs.offsets import DateOffset
from pandas._libs.tslibs.period import Period
from pandas._typing import (
S1,
Frequency,
TimeAmbiguous,
TimeNonexistent,
TimestampConvention,
Expand Down Expand Up @@ -174,7 +174,7 @@ _DTTimestampTimedeltaReturnType = TypeVar(
class _DatetimeRoundingMethods(Generic[_DTTimestampTimedeltaReturnType]):
def round(
self,
freq: str | BaseOffset | None,
freq: Frequency | None,
ambiguous: Literal["raise", "infer", "NaT"] | bool | np_ndarray_bool = ...,
nonexistent: (
Literal["shift_forward", "shift_backward", "NaT", "raise"]
Expand All @@ -184,7 +184,7 @@ class _DatetimeRoundingMethods(Generic[_DTTimestampTimedeltaReturnType]):
) -> _DTTimestampTimedeltaReturnType: ...
def floor(
self,
freq: str | BaseOffset | None,
freq: Frequency | None,
ambiguous: Literal["raise", "infer", "NaT"] | bool | np_ndarray_bool = ...,
nonexistent: (
Literal["shift_forward", "shift_backward", "NaT", "raise"]
Expand All @@ -194,7 +194,7 @@ class _DatetimeRoundingMethods(Generic[_DTTimestampTimedeltaReturnType]):
) -> _DTTimestampTimedeltaReturnType: ...
def ceil(
self,
freq: str | BaseOffset | None,
freq: Frequency | None,
ambiguous: Literal["raise", "infer", "NaT"] | bool | np_ndarray_bool = ...,
nonexistent: (
Literal["shift_forward", "shift_backward", "NaT", "raise"]
Expand All @@ -220,9 +220,7 @@ class _DatetimeLikeNoTZMethods(
_DTToPeriodReturnType,
],
):
def to_period(
self, freq: str | BaseOffset | None = ...
) -> _DTToPeriodReturnType: ...
def to_period(self, freq: Frequency | None = ...) -> _DTToPeriodReturnType: ...
def tz_localize(
self,
tz: TimeZones,
Expand Down Expand Up @@ -353,12 +351,12 @@ class _PeriodProperties(
def strftime(self, date_format: str) -> _PeriodStrReturnTypes: ...
def to_timestamp(
self,
freq: str | DateOffset | None = ...,
freq: Frequency | None = ...,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay so here is a good example:

idx = pd.PeriodIndex(["2023", "2024", "2025"], freq="D")
s1 = pd.Series([1, 2, 3], index=idx)
s1.to_timestamp(freq=pd.tseries.offsets.BDay())

This will raise a FutureWarning:

<stdin>:1: FutureWarning: PeriodDtype[B] is deprecated and will be removed in a future version. Use a DatetimeIndex with freq='B' instead

The issue is that BDay is not a DateOffset. I think it is a bit of an edge case and this situation works at runtime for now but good to think about the future, I am good with letting the types a bit looser here, @Dr-Irv what do you think?

how: TimestampConvention = ...,
) -> _PeriodDTAReturnTypes: ...
def asfreq(
self,
freq: str | DateOffset | None = ...,
freq: Frequency | None = ...,
how: Literal["E", "END", "FINISH", "S", "START", "BEGIN"] = ...,
) -> _PeriodPAReturnTypes: ...

Expand Down Expand Up @@ -453,7 +451,7 @@ class _dtDescriptor(CombinedDatetimelikeProperties, Generic[S1]):
) -> CombinedDatetimelikeProperties: ...
def round(
self,
freq: str | BaseOffset | None,
freq: Frequency | None,
ambiguous: Literal["raise", "infer", "NaT"] | bool | np_ndarray_bool = ...,
nonexistent: (
Literal["shift_forward", "shift_backward", "NaT", "raise"]
Expand All @@ -463,7 +461,7 @@ class _dtDescriptor(CombinedDatetimelikeProperties, Generic[S1]):
) -> Series[S1]: ...
def floor(
self,
freq: str | BaseOffset | None,
freq: Frequency | None,
ambiguous: Literal["raise", "infer", "NaT"] | bool | np_ndarray_bool = ...,
nonexistent: (
Literal["shift_forward", "shift_backward", "NaT", "raise"]
Expand All @@ -473,7 +471,7 @@ class _dtDescriptor(CombinedDatetimelikeProperties, Generic[S1]):
) -> Series[S1]: ...
def ceil(
self,
freq: str | BaseOffset | None,
freq: Frequency | None,
ambiguous: Literal["raise", "infer", "NaT"] | bool | np_ndarray_bool = ...,
nonexistent: (
Literal["shift_forward", "shift_backward", "NaT", "raise"]
Expand Down
14 changes: 6 additions & 8 deletions pandas-stubs/core/indexes/datetimes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import numpy as np
from pandas import (
DataFrame,
Index,
Timedelta,
TimedeltaIndex,
Timestamp,
)
Expand All @@ -26,7 +25,6 @@ from pandas.core.indexes.datetimelike import DatetimeTimedeltaMixin
from pandas.core.series import Series
from typing_extensions import Self

from pandas._libs.tslibs.offsets import DateOffset
from pandas._typing import (
AxesData,
DateAndDatetimeLike,
Expand Down Expand Up @@ -103,14 +101,14 @@ class DatetimeIndex(
@property
def dtype(self) -> np.dtype | DatetimeTZDtype: ...
def shift(
self, periods: int = 1, freq: DateOffset | Timedelta | str | None = None
self, periods: int = 1, freq: Frequency | timedelta | None = None
) -> Self: ...

@overload
def date_range(
start: str | DateAndDatetimeLike,
end: str | DateAndDatetimeLike,
freq: str | timedelta | Timedelta | BaseOffset | None = None,
freq: Frequency | timedelta | None = None,
tz: TimeZones = None,
normalize: bool = False,
name: Hashable | None = None,
Expand All @@ -133,7 +131,7 @@ def date_range(
start: str | DateAndDatetimeLike,
*,
periods: int,
freq: str | timedelta | Timedelta | BaseOffset | None = None,
freq: Frequency | timedelta | None = None,
tz: TimeZones = None,
normalize: bool = False,
name: Hashable | None = None,
Expand All @@ -145,7 +143,7 @@ def date_range(
*,
end: str | DateAndDatetimeLike,
periods: int,
freq: str | timedelta | Timedelta | BaseOffset | None = None,
freq: Frequency | timedelta | None = None,
tz: TimeZones = None,
normalize: bool = False,
name: Hashable | None = None,
Expand All @@ -157,7 +155,7 @@ def bdate_range(
start: str | DateAndDatetimeLike | None = ...,
end: str | DateAndDatetimeLike | None = ...,
periods: int | None = ...,
freq: str | timedelta | Timedelta | BaseOffset = ...,
freq: Frequency | timedelta = ...,
tz: TimeZones = ...,
normalize: bool = ...,
name: Hashable | None = ...,
Expand All @@ -171,7 +169,7 @@ def bdate_range(
end: str | DateAndDatetimeLike | None = ...,
periods: int | None = ...,
*,
freq: str | timedelta | Timedelta | BaseOffset,
freq: Frequency | timedelta,
tz: TimeZones = ...,
normalize: bool = ...,
name: Hashable | None = ...,
Expand Down
10 changes: 5 additions & 5 deletions pandas-stubs/core/indexes/interval.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ from pandas._libs.interval import (
Interval as Interval,
IntervalMixin,
)
from pandas._libs.tslibs.offsets import BaseOffset
from pandas._typing import (
DatetimeLike,
DtypeArg,
FillnaOptions,
Frequency,
IntervalClosedType,
IntervalT,
Label,
Expand Down Expand Up @@ -328,7 +328,7 @@ def interval_range(
start: _TimestampLike,
end: _TimestampLike | None = ...,
periods: int | None = ...,
freq: str | BaseOffset | pd.Timedelta | dt.timedelta | None = ...,
freq: Frequency | dt.timedelta | None = ...,
name: Hashable = ...,
closed: IntervalClosedType = ...,
) -> IntervalIndex[Interval[pd.Timestamp]]: ...
Expand All @@ -338,7 +338,7 @@ def interval_range(
start: None = ...,
end: _TimestampLike,
periods: int | None = ...,
freq: str | BaseOffset | pd.Timedelta | dt.timedelta | None = ...,
freq: Frequency | dt.timedelta | None = ...,
name: Hashable = ...,
closed: IntervalClosedType = ...,
) -> IntervalIndex[Interval[pd.Timestamp]]: ...
Expand All @@ -347,7 +347,7 @@ def interval_range(
start: _TimedeltaLike,
end: _TimedeltaLike | None = ...,
periods: int | None = ...,
freq: str | BaseOffset | pd.Timedelta | dt.timedelta | None = ...,
freq: Frequency | dt.timedelta | None = ...,
name: Hashable = ...,
closed: IntervalClosedType = ...,
) -> IntervalIndex[Interval[pd.Timedelta]]: ...
Expand All @@ -357,7 +357,7 @@ def interval_range(
start: None = ...,
end: _TimedeltaLike,
periods: int | None = ...,
freq: str | BaseOffset | pd.Timedelta | dt.timedelta | None = ...,
freq: Frequency | dt.timedelta | None = ...,
name: Hashable = ...,
closed: IntervalClosedType = ...,
) -> IntervalIndex[Interval[pd.Timedelta]]: ...
15 changes: 8 additions & 7 deletions pandas-stubs/core/indexes/timedeltas.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ from typing import (

import numpy as np
from pandas import (
DateOffset,
Index,
Period,
)
Expand All @@ -23,9 +22,9 @@ from pandas.core.series import Series
from typing_extensions import Self

from pandas._libs import Timedelta
from pandas._libs.tslibs import BaseOffset
from pandas._typing import (
AxesData,
Frequency,
TimedeltaConvertibleTypes,
np_ndarray_td,
num,
Expand All @@ -39,7 +38,7 @@ class TimedeltaIndex(
data: (
Sequence[dt.timedelta | Timedelta | np.timedelta64 | float] | AxesData
) = ...,
freq: str | BaseOffset = ...,
freq: Frequency = ...,
closed: object = ...,
dtype: Literal["<m8[ns]"] = ...,
copy: bool = ...,
Expand Down Expand Up @@ -86,14 +85,16 @@ class TimedeltaIndex(
def inferred_type(self) -> str: ...
@final
def to_series(self, index=..., name: Hashable = ...) -> Series[Timedelta]: ...
def shift(self, periods: int = 1, freq=...) -> Self: ...
def shift(
self, periods: int = 1, freq: Frequency | dt.timedelta | None = None
) -> Self: ...

@overload
def timedelta_range(
start: TimedeltaConvertibleTypes,
end: TimedeltaConvertibleTypes,
*,
freq: str | DateOffset | Timedelta | dt.timedelta | None = None,
freq: Frequency | Timedelta | dt.timedelta | None = None,
name: Hashable | None = None,
closed: Literal["left", "right"] | None = None,
unit: None | str = ...,
Expand All @@ -103,7 +104,7 @@ def timedelta_range(
*,
end: TimedeltaConvertibleTypes,
periods: int,
freq: str | DateOffset | Timedelta | dt.timedelta | None = None,
freq: Frequency | Timedelta | dt.timedelta | None = None,
name: Hashable | None = None,
closed: Literal["left", "right"] | None = None,
unit: None | str = ...,
Expand All @@ -113,7 +114,7 @@ def timedelta_range(
start: TimedeltaConvertibleTypes,
*,
periods: int,
freq: str | DateOffset | Timedelta | dt.timedelta | None = None,
freq: Frequency | Timedelta | dt.timedelta | None = None,
name: Hashable | None = None,
closed: Literal["left", "right"] | None = None,
unit: None | str = ...,
Expand Down
5 changes: 3 additions & 2 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ from pandas._typing import (
FillnaOptions,
FloatDtypeArg,
FloatFormatType,
Frequency,
GenericT,
GenericT_co,
GroupByObjectNonScalar,
Expand Down Expand Up @@ -4520,7 +4521,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
@overload
def rolling(
self,
window: int | _str | timedelta | BaseOffset | BaseIndexer,
window: int | Frequency | timedelta | BaseIndexer,
min_periods: int | None = ...,
center: _bool = ...,
on: _str | None = ...,
Expand All @@ -4533,7 +4534,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
@overload
def rolling(
self,
window: int | _str | timedelta | BaseOffset | BaseIndexer,
window: int | Frequency | timedelta | BaseIndexer,
min_periods: int | None = ...,
center: _bool = ...,
on: _str | None = ...,
Expand Down