-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: arithmetic between datetime.timedelta scalar and series produces timedelta64[us] instead of timedelta64[ns] #59656
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
Thanks for the report, confirmed on main. Further investigations and PRs to fix are welcome! |
Hmm, |
@yuanx749 - I'm seeing nanosecond. x = pd.Timedelta(days=1)
print(x.unit)
# ns |
take |
Alright, I did a little digging and found what I believe to be the source of the issue. It seems that there is a special case for how the pandas Timedelta constructor handles resolution when constructed from a datetime.timedelta object. To illustrate:
When a datetime.timedelta object is prepared for array ops it is cast up to Timedelta, which explains the microsecond resolution observed in the issue description:
However when a Series is constructed with a datetime.timedelta object, that object is NOT cast up to a pandas Timedelta. Rather it is converted using the maybe_infer_to_datetimelike function (and subsequently the convert_to_timedelta64 function), which results in a numpy timedelta64 object with nanosecond resolution. The interesting part is that when handling datetime.timedelta objects, the Timedelta constructor explicitly sets the resolution to microseconds, whereas the convert_to_timedelta64 function explicitly sets the resolution to nanoseconds.
In light of this inconsistency, could I get some clarification on which resolution is the correct/expected behavior for datetime.timedelta objects? My intuition is that the Timedelta constructor is handling it incorrectly because the delta_to_nanoseconds call seems to be redundant, as the target resolution passed in is the same as the input resolution defined inside the function for datetime.timedelta objects, but I'm not 100% sure. |
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
Constructing a series out of a
datetime.timedelta
gives a timedelta with nanosecond resolution, but multiplying (or dividing) adatetime.timedelta
by a numeric series gives microsecond resolution.Expected Behavior
The resolution should be the same in both cases.
Installed Versions
The text was updated successfully, but these errors were encountered: