Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ Deprecations

Bug fixes
~~~~~~~~~

- Fix ZeroDivisionError from saving dask array with empty dimension (:issue: `5741`).
By `Joseph K Aicher <https://github.com/jaicher>`_.
- Fixed performance bug where ``cftime`` import attempted within various core operations if ``cftime`` not
installed (:pull:`5640`).
By `Luke Sewell <https://github.com/lusewell>`_
Expand Down
5 changes: 5 additions & 0 deletions xarray/backends/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1322,6 +1322,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)
Expand Down
10 changes: 10 additions & 0 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down