Skip to content

Commit a440558

Browse files
authored
Fix error when writing scalar varaibles to Zarr with region={} (#10796)
* Fix error when writing scalar varaibles to Zarr with region={} * Add PR number * fix test without dask
1 parent 4466deb commit a440558

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ Bug fixes
117117
- Allow ``combine_attrs="drop_conflicts"`` to handle objects with ``__eq__`` methods that return
118118
non-bool values (e.g., numpy arrays) without raising ``ValueError`` (:pull:`10726`).
119119
By `Maximilian Roos <https://github.com/max-sixty>`_.
120+
- Fix error raised when writing scalar variables to Zarr with ``region={}``
121+
(:pull:`10796`).
122+
By `Stephan Hoyer <https://github.com/shoyer>`_.
120123

121124
Documentation
122125
~~~~~~~~~~~~~

xarray/backends/zarr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1364,7 +1364,7 @@ def _validate_and_autodetect_region(self, ds: Dataset) -> Dataset:
13641364
non_matching_vars = [
13651365
k for k, v in ds.variables.items() if not set(region).intersection(v.dims)
13661366
]
1367-
if non_matching_vars:
1367+
if region and non_matching_vars:
13681368
raise ValueError(
13691369
f"when setting `region` explicitly in to_zarr(), all "
13701370
f"variables in the dataset to write must have at least "

xarray/tests/test_backends.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3443,6 +3443,14 @@ def test_write_region(self, consolidated, compute, use_dask, write_empty) -> Non
34433443
) as actual:
34443444
assert_identical(actual, nonzeros)
34453445

3446+
def test_region_scalar(self) -> None:
3447+
ds = Dataset({"x": 0})
3448+
with self.create_zarr_target() as store:
3449+
ds.to_zarr(store)
3450+
ds.to_zarr(store, region={}, mode="r+")
3451+
with xr.open_zarr(store) as actual:
3452+
assert_identical(actual, ds)
3453+
34463454
@pytest.mark.parametrize("mode", [None, "r+", "a"])
34473455
def test_write_region_mode(self, mode) -> None:
34483456
zeros = Dataset({"u": (("x",), np.zeros(10))})

0 commit comments

Comments
 (0)