Skip to content

Commit

Permalink
Fix zarr append with groups (#3610)
Browse files Browse the repository at this point in the history
* bug fixed and added zarr group tests

* black .

* added info to whats-new

Co-authored-by: Ryan Abernathey <ryan.abernathey@gmail.com>
  • Loading branch information
niowniow and rabernat authored Mar 2, 2020
1 parent 45d88fc commit 8512b7b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ Bug fixes
- xarray now respects the over, under and bad colors if set on a provided colormap.
(:issue:`3590`, :pull:`3601`)
By `johnomotani <https://github.com/johnomotani>`_.
- Fix :py:meth:`xarray.core.dataset.Dataset.to_zarr` when using `append_dim` and `group`
simultaneously. (:issue:`3170`). By `Matthias Meyer <https://github.com/niowniow>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
4 changes: 2 additions & 2 deletions xarray/backends/zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ def store(
if len(existing_variables) > 0:
# there are variables to append
# their encoding must be the same as in the store
ds = open_zarr(self.ds.store, chunks=None)
ds = open_zarr(self.ds.store, group=self.ds.path, chunks=None)
variables_with_encoding = {}
for vn in existing_variables:
variables_with_encoding[vn] = variables[vn].copy(deep=False)
Expand Down Expand Up @@ -487,7 +487,7 @@ def open_zarr(
directory in file system where a Zarr DirectoryStore has been stored.
synchronizer : object, optional
Array synchronizer provided to zarr
group : str, obtional
group : str, optional
Group path. (a.k.a. `path` in zarr terminology.)
chunks : int or dict or tuple or {None, 'auto'}, optional
Chunk sizes along each dimension, e.g., ``5`` or
Expand Down
35 changes: 24 additions & 11 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -1729,39 +1729,52 @@ def test_hidden_zarr_keys(self):
pass

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

# overwrite mode
with self.roundtrip(original, save_kwargs={"mode": "w"}) as actual:
with self.roundtrip(
original,
save_kwargs={"mode": "w", "group": group},
open_kwargs={"group": group},
) as actual:
assert_identical(original, actual)

# don't overwrite mode
with self.roundtrip(original, save_kwargs={"mode": "w-"}) as actual:
with self.roundtrip(
original,
save_kwargs={"mode": "w-", "group": group},
open_kwargs={"group": group},
) as actual:
assert_identical(original, actual)

# make sure overwriting works as expected
with self.create_zarr_target() as store:
self.save(original, store)
# should overwrite with no error
self.save(original, store, mode="w")
with self.open(store) as actual:
self.save(original, store, mode="w", group=group)
with self.open(store, group=group) as actual:
assert_identical(original, actual)
with pytest.raises(ValueError):
self.save(original, store, mode="w-")

# check append mode for normal write
with self.roundtrip(original, save_kwargs={"mode": "a"}) as actual:
with self.roundtrip(
original,
save_kwargs={"mode": "a", "group": group},
open_kwargs={"group": group},
) as actual:
assert_identical(original, actual)

ds, ds_to_append, _ = create_append_test_data()

# check append mode for append write
ds, ds_to_append, _ = create_append_test_data()
with self.create_zarr_target() as store_target:
ds.to_zarr(store_target, mode="w")
ds_to_append.to_zarr(store_target, append_dim="time")
ds.to_zarr(store_target, mode="w", group=group)
ds_to_append.to_zarr(store_target, append_dim="time", group=group)
original = xr.concat([ds, ds_to_append], dim="time")
assert_identical(original, xr.open_zarr(store_target))
actual = xr.open_zarr(store_target, group=group)
assert_identical(original, actual)

def test_compressor_encoding(self):
original = create_test_data()
Expand Down

0 comments on commit 8512b7b

Please sign in to comment.