Skip to content

Numpy timedelta64 NaT not converted to uint64 NaN #16391

Open

Description

@weiji14

Not sure if this is expected behaviour, but converting a timedelta64 Not a Time (NaT) object to an uint64 type doesn't give NaN. Instead, it produces a large number.

Reproducing code example:

import numpy as np

timearray = np.array([np.timedelta64('NaT'), np.timedelta64(45969543659411542)])
# array([            'NaT', 45969543659411542], dtype=timedelta64)

Current:

intarray = timearray.astype(dtype=np.uint64)
# array([9223372036854775808,   45969543659411542], dtype=uint64)

Expected:

intarray = timearray.astype(dtype=np.uint64)
# array([           nan,   45969543659411542], dtype=uint64)

Error message:

Numpy/Python version information:

1.18.2 3.8.2 | packaged by conda-forge | (default, Apr 24 2020, 08:20:52)
[GCC 7.3.0]

Activity

  1. mattip commented on May 27, 2020

    @mattip
    Member

    There is no such creature as an int nan, float64 has the same behaviour.

    >>> np.array([float('nan')]).astype(np.uint64)
    array([9223372036854775808], dtype=uint64)
    

    We could perhaps warn here.

  2. ihnorton commented on Jun 18, 2020

    @ihnorton
    Contributor

    I've encountered similar surprises given that the representation of NaT as typemin(int64) seems to be undocumented (related). Should it be?

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