Skip to content

Conversation

@jbrockmendel
Copy link
Member

This implements resolution inference for array_to_timedelta64 matching the behavior for the Timedelta scalar. This does not change anything for strings, just np.timedelta64, pytimedelta, and pd.Timedelta objects.

One unintended side effect is that pd.Series([np.timedelta64(1, "M")]) raises instead of casting to "30 days 10:29:06". Need to give that some thought.

There is some de-duplication that can be done either before merging or as follow-up.

This fixes some xfails. I'm pretty sure this also addresses some open issues, will need to track those down and add tests.


# These should not overflow!
exp = TimedeltaIndex([NaT])
exp = TimedeltaIndex([NaT], dtype="m8[ns]")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the default unit that would be used in this case?
Still nanoseconds?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the default in all-NaT cases is seconds (same as for datetimes). We made that choice for datetimes to ensure that in e.g.

left = pd.to_datetime(not_all_nat)
right = pd.to_datetime(all_nat)

result1 = pd.concat([left, right])
result2 = left - right

result1.unit and result2.unit should always get left.unit.

Copy link
Member

@jorisvandenbossche jorisvandenbossche Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a specific test about it (because here you are overriding the default by providing a dtype)? I had fetched the branch to test a few things, and seeing:

>>> pd.to_datetime([pd.NaT])
DatetimeIndex(['NaT'], dtype='datetime64[s]', freq=None)
>>> pd.to_timedelta([pd.NaT])
TimedeltaIndex([NaT], dtype='timedelta64[ns]', freq=None)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. The TimedeltaIndex constructor has the same behavior. I'll try to track this down.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just updated with the fix+test for this

@jorisvandenbossche
Copy link
Member

One unintended side effect is that pd.Series([np.timedelta64(1, "M")]) raises instead of casting to "30 days 10:29:06". Need to give that some thought.

My feeling is that this is a positive change

@mroeschke mroeschke mentioned this pull request Nov 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants