`xarray>=2025.1.2` has inconsistent treatment of `np.datetime64` and `datetime`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
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 (https://github.com/pandas-dev/pandas/issues/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')?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the reported behavior with the DataArray examples using both datetime and np.datetime64, then compare the resulting dtypes and equality behavior. Read the pandas issue linked in the report and trace the datetime construction path in xarray; done requires an agreed treatment or recommendation for the differing resolutions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, pandas, python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100