Equality between datetime64[s] and datetime64[ns] is not consistent for coordinates vs data variables
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
What happened?
When the datetime resolution of coordinates differs, this breaks equality via .equals:
datetime_ns = np.array(['2025-01-01'], dtype='datetime64[ns]')
datetime_s = np.array(['2025-01-01'], dtype='datetime64[s]')
da_ns_coords = xarray.DataArray(dims=('x',), data=[0], coords={'x': datetime_ns})
da_s_coords = xarray.DataArray(dims=('x',), data=[0], coords={'x': datetime_s})
da_ns_coords.equals(da_s_coords)
=> False
But not via .broadcast_equals:
da_ns_coords.broadcast_equals(da_s_coords)
=> True
For data variables, equality via .equals holds even when the datetime resolution differs:
da_ns = xarray.DataArray(dims=('x',), data=datetime_ns)
da_s = xarray.DataArray(dims=('x',), data=datetime_s)
da_ns.equals(da_s)
=> True
What did you expect to happen?
I expected the two values to be equal even when the dtypes are datetime64[ns] vs datetime64[s].
At a minimum, I expected the treatment of dtypes in equality to be consistent for coordinates vs data variables, and for .equals vs .broadcast_equals.
Minimal Complete Verifiable Example
See above.
MVCE confirmation
- Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
- Complete example — the example is self-contained, including all data and the text of any traceback.
- Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
- New issue — a search of GitHub Issues suggests this is not a duplicate.
- Recent environment — the issue occurs with the latest version of xarray and its dependencies.
Anything else we need to know?
I suspect the cause is that pandas DatetimeIndex is not .equals when the dtype differs:
index_ns = da_ns_coords.indexes['x']
index_s = da_s_coords.indexes['x']
index_ns
=> DatetimeIndex(['2025-01-01'], dtype='datetime64[ns]', name='x', freq=None)
index_s
=> DatetimeIndex(['2025-01-01'], dtype='datetime64[s]', name='x', freq=None)
index_ns.equals(index_s)
=> False
Environment
xarray: 2025.01.2
pandas: 2.2.3
numpy: 2.2.1
scipy: 1.13.1
netCDF4: 1.4.1
pydap: None
h5netcdf: 999
h5py: 3.11.0
zarr: 2.18.2
cftime: 1.6.4
nc_time_axis: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: 3.9.1
cartopy: None
seaborn: 0.12.2
numbagg: None
fsspec: 2023.3.0
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 0.dev0+unknown
pip: None
conda: None
pytest: None
mypy: None
IPython: 7.34.0
sphinx: None
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
Start by running the MVCE comparing DataArray.equals and DataArray.broadcast_equals for datetime64[s] and datetime64[ns]. Trace the equality entry points and coordinate index comparisons, then add regression coverage showing consistent results for coordinates and data variables. Done means equivalent datetime values compare equally through both methods.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, pandas, python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100