pydata / pydata/xarray

Corrupted data when Xarray writes to Zarr Datetime64 dtype

Open
#11,350 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug needs triage
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

What happened?

Hi,
when using the new Zarr v3 DateTime64 dtype, I have trouble correctly writing to it with xarray - I have not found a way to write the correct values.
I believe it is probably related to some CF coding enabled when it should not be, or something like that.

Am I doing something wrong?

What did you expect to happen?

I expected Xarray to not corrupt data

Minimal Complete Verifiable Example
# /// script
# requires-python = ">=3.11"
# dependencies = [
#   "xarray[complete]@git+https://github.com/pydata/xarray.git@main",
# ]
# ///
#
# This script automatically imports the development branch of xarray to check for issues.
# Please delete this header if you have _not_ tested this script with `uv run`!

import xarray as xr
xr.show_versions()

import numpy as np
import zarr
import zarr.storage
import zarr.dtype

store = zarr.storage.MemoryStore()

g = zarr.create_group(store)
a = g.create_array('a', shape=(2,), dtype=zarr.dtype.DateTime64(unit='s', scale_factor=1), dimension_names=['time'])
a[:] = np.array(['2025-01-01', '2025-01-02'], dtype='<M8[us]')

ds = xr.open_zarr(store, chunks=None, consolidated=False).compute()
print(ds)  # reading is ok
# <xarray.Dataset> Size: 16B
# Dimensions:  (time: 2)
# Dimensions without coordinates: time
# Data variables:
#     a        (time) datetime64[s] 16B 2025-01-01 2025-01-02

ds.to_zarr(store, mode='r+', consolidated=False)

ds = xr.open_zarr(store, chunks=None, consolidated=False).compute()
print(ds)  # writing writes bogus values
# <xarray.Dataset> Size: 16B
# Dimensions:  (time: 2)
# Dimensions without coordinates: time
# Data variables:
#     a        (time) datetime64[s] 16B 1970-01-01 1970-01-01T00:00:01
Steps to reproduce

uv run the script

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.
Relevant log output
<xarray.Dataset> Size: 16B
Dimensions:  (time: 2)
Dimensions without coordinates: time
Data variables:
    a        (time) datetime64[s] 16B 2025-01-01 2025-01-02
<xarray.Dataset> Size: 16B
Dimensions:  (time: 2)
Dimensions without coordinates: time
Data variables:
    a        (time) datetime64[s] 16B 1970-01-01 1970-01-01T00:00:01
Anything else we need to know?

No response

Environment

INSTALLED VERSIONS

commit: None
python: 3.12.12 (main, Jan 14 2026, 23:36:32) [Clang 21.1.4 ]
python-bits: 64
OS: Darwin
OS-release: 25.4.0
machine: arm64
processor: arm
byteorder: little
LC_ALL: None
LANG: None
LOCALE: (None, 'UTF-8')
libhdf5: 1.14.6
libnetcdf: 4.9.3

xarray: 2026.4.1.dev10+gd022da542
pandas: 3.0.3
numpy: 2.4.6
scipy: 1.17.1
netCDF4: 1.7.4
pydap: 3.5.9
h5netcdf: 1.8.1
h5py: 3.16.0
zarr: 3.2.1
cftime: 1.6.5
nc_time_axis: 1.4.1
iris: None
bottleneck: 1.6.0
dask: 2026.3.0
distributed: 2026.3.0
matplotlib: 3.10.9
cartopy: 0.25.0
seaborn: 0.13.2
numbagg: 0.9.4
fsspec: 2026.4.0
cupy: None
pint: None
sparse: 0.18.0
flox: 0.11.2
numpy_groupies: 0.11.3
setuptools: None
pip: None
conda: None
pytest: None
mypy: None
IPython: None
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 provided MVCE with the listed xarray, NumPy, and Zarr versions, then trace the open_zarr and to_zarr paths for datetime64 handling and CF encoding. The fix is complete when the round trip preserves the original 2025-01-01 and 2025-01-02 values instead of writing epoch-based values.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.