pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.55k stars 17.9k forks source link

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

Open 5j9 opened 4 days ago

5j9 commented 4 days ago

Pandas version checks

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

ZKaoChi commented 4 days ago

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
5j9 commented 4 days ago

@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')
yuanx749 commented 3 days ago

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

ZKaoChi commented 2 days ago

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

ZKaoChi commented 2 days ago

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?