pydata / pydata/xarray

to_zarr with append can break existing dataset

Open
#10,766 5 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?

When appending an incomplete dataset to an existing zarr store, the existing zarr store can end up in a broken state, because not all dimension sizes of existing variables are updated consistently.

What did you expect to happen?

I would have expected that xarray doesn't break my existing dataset, i.e. either:

  • xarray should raise an exception when trying to append (such that the existing dataset doesn't break) or
  • appending ds2 should appropriately fill the missing array cells of the b variable with missing values
Minimal Complete Verifiable Example
# /// script
# requires-python = ">=3.13"
# dependencies = [
#     "xarray",
#     "zarr",
# ]
# ///

import xarray as xr
xr.show_versions()

m = {}
ds1 = xr.Dataset({"a": (("x",), [1, 2]), "b": (("x",), [10, 20])})
ds2 = xr.Dataset({"a": (("x",), [3, 4])})
ds1.to_zarr(m)
ds2.to_zarr(m, append_dim="x")
xr.open_dataset(m, engine="zarr")
Steps to reproduce
uv run issue.py
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
ValueError: conflicting sizes for dimension 'x': length 2 on 'b' and length 4 on {'x': 'a'}
Anything else we need to know?

No response

Environment
INSTALLED VERSIONS ------------------ commit: None python: 3.13.5 (main, Jun 11 2025, 15:36:57) [Clang 17.0.0 (clang-1700.0.13.3)] python-bits: 64 OS: Darwin OS-release: 24.6.0 machine: arm64 processor: arm byteorder: little LC_ALL: None LANG: de_DE.UTF-8 LOCALE: ('de_DE', 'UTF-8') libhdf5: None libnetcdf: None

xarray: 2025.9.0
pandas: 2.3.2
numpy: 2.3.3
scipy: None
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
zarr: 3.1.2
cftime: None
nc_time_axis: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
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 reproducing the issue with the provided MVCE, then trace the to_zarr entry point and its append_dim handling for incomplete datasets. Done means appending ds2 either raises before modifying the existing store or keeps every variable dimension consistent by filling missing cells appropriately.

Written by the indexing model from the issue text.

Assessment

Tech stack
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.