You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BUG: timedelta64 with ns units converts to python int rather than a python timedelta #28596
When converting a timedelta64 with nanosecond units to a python timedelta object, the result unexpectedly has the int type. With microsecond (or coarser) units the results is a python timedelta object, as expected.
Instead of silently generating an object with an int type, I would have expected:
A timedelta object with the nanosecond resolution dropped (since timedelta only supports microsecond resolution), perhaps with a warning emitted.
changed the title [-]BUG: `timedelta64` with ns units converts to python int rather than a python datetime[/-][+]BUG: `timedelta64` with ns units converts to python `int` rather than a python `datetime`[/+]on Mar 27, 2025
This is a long-term usability issue for NumPy's datetime/timedelta types.
I think a reasonable choice would be to convert to a datetime.timedelta only if the nano-second part of the delta is zero. Otherwise we should raise error.
Always raising an error and requiring users to switch to another precision would also be OK, although nano-second precision timedeltas that don't actually require nanosecond precision are common because until recently, timedelta64[ns] was all that were supported by pandas.
changed the title [-]BUG: `timedelta64` with ns units converts to python `int` rather than a python `datetime`[/-][+]BUG: `timedelta64` with ns units converts to python `int` rather than a python `timdelta`[/+]on Mar 28, 2025
changed the title [-]BUG: `timedelta64` with ns units converts to python `int` rather than a python `timdelta`[/-][+]BUG: `timedelta64` with ns units converts to python `int` rather than a python `timedelta`[/+]on Mar 28, 2025
I'm not a big fan of making the timedelta64.item()type dependent on its value. It doesn't really feel very NEP-50 to me. But most importantly, that would make it impossible to annotate in the stubs.
Activity
[-]BUG: `timedelta64` with ns units converts to python int rather than a python datetime[/-][+]BUG: `timedelta64` with ns units converts to python `int` rather than a python `datetime`[/+]shoyer commentedon Mar 27, 2025
This is a long-term usability issue for NumPy's datetime/timedelta types.
I think a reasonable choice would be to convert to a
datetime.timedeltaonly if the nano-second part of the delta is zero. Otherwise we should raise error.Always raising an error and requiring users to switch to another precision would also be OK, although nano-second precision timedeltas that don't actually require nanosecond precision are common because until recently,
timedelta64[ns]was all that were supported by pandas.mdsmarte commentedon Mar 28, 2025
Note that the same problem also exists for
datetime64.....astype(datetime)converts to anintinstead of adatetimewhen it has has nanosecond units.[-]BUG: `timedelta64` with ns units converts to python `int` rather than a python `datetime`[/-][+]BUG: `timedelta64` with ns units converts to python `int` rather than a python `timdelta`[/+][-]BUG: `timedelta64` with ns units converts to python `int` rather than a python `timdelta`[/-][+]BUG: `timedelta64` with ns units converts to python `int` rather than a python `timedelta`[/+]Mohit-Kundu commentedon Apr 1, 2025
Hi @mdsmarte @shoyer, I'm willing to work on this!
jorenham commentedon Apr 9, 2025
I'm not a big fan of making the
timedelta64.item()type dependent on its value. It doesn't really feel very NEP-50 to me. But most importantly, that would make it impossible to annotate in the stubs.JeisonPham commentedon Jun 16, 2025
@Mohit-Kundu Are you still working on this? Can I take it over?