diff --git a/doc/whats-new.rst b/doc/whats-new.rst index a830876a1c7..6b7597ee494 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -74,6 +74,9 @@ Bug fixes - Allow ``combine_attrs="drop_conflicts"`` to handle objects with ``__eq__`` methods that return non-bool values (e.g., numpy arrays) without raising ``ValueError`` (:pull:`10726`). By `Maximilian Roos `_. +- Fix error raised when writing scalar variables to Zarr with ``region={}`` + (:pull:`10796`). + By `Stephan Hoyer `_. Documentation ~~~~~~~~~~~~~ diff --git a/xarray/backends/zarr.py b/xarray/backends/zarr.py index f0578ca9352..51a846e4a10 100644 --- a/xarray/backends/zarr.py +++ b/xarray/backends/zarr.py @@ -1359,7 +1359,7 @@ def _validate_and_autodetect_region(self, ds: Dataset) -> Dataset: non_matching_vars = [ k for k, v in ds.variables.items() if not set(region).intersection(v.dims) ] - if non_matching_vars: + if region and non_matching_vars: raise ValueError( f"when setting `region` explicitly in to_zarr(), all " f"variables in the dataset to write must have at least " diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 6fa1402c1f3..89098564ef0 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -3443,6 +3443,14 @@ def test_write_region(self, consolidated, compute, use_dask, write_empty) -> Non ) as actual: assert_identical(actual, nonzeros) + def test_region_scalar(self) -> None: + ds = Dataset({"x": 0}) + with self.create_zarr_target() as store: + ds.to_zarr(store) + ds.to_zarr(store, region={}, mode="r+") + with xr.open_zarr(store) as actual: + assert_identical(actual, ds) + @pytest.mark.parametrize("mode", [None, "r+", "a"]) def test_write_region_mode(self, mode) -> None: zeros = Dataset({"u": (("x",), np.zeros(10))})