pydata / pydata/xarray

Equality between datetime64[s] and datetime64[ns] is not consistent for coordinates vs data variables

Open
#10,045 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
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
INSTALLED VERSIONS ------------------ commit: None python: 3.11.8 (stable, redacted, redacted) [Clang 9999.0.0 (4018317407006b2c632fbb75729de624a2426439)] python-bits: 64 OS: Linux OS-release: 6.10.11-1rodete2-amd64 machine: x86_64 processor: byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: ('en_US', 'UTF-8') libhdf5: 1.12.3 libnetcdf: 4.6.1

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.