Open 5j9 opened 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
@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')
Agreed. datetime.timedelta
also accepts float, and Timedelta
is the pandas equivalent of it.
@5j9 Thanks for your correction! It was my oversight not to notice the units of the data.
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?
Pandas version checks
main
hereLocation 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