pydata / pydata/xarray

Zarr: Update data variable attributes with `mode="a"`

Open
#10,162 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

What happened?

When appending a coordinate to an existing store, to_zarr will save the coordinate as a data variable.

What did you expect to happen?

to_zarr should record the appended item as a coordinate

Minimal Complete Verifiable Example
import numpy as np
import xarray as xr

da = xr.DataArray(np.ones(3), dims="x", name="a")
da.to_zarr("test")
da.coords["c"] = ("x", np.ones(3))
da.to_zarr("test", mode="a")
xr.open_zarr("test")
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: 48B
Dimensions:  (x: 3)
Dimensions without coordinates: x
Data variables:
    a        (x) float64 24B dask.array<chunksize=(3,), meta=np.ndarray>
    c        (x) float64 24B dask.array<chunksize=(3,), meta=np.ndarray>
Anything else we need to know?

In the example I gave above this is happening because open_zarr expects the a zarr array to have an attribute called "coordinates" that is set to "c" but ZarrStore.set_variables doesn't save attributes for existing variables unless mode is set to "w".

Environment

INSTALLED VERSIONS

commit: None
python: 3.12.9 | packaged by conda-forge | (main, Mar 4 2025, 22:44:42) [Clang 18.1.8 ]
python-bits: 64
OS: Darwin
OS-release: 24.3.0
machine: arm64
processor: arm
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: None
libnetcdf: None

xarray: 0.1.dev5886+gcdebbf5
pandas: 2.2.3
numpy: 2.1.3
scipy: 1.15.2
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
zarr: 3.0.5
cftime: None
nc_time_axis: None
iris: None
bottleneck: None
dask: 2025.2.0
distributed: 2025.2.0
matplotlib: 3.10.1
cartopy: None
seaborn: 0.13.2
numbagg: None
fsspec: 2025.3.0
cupy: None
pint: 0.24.4
sparse: None
flox: None
numpy_groupies: None
setuptools: 75.8.2
pip: 25.0.1
conda: 24.11.3
pytest: None
mypy: None
IPython: 9.0.2
sphinx: 8.2.3

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 with xarray/backends/zarr.py, especially ZarrStore.set_variables and the mode handling linked in the issue. Reproduce the provided to_zarr/open_zarr example, then add or update coverage for appending a coordinate and verify that reopening the store retains it as a coordinate.

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
Stale
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.