-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
BUG: inconsistent behaviour of 'to_timedelta' for float 'arg' and different 'unit' #56629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Another strange edge case, reported in #46819
but
Why is |
The problem should be in line 223–225 and 228 below: pandas/pandas/_libs/tslibs/conversion.pyx Lines 214 to 232 in 12d69c8
In fact
despite the fact that
I would say that the bug is the naive assumption that
give raise to the same results in decimal and binary floating point:
|
After some more experimentation I arrived at some preliminary remarks.
So actually we have here two separate issues. Point 1. could be considered a desing choice: it is open to discussion if other more coherent coherent behaviours (always round, always truncate) are worth the risk of breaking current code. Point 2. is clearly a bug, which is orthogonal to 1. However, it makes no sense to submit a PR for 2. alone, if also 1. has to be addressed. |
Ensure correct rounding, but still preserve truncation when conversion is from ns resolution to ns resolution.
Please see my POC implementation with correct rounding but preserving truncation in the ns → ns case: |
Uh oh!
There was an error while loading. Please reload this page.
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
to_timedelta
docs are not clear on how sub nanosecond precision should be rounded/truncated when differentunit
are used in the conversion.Expected Behavior
Equivalent definitions 1 of timedelta should give raise to the same result. Current behaviour seems to be
unit='ns'
This is very confusing, and can give raise to small glitches in the conversions.
Please note that this behaviour is not due to the fact that decimal literals have no “exact”
float
representation:still fails with
although there is no rounding in
2000 / 1024
and2 / 1024
.Please note also that truncating sub nanosecond precision is not consistent with
datetime.timedelta
which has µs resolution. In fact the docs clearly state thatAnalogoulsy
to_timedelta
and theTimedelta
constructor should always round to the nearest nanosecond using round-half-to-even tiebreaker whenfloat
args are used.Installed Versions
Footnotes
Here “equivalent“ is meant within
float
precision, in fact1.75 == 1.75e-3 * 1000
↩The text was updated successfully, but these errors were encountered: