pandas-dev / pandas-stubs

Public type stubs for pandas
BSD 3-Clause "New" or "Revised" License
233 stars 123 forks source link

`pd.Timedelta` should also accept `float` values #1015

Open 5j9 opened 2 weeks ago

5j9 commented 2 weeks ago

The value parameter of Timedelta currently has the following type-hint: https://github.com/pandas-dev/pandas-stubs/blob/f71224c6211ff436424be614ba47f494f64c4618/pandas-stubs/_libs/tslibs/timedeltas.pyi#L105

I think it should also accept float, so that type checkers won't complain on following code snippet:

import pandas as pd
pd.Timedelta(3.5, 'hours')
Argument of type "float" cannot be assigned to parameter "value" of type "str | int | Timedelta | timedelta | timedelta64" in function "__new__"
  Type "float" is not assignable to type "str | int | Timedelta | timedelta | timedelta64"
    "float" is not assignable to "str"
    "float" is not assignable to "int"
    "float" is not assignable to "Timedelta"
    "float" is not assignable to "timedelta"
    "float" is not assignable to "timedelta64"
Pylance[reportArgumentType](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportArgumentType)
Dr-Irv commented 2 weeks ago

Thanks for the report. The docs at https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Timedelta.html#pandas.Timedelta have the same issue, so can you raise an issue there as well?

PR with tests welcome.

5j9 commented 2 weeks ago

https://github.com/pandas-dev/pandas/issues/60044