Skip to content

Corrupted data when Xarray writes to Zarr Datetime64 dtype #11350

@vladidobro

Description

@vladidobro

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

Details

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugneeds triageIssue that has not been reviewed by xarray team member

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions