From e3fe2491dead8a4537d1ec3a0360a84229113a46 Mon Sep 17 00:00:00 2001 From: Joseph K Aicher <4666753+jaicher@users.noreply.github.com> Date: Thu, 26 Aug 2021 15:43:33 -0400 Subject: [PATCH 1/3] Added test_save_emptydim for zarr backends, which fails when chunking Added test that fails when saving to zarr a dataset with a chunked array that has a dimension of length zero (Issue #5741) --- xarray/tests/test_backends.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 3ca20cade56..adb8e19f6d0 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -2186,6 +2186,16 @@ def test_to_zarr_append_compute_false_roundtrip(self): with self.open(store) as actual: assert_identical(xr.concat([ds, ds_to_append], dim="time"), actual) + @pytest.mark.parametrize("chunk", [False, True]) + def test_save_emptydim(self, chunk): + if chunk and not has_dask: + pytest.skip("requires dask") + ds = Dataset({"x": (("a", "b"), np.empty((5, 0))), "y": ("a", [1, 2, 5, 8, 9])}) + if chunk: + ds = ds.chunk({}) # chunk dataset to save dask array + with self.roundtrip(ds) as ds_reload: + assert_identical(ds, ds_reload) + @pytest.mark.parametrize("consolidated", [False, True]) @pytest.mark.parametrize("compute", [False, True]) @pytest.mark.parametrize("use_dask", [False, True]) From cc01b35f1137db55a7cb5d415aa4c3df55d8d866 Mon Sep 17 00:00:00 2001 From: Joseph K Aicher <4666753+jaicher@users.noreply.github.com> Date: Thu, 26 Aug 2021 15:57:29 -0400 Subject: [PATCH 2/3] Load all variables with zero entries before saving to_zarr This addresses Issue #5741 and allows `test_save_emptydim` to pass. We get around `to_zarr` not liking dask arrays with zero length dimensions by giving it numpy arrays, which works for some reason --- xarray/backends/api.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/xarray/backends/api.py b/xarray/backends/api.py index 2c9b25f860f..7b9980e035f 100644 --- a/xarray/backends/api.py +++ b/xarray/backends/api.py @@ -1327,6 +1327,11 @@ def to_zarr( See `Dataset.to_zarr` for full API docs. """ + # Load empty arrays to avoid bug saving zero length dimensions (Issue #5741) + for v in dataset.variables.values(): + if v.size == 0: + v.load() + # expand str and Path arguments store = _normalize_path(store) chunk_store = _normalize_path(chunk_store) From 09e39870afd19107e4beaac40b48f2ec55e9cc7f Mon Sep 17 00:00:00 2001 From: Joseph K Aicher <4666753+jaicher@users.noreply.github.com> Date: Thu, 26 Aug 2021 16:06:14 -0400 Subject: [PATCH 3/3] Updated whats-new.rst with information about fix for #5741 --- doc/whats-new.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 348188cbd26..b4c26b278f1 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -57,6 +57,9 @@ Deprecations Bug fixes ~~~~~~~~~ +- Fix ZeroDivisionError from saving dask array with empty dimension (:issue: `5741`). + By `Joseph K Aicher `_. + Documentation ~~~~~~~~~~~~~