Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] 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
import pandas as pd
starttime = pd.Series(["2145-11-02 06:00:00"]).astype("datetime64[ns]")
endtime = pd.Series(["2145-11-02 07:06:00"]).astype("datetime64[ns]")
diff = endtime - starttime
assert diff.values.item() == 3960000000000
a = (endtime - starttime).dt.total_seconds().values
b = (endtime - starttime).values.astype(int) / 1_000_000_000
c = (endtime - starttime).values / np.timedelta64(1, "s")
assert b == c, f"{c-b}" # ✔
assert a == c, f"{a-c}" # ✘ AssertionError: [4.54747351e-13]
Issue Description
I noticed this when I was trying to reproduce a preprocessing pipeline for some dataset. (Don't mind the weird dates, they just come from some de-identified data).
It seems that dt.total_seconds
yields a too large value, probably due to a rounding issue.
In this example,
starttime = 5_548_888_800_000_000_000
endtime = 5_548_892_760_000_000_000
diff = 3_960_000_000_000
Since 1_000_000_000
divides the diff, the result should be precisely 3960
seconds, which is exactly representable as a float, however the dt.total_seconds
seems to accidentally round up:
np.set_printoptions(100)
print(np.frexp(a)) # 0.9667968750000001
print(np.frexp(b)) # 0.966796875
print(np.frexp(c)) # 0.966796875
However, curiously:
endtime= pd.Timestamp("2145-11-02 07:06:00")
starttime = pd.Timestamp("2145-11-02 06:00:00")
np.frexp( (endtime - starttime).total_seconds() ) # 0.966796875
So the issue might be related to the .dt
?
Expected Behavior
It should agree with the numpy result.
Installed Versions
Comment From: randolf-scholz
Potentially relevant code sections:
-
https://github.com/pandas-dev/pandas/blob/54347fe684e0f7844bf407b1fb958a5269646825/pandas/core/arrays/timedeltas.py#L771-L772
-
https://github.com/pandas-dev/pandas/blob/6dc589b957e494750351e8572b45b8fa12c27d01/pandas/_libs/tslibs/dtypes.pyx#L393-L394
-
https://github.com/pandas-dev/pandas/blob/62a69beddbedde349891378992c902c0b9341a9f/pandas/_libs/tslibs/np_datetime.pyx#L515-L550
Comment From: seljaks
Hi @randolf-scholz, I ran your example in 1.4.4
and got the same error. However it didn't appear in 1.5.0
or on the main branch, so it would appear this bug has been fixed.
Comment From: MarcoGorelli
~Thanks for the report From git bisect, looks like this was fixed in #46828~ this doesn't look right, will take another look later
Looks like it was #47421
git bisect: https://www.kaggle.com/code/marcogorelli/pandas-regression?scriptVersionId=106198797
Comment From: MarcoGorelli
Looks like this is due to using 1_000_000_000
instead of 1e9
There is
https://github.com/jbrockmendel/pandas/blob/2f6116f259b88f9a6684fdf0ffe0e45078439602/pandas/core/arrays/timedeltas.py#L821-L823
which tests this, but still, probably better to have a proper test rather than relying on a doctest
@randolf-scholz fancy submitting a PR?
Comment From: MarcoGorelli
cc @jbrockmendel just FYI, as it looks like this was a welcome surprise