From 5e68f550bf37e309a7ab23592319574ee802ff2c Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Mon, 29 Sep 2025 17:56:11 -0700 Subject: [PATCH 1/3] Fix error when writing scalar varaibles to Zarr with region={} --- doc/whats-new.rst | 2 ++ xarray/backends/zarr.py | 2 +- xarray/tests/test_backends.py | 8 ++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index a830876a1c7..e75a0992546 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -74,6 +74,8 @@ 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={}``. + 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..a93b9e18c36 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, compute=False) + ds.to_zarr(store, region={}, mode="r+", compute=True) + 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))}) From 7015117962388d86cc9665249943c6812684e82d Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Mon, 29 Sep 2025 17:57:00 -0700 Subject: [PATCH 2/3] Add PR number --- doc/whats-new.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index e75a0992546..6b7597ee494 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -74,7 +74,8 @@ 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={}``. +- Fix error raised when writing scalar variables to Zarr with ``region={}`` + (:pull:`10796`). By `Stephan Hoyer `_. Documentation From baefab1c4d2c1a91af8495d7b9f384a1c9703fc8 Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Fri, 3 Oct 2025 10:54:44 -0700 Subject: [PATCH 3/3] fix test without dask --- xarray/tests/test_backends.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index a93b9e18c36..89098564ef0 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -3446,8 +3446,8 @@ def test_write_region(self, consolidated, compute, use_dask, write_empty) -> Non def test_region_scalar(self) -> None: ds = Dataset({"x": 0}) with self.create_zarr_target() as store: - ds.to_zarr(store, compute=False) - ds.to_zarr(store, region={}, mode="r+", compute=True) + ds.to_zarr(store) + ds.to_zarr(store, region={}, mode="r+") with xr.open_zarr(store) as actual: assert_identical(actual, ds)