Skip to content

xarray>=2025.1.2 has inconsistent treatment of np.datetime64 and datetime #10220

@DahnJ

Description

@DahnJ

What is your issue?

In xarray prior to 2025.1.2 usage of np.datetime64 and datetime resulted in identical result

from datetime import datetime
import numpy as np
import xarray as xr

a = xr.DataArray(coords={'time': [np.datetime64('2020')]}, dims=['time'])
b = xr.DataArray(coords={'time': [datetime(2020,1,1)]}, dims=['time'])

print(a['time'].dtype)
print(b['time'].dtype)

would yield

datetime64[ns]
datetime64[ns]

From version 2025.1.2, this is no longer true, and we get

datetime64[s]
datetime64[ns]

Together with the equality check failing (pandas-dev/pandas#55694), this means comparing DataArrays and Datasets is now subtly dependent on whether datetime or np.datetime64 was used to construct them.

Is this the intended behaviour? Is there a recommended way to deal with this difference beyond requiring the users to always specify np.datetime(..., 'ns')?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions