Skip to content

Commit 8512b7b

Browse files
niowniowrabernat
andauthored
Fix zarr append with groups (pydata#3610)
* bug fixed and added zarr group tests * black . * added info to whats-new Co-authored-by: Ryan Abernathey <[email protected]>
1 parent 45d88fc commit 8512b7b

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ Bug fixes
5656
- xarray now respects the over, under and bad colors if set on a provided colormap.
5757
(:issue:`3590`, :pull:`3601`)
5858
By `johnomotani <https://github.com/johnomotani>`_.
59+
- Fix :py:meth:`xarray.core.dataset.Dataset.to_zarr` when using `append_dim` and `group`
60+
simultaneously. (:issue:`3170`). By `Matthias Meyer <https://github.com/niowniow>`_.
5961

6062
Documentation
6163
~~~~~~~~~~~~~

xarray/backends/zarr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ def store(
373373
if len(existing_variables) > 0:
374374
# there are variables to append
375375
# their encoding must be the same as in the store
376-
ds = open_zarr(self.ds.store, chunks=None)
376+
ds = open_zarr(self.ds.store, group=self.ds.path, chunks=None)
377377
variables_with_encoding = {}
378378
for vn in existing_variables:
379379
variables_with_encoding[vn] = variables[vn].copy(deep=False)
@@ -487,7 +487,7 @@ def open_zarr(
487487
directory in file system where a Zarr DirectoryStore has been stored.
488488
synchronizer : object, optional
489489
Array synchronizer provided to zarr
490-
group : str, obtional
490+
group : str, optional
491491
Group path. (a.k.a. `path` in zarr terminology.)
492492
chunks : int or dict or tuple or {None, 'auto'}, optional
493493
Chunk sizes along each dimension, e.g., ``5`` or

xarray/tests/test_backends.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,39 +1729,52 @@ def test_hidden_zarr_keys(self):
17291729
pass
17301730

17311731
@pytest.mark.skipif(LooseVersion(dask_version) < "2.4", reason="dask GH5334")
1732-
def test_write_persistence_modes(self):
1732+
@pytest.mark.parametrize("group", [None, "group1"])
1733+
def test_write_persistence_modes(self, group):
17331734
original = create_test_data()
17341735

17351736
# overwrite mode
1736-
with self.roundtrip(original, save_kwargs={"mode": "w"}) as actual:
1737+
with self.roundtrip(
1738+
original,
1739+
save_kwargs={"mode": "w", "group": group},
1740+
open_kwargs={"group": group},
1741+
) as actual:
17371742
assert_identical(original, actual)
17381743

17391744
# don't overwrite mode
1740-
with self.roundtrip(original, save_kwargs={"mode": "w-"}) as actual:
1745+
with self.roundtrip(
1746+
original,
1747+
save_kwargs={"mode": "w-", "group": group},
1748+
open_kwargs={"group": group},
1749+
) as actual:
17411750
assert_identical(original, actual)
17421751

17431752
# make sure overwriting works as expected
17441753
with self.create_zarr_target() as store:
17451754
self.save(original, store)
17461755
# should overwrite with no error
1747-
self.save(original, store, mode="w")
1748-
with self.open(store) as actual:
1756+
self.save(original, store, mode="w", group=group)
1757+
with self.open(store, group=group) as actual:
17491758
assert_identical(original, actual)
17501759
with pytest.raises(ValueError):
17511760
self.save(original, store, mode="w-")
17521761

17531762
# check append mode for normal write
1754-
with self.roundtrip(original, save_kwargs={"mode": "a"}) as actual:
1763+
with self.roundtrip(
1764+
original,
1765+
save_kwargs={"mode": "a", "group": group},
1766+
open_kwargs={"group": group},
1767+
) as actual:
17551768
assert_identical(original, actual)
17561769

1757-
ds, ds_to_append, _ = create_append_test_data()
1758-
17591770
# check append mode for append write
1771+
ds, ds_to_append, _ = create_append_test_data()
17601772
with self.create_zarr_target() as store_target:
1761-
ds.to_zarr(store_target, mode="w")
1762-
ds_to_append.to_zarr(store_target, append_dim="time")
1773+
ds.to_zarr(store_target, mode="w", group=group)
1774+
ds_to_append.to_zarr(store_target, append_dim="time", group=group)
17631775
original = xr.concat([ds, ds_to_append], dim="time")
1764-
assert_identical(original, xr.open_zarr(store_target))
1776+
actual = xr.open_zarr(store_target, group=group)
1777+
assert_identical(original, actual)
17651778

17661779
def test_compressor_encoding(self):
17671780
original = create_test_data()

0 commit comments

Comments
 (0)