Skip to content

BUG: timedelta64 with ns units converts to python int rather than a python timedelta #28596

Open

Description

@mdsmarte

Describe the issue:

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.
  • An error.

Reproduce the code example:

>>> import numpy as np
>>> obj = np.timedelta64(1, "ns")
>>> from datetime import timedelta                      
>>> type(obj.astype("timedelta64[us]").astype(timedelta))
<class 'datetime.timedelta'>
>>> type(obj.astype("timedelta64[ns]").astype(timedelta))
<class 'int'>
>>> type(obj.astype(timedelta))
<class 'int'>

Error message:

Python and NumPy Versions:

1.23.5
3.11.11 |Enthought, Inc. (x86_64)| (default, Mar 26 2025, 11:29:43) [Clang 15.0.0 (clang-1500.1.0.2.5)]

Runtime Environment:

No response

Context for the issue:

No response

Activity

  1. 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
  2. shoyer commented on Mar 27, 2025

    @shoyer
    Member

    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.

  3. mdsmarte commented on Mar 28, 2025

    @mdsmarte
    Author

    Note that the same problem also exists for datetime64.... .astype(datetime) converts to an int instead of a datetime when it has has nanosecond units.

  4. 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
  5. 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
  6. Mohit-Kundu commented on Apr 1, 2025

    @Mohit-Kundu

    Hi @mdsmarte @shoyer, I'm willing to work on this!

  7. jorenham commented on Apr 9, 2025

    @jorenham
    Member

    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.

  8. JeisonPham commented on Jun 16, 2025

    @JeisonPham

    @Mohit-Kundu Are you still working on this? Can I take it over?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions