-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Fix nanosecond timedeltas #45108
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
Changes from 6 commits
cff6ee6
c0f8127
c9d7747
e1110bf
4406deb
b8d37e7
88145b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,7 +180,7 @@ cpdef int64_t delta_to_nanoseconds(delta) except? -1: | |
if PyDelta_Check(delta): | ||
try: | ||
return ( | ||
delta.days * 24 * 60 * 60 * 1_000_000 | ||
delta.days * 24 * 3600 * 1_000_000 | ||
+ delta.seconds * 1_000_000 | ||
+ delta.microseconds | ||
) * 1000 | ||
|
@@ -1257,6 +1257,9 @@ class Timedelta(_Timedelta): | |
truncated to nanoseconds. | ||
""" | ||
|
||
_req_any_kwargs_new = {"weeks", "days", "hours", "minutes", "seconds", | ||
"milliseconds", "microseconds", "nanoseconds"} | ||
|
||
def __new__(cls, object value=_no_input, unit=None, **kwargs): | ||
cdef _Timedelta td_base | ||
|
||
|
@@ -1267,19 +1270,34 @@ class Timedelta(_Timedelta): | |
"(days,seconds....)") | ||
|
||
kwargs = {key: _to_py_int_float(kwargs[key]) for key in kwargs} | ||
|
||
nano = convert_to_timedelta64(kwargs.pop('nanoseconds', 0), 'ns') | ||
try: | ||
value = nano + convert_to_timedelta64(timedelta(**kwargs), | ||
'ns') | ||
except TypeError as e: | ||
if not cls._req_any_kwargs_new.intersection(kwargs): | ||
raise ValueError( | ||
"cannot construct a Timedelta from the passed arguments, " | ||
"allowed keywords are " | ||
"[weeks, days, hours, minutes, seconds, " | ||
"milliseconds, microseconds, nanoseconds]" | ||
) | ||
|
||
# GH43764, making sure any nanoseconds contributions from any kwarg | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not a useful comment as its not relevant for a current reader. However a comment explaining what you are doing would be useful. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
# is taken into consideration | ||
seconds = int(( | ||
( | ||
(kwargs.get('days', 0) + kwargs.get('weeks', 0) * 7) * 24 | ||
+ kwargs.get('hours', 0) | ||
) * 3600 | ||
+ kwargs.get('minutes', 0) * 60 | ||
+ kwargs.get('seconds', 0) | ||
) * 1_000_000_000 | ||
) | ||
|
||
value = convert_to_timedelta64( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's just directly create a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice. Oversaw that. Performance is even better now. |
||
kwargs.get('nanoseconds', 0) | ||
+ int(kwargs.get('microseconds', 0) * 1_000) | ||
+ int(kwargs.get('milliseconds', 0) * 1_000_000) | ||
+ seconds | ||
, 'ns' | ||
) | ||
|
||
if unit in {'Y', 'y', 'M'}: | ||
raise ValueError( | ||
"Units 'M', 'Y', and 'y' are no longer supported, as they do not " | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change means we now fail to raise on
pd.Timedelta(days=2, foo=9)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am going to create a PR for this..