pandas icon indicating copy to clipboard operation
pandas copied to clipboard

DOC: The `value` parameter of `pandas.Timedelta` can also accept float

Open 5j9 opened this issue 1 year ago • 5 comments

Pandas version checks

  • [X] I have checked that the issue still exists on the latest versions of the docs on main here

Location of the documentation

https://pandas.pydata.org/docs/dev/reference/api/pandas.Timedelta.html#pandas.Timedelta

Documentation problem

It currently states:

https://github.com/pandas-dev/pandas/blob/2a10e04a099d5f1633abcdfbb2dd9fdf09142f8d/pandas/_libs/tslibs/timedeltas.pyx#L1867

There is no float.

Suggested fix for documentation

Add float, to accepted types.

There already exists a code branch for float values so this should not be a problem: https://github.com/pandas-dev/pandas/blob/2a10e04a099d5f1633abcdfbb2dd9fdf09142f8d/pandas/_libs/tslibs/timedeltas.pyx#L2056

5j9 avatar Oct 15 '24 01:10 5j9

Hello! I tested this method and I found that although pd.Timedelta can accept float values numbers, the result only takes the integer part. For example

print(pd.Timedelta(3651015))
0 days 00:00:00.003651015
print(pd.Timedelta(365.1015))
0 days 00:00:00.000000365

ZKaoChi avatar Oct 15 '24 02:10 ZKaoChi

@ZKaoChi Hi! Thanks for looking into this.

The default unit value is ns, and I think that's the lowest resolution supported. So that might be expected for the default ns unit. But if you pass any other unit, the fractional part will matter:

In [11]: pd.Timedelta(3.5, 'hours')
Out[11]: Timedelta('0 days 03:30:00')

In [12]: pd.Timedelta(3, 'hours')
Out[12]: Timedelta('0 days 03:00:00')

In [15]: pd.Timedelta(3, 'W')
Out[15]: Timedelta('21 days 00:00:00')

In [16]: pd.Timedelta(3.5, 'W')
Out[16]: Timedelta('24 days 12:00:00')

In [17]: pd.Timedelta(3.5, 'microseconds')
Out[17]: Timedelta('0 days 00:00:00.000003500')

In [18]: pd.Timedelta(3, 'microseconds')
Out[18]: Timedelta('0 days 00:00:00.000003')

5j9 avatar Oct 15 '24 03:10 5j9

Agreed. datetime.timedelta also accepts float, and Timedelta is the pandas equivalent of it.

yuanx749 avatar Oct 15 '24 04:10 yuanx749

@5j9 Thanks for your correction! It was my oversight not to notice the units of the data.

ZKaoChi avatar Oct 16 '24 10:10 ZKaoChi

I'm new and I wanna solve this issue, but my pre-commit.ci - pr was failed because of cython-lint. Coule anyone please teache me how to deal with it?

ZKaoChi avatar Oct 16 '24 12:10 ZKaoChi