-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Timedelta.total_seconds method is returning wrong values in nanosecond intervals #46819
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
It`s an issue related to |
Seems that |
Thanks @Tiarles for the report.
first bad commit: [3b2c8f6] BUG: nonexistent Timestamp pre-summer/winter DST w/dateutil timezone (#31155) see #31155 (comment) and #31354 |
I don't see what we can do here, since we are relying on the CPython timedelta implementation which doesn't know nanoseconds. cdef _timedelta_from_value_and_reso(int64_t value, NPY_DATETIMEUNIT reso):
# Could make this a classmethod if/when cython supports cdef classmethods
cdef:
_Timedelta td_base
if reso == NPY_FR_ns:
td_base = _Timedelta.__new__(Timedelta, microseconds=int(value) // 1000)
elif reso == NPY_DATETIMEUNIT.NPY_FR_us:
td_base = _Timedelta.__new__(Timedelta, microseconds=int(value))
elif reso == NPY_DATETIMEUNIT.NPY_FR_ms:
td_base = _Timedelta.__new__(Timedelta, milliseconds=int(value))
elif reso == NPY_DATETIMEUNIT.NPY_FR_s:
td_base = _Timedelta.__new__(Timedelta, seconds=int(value))
... timedelta inherits its |
I'm not sure, but maybe this issue is related to this simple failing example: >>> pd.to_timedelta(2.5225, 's').total_seconds()
2.522499 The proposed workaround with >>> pd.to_timedelta(2.5225, 's') / pd.Timedelta(seconds=1)
2.522499999 What does is specifying the time in nanoseconds: >>> pd.to_timedelta(2.5225 * 10 ** 9, 'ns').total_seconds()
2.5225 |
@CloseChoice Maybe just add to the docs that The fact that I understand that converting 64 bit integers to |
@hagenw The “decimal” literal
But
The consequence is that A simple check:
The problem here seems to be that |
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
The
total_seconds
method from theTimedelta
class has an unexpected behavior on Pandas 1.4.2, since Pandas 1.0.5.For a small difference in nanoseconds scale the number of seconds on the interval returns 0,
Expected Behavior
The expected behavior is like what happens in Pandas 1.0.5, where:
Installed Versions
Python 3.8.4
Env w/ Pandas 1.4.2:
Env w/ Pandas 1.0.5:
The text was updated successfully, but these errors were encountered: