to_zarr with append can break existing dataset
Nobody has claimed this yet.
- 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
ds2should appropriately fill the missing array cells of thebvariable 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
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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