Skip to content

pandas.tseries.frequencies.to_offset should accept arguments of type datetime.timedelta #1705

@AguileraG

Description

@AguileraG

Describe the bug
The documentation for pandas.tseries.frequencies.to_offset mentions that the parameter freq can be an instance of datetime.timedelta.

However, the declarations of pandas.tseries.frequencies.to_offset found in frequencies.pyi only considers that freq can be None or Frequency, which do not include datetime.timedelta.

To Reproduce
The following example can be used to reproduce this bug:

import datetime

from pandas.tseries.frequencies import to_offset

delta = datetime.timedelta(minutes=60)
delta_string = "60min"

offset = to_offset(delta)
offset_string = to_offset(delta_string)

assert offset == offset_string

Both mypy and pyright report an error on line 8, even though no exceptions are raised at runtime and the assert expression does not fail:

$ mypy example.py
example.py:8: error: No overload variant of "to_offset" matches argument type "timedelta"  [call-overload]
example.py:8: note: Possible overload variants:
example.py:8: note:     def to_offset(freq: None, is_period: bool = ...) -> None
example.py:8: note:     def to_offset(freq: str | BaseOffset, is_period: bool = ...) -> DateOffset
Found 1 error in 1 file (checked 1 source file)
$ pyright example.py
WARNING: there is a new pyright version available (v1.1.406 -> v1.1.408).
Please install the new version or set PYRIGHT_PYTHON_FORCE_VERSION to `latest`

/home/user/example.py
  /home/user/example.py:8:10 - error: No overloads for "to_offset" match the provided arguments (reportCallIssue)
  /home/user/example.py:8:20 - error: Argument of type "timedelta" cannot be assigned to parameter "freq" of type "Frequency" in function "to_offset"
    Type "timedelta" is not assignable to type "Frequency"
      "timedelta" is not assignable to "str"
      "timedelta" is not assignable to "BaseOffset" (reportArgumentType)
2 errors, 0 warnings, 0 informations

Please complete the following information:

  • OS: Linux running on WSL2 (Windows 11 Pro 25H2)
  • OS Version: Debian 13
  • Python 3.13.5
  • Pyright 1.1.406, Mypy 1.18.2
  • pandas-stubs v2.3.2.250926

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions