diff --git a/pandas-stubs/_libs/tslibs/offsets.pyi b/pandas-stubs/_libs/tslibs/offsets.pyi index 4b6a61230..6ac91ce88 100644 --- a/pandas-stubs/_libs/tslibs/offsets.pyi +++ b/pandas-stubs/_libs/tslibs/offsets.pyi @@ -103,11 +103,6 @@ class BaseOffset: class SingleConstructorOffset(BaseOffset): def __reduce__(self): ... -@overload -def to_offset(freq: None) -> None: ... -@overload -def to_offset(freq: timedelta | BaseOffset | str) -> BaseOffset: ... - class Tick(SingleConstructorOffset): def __init__(self, n: int = ..., normalize: bool = ...) -> None: ... @property diff --git a/pandas-stubs/tseries/frequencies.pyi b/pandas-stubs/tseries/frequencies.pyi index 199cb1443..d65f096f7 100644 --- a/pandas-stubs/tseries/frequencies.pyi +++ b/pandas-stubs/tseries/frequencies.pyi @@ -1,12 +1,19 @@ +from typing import overload + from pandas import ( DatetimeIndex, Series, TimedeltaIndex, ) -from pandas.tseries.offsets import DateOffset as DateOffset +from pandas._typing import Frequency + +from pandas.tseries.offsets import DateOffset def get_period_alias(offset_str: str) -> str | None: ... -def to_offset(freq) -> DateOffset | None: ... +@overload +def to_offset(freq: None) -> None: ... +@overload +def to_offset(freq: Frequency) -> DateOffset: ... def get_offset(name: str) -> DateOffset: ... def infer_freq(index: Series | DatetimeIndex | TimedeltaIndex) -> str | None: ... diff --git a/tests/test_timefuncs.py b/tests/test_timefuncs.py index 8f9424713..9cc22006f 100644 --- a/tests/test_timefuncs.py +++ b/tests/test_timefuncs.py @@ -28,7 +28,9 @@ from pandas._libs import NaTType from pandas._libs.tslibs import BaseOffset -from pandas._libs.tslibs.offsets import DateOffset + +from pandas.tseries.frequencies import to_offset +from pandas.tseries.offsets import DateOffset if TYPE_CHECKING: from pandas._typing import FulldatetimeDict @@ -1425,3 +1427,8 @@ def test_DatetimeIndex_sub_timedelta() -> None: ), pd.DatetimeIndex, ) + + +def test_to_offset() -> None: + check(assert_type(to_offset(None), None), type(None)) + check(assert_type(to_offset("1D"), DateOffset), DateOffset)