Skip to content

fix: skip CF encoding for zarr arrays with native DateTime64 dtype (GH#11350)#11419

Open
C1-BA-B1-F3 wants to merge 1 commit into
pydata:mainfrom
C1-BA-B1-F3:fix-zarr-datetime64-corruption
Open

fix: skip CF encoding for zarr arrays with native DateTime64 dtype (GH#11350)#11419
C1-BA-B1-F3 wants to merge 1 commit into
pydata:mainfrom
C1-BA-B1-F3:fix-zarr-datetime64-corruption

Conversation

@C1-BA-B1-F3

Copy link
Copy Markdown

Problem

When writing a dataset back to a zarr store with native dtype (zarr v3), the datetime values get corrupted. For example, and become and .

Root Cause

The function applies CF datetime encoding which converts to with and attributes. When these values (e.g., for "days since 2025-01-01") are written to a zarr array, the array interprets them as raw epoch seconds, resulting in corrupted data.

Fix

In , detect when an existing zarr array has a native dtype (via ). For these arrays, skip CF encoding and write the raw values directly, since zarr handles the datetime storage natively.

Changes

  • : Skip CF encoding for existing zarr arrays with native datetime64 dtype
  • : Add regression test

Testing

  • Regression test passes
  • All 172 datetime/timedelta tests pass
  • All 387 TestZarrDictStore tests pass

Fixes #11350

@github-actions github-actions Bot added topic-backends topic-zarr Related to zarr storage library io topic-NamedArray Lightweight version of Variable labels Jun 26, 2026
…H#11350)

When writing to a zarr array with native DateTime64 dtype (zarr v3),
skip CF datetime encoding. CF encoding converts datetime64 to int64
with units/calendar attributes, which corrupts data when written to
a native DateTime64 zarr array — the int64 values get misinterpreted
as raw epoch seconds instead of the encoded 'days since' values.

Fixes pydata#11350
@C1-BA-B1-F3 C1-BA-B1-F3 force-pushed the fix-zarr-datetime64-corruption branch from f92ddef to d864330 Compare June 26, 2026 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

io topic-backends topic-NamedArray Lightweight version of Variable topic-zarr Related to zarr storage library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Corrupted data when Xarray writes to Zarr Datetime64 dtype

1 participant