Skip to content

BUG: np.datetime64("nat", "ms") does not use unit in repr (unlike timedelta) #28496

Open
@mroeschke

Description

@mroeschke

Describe the issue:

When astypeing a unit-less datetime64 NaT to the same type with specified unit, the unit is not preserved unlike the analgous operation for timedelta64

Reproduce the code example:

>>> import numpy as np
>>> np.__version__
'2.3.0.dev0+git20250301.dfb054e'
>>> np.timedelta64("nat")
np.timedelta64('NaT')
>>> np.timedelta64("nat").astype(np.dtype("timedelta64[ns]"))
np.timedelta64('NaT','ns')
>>> np.datetime64("nat")
np.datetime64('NaT')
>>> np.datetime64("nat").astype(np.dtype("datetime64[ns]"))
np.datetime64('NaT')

Error message:

Python and NumPy Versions:

'2.3.0.dev0+git20250301.dfb054e'

Runtime Environment:

No response

Context for the issue:

No response

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