Skip to content

to_zarr(append_dim='dim0') doesn't need mode='a' #3123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 29, 2019
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
7 changes: 4 additions & 3 deletions doc/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,9 @@ store is already present at that path, an error will be raised, preventing it
from being overwritten. To override this behavior and overwrite an existing
store, add ``mode='w'`` when invoking ``to_zarr``.

It is also possible to append to an existing store. For that, add ``mode='a'``
and set ``append_dim`` to the name of the dimension along which to append.
It is also possible to append to an existing store. For that, set
``append_dim`` to the name of the dimension along which to append. ``mode``
can be omitted as it will internally be set to ``'a'``.

.. ipython:: python
:suppress:
Expand All @@ -628,7 +629,7 @@ and set ``append_dim`` to the name of the dimension along which to append.
coords={'x': [10, 20, 30, 40],
'y': [1,2,3,4,5],
't': pd.date_range('2001-01-03', periods=2)})
ds2.to_zarr('path/to/directory.zarr', mode='a', append_dim='t')
ds2.to_zarr('path/to/directory.zarr', append_dim='t')

To store variable length strings use ``dtype=object``.

Expand Down
4 changes: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ New functions/methods
Enhancements
~~~~~~~~~~~~

- In :py:meth:`~xarray.Dataset.to_zarr`, passing ``mode`` is not mandatory if
``append_dim`` is set, as it will automatically be set to ``'a'`` internally.
By `David Brochart <https://github.com/davidbrochart>`_.

Bug fixes
~~~~~~~~~

Expand Down
4 changes: 2 additions & 2 deletions xarray/backends/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ def _validate_append_dim_and_encoding(ds_to_append, store, append_dim,
)


def to_zarr(dataset, store=None, mode='w-', synchronizer=None, group=None,
def to_zarr(dataset, store=None, mode=None, synchronizer=None, group=None,
encoding=None, compute=True, consolidated=False, append_dim=None):
"""This function creates an appropriate datastore for writing a dataset to
a zarr ztore
Expand All @@ -1118,7 +1118,7 @@ def to_zarr(dataset, store=None, mode='w-', synchronizer=None, group=None,
_validate_dataset_names(dataset)
_validate_attrs(dataset)

if mode == "a":
if mode == 'a':
_validate_datatypes_for_zarr_append(dataset)
_validate_append_dim_and_encoding(dataset, store, append_dim,
group=group,
Expand Down
19 changes: 16 additions & 3 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1389,7 +1389,7 @@ def to_netcdf(
def to_zarr(
self,
store: Union[MutableMapping, str, Path] = None,
mode: str = 'w-',
mode: str = None,
synchronizer=None,
group: str = None,
encoding: Mapping = None,
Expand All @@ -1407,10 +1407,13 @@ def to_zarr(
----------
store : MutableMapping, str or Path, optional
Store or path to directory in file system.
mode : {'w', 'w-', 'a'}
mode : {'w', 'w-', 'a', None}
Persistence mode: 'w' means create (overwrite if exists);
'w-' means create (fail if exists);
'a' means append (create if does not exist).
If ``append_dim`` is set, ``mode`` can be omitted as it is
internally set to ``'a'``. Otherwise, ``mode`` will default to
`w-` if not set.
synchronizer : object, optional
Array synchronizer
group : str, optional
Expand All @@ -1426,14 +1429,24 @@ def to_zarr(
If True, apply zarr's `consolidate_metadata` function to the store
after writing.
append_dim: hashable, optional
If mode='a', the dimension on which the data will be appended.
If set, the dimension on which the data will be appended.

References
----------
https://zarr.readthedocs.io/
"""
if encoding is None:
encoding = {}
if (mode == 'a') or (append_dim is not None):
if mode is None:
mode = 'a'
elif mode != 'a':
raise ValueError(
"append_dim was set along with mode='{}', either set "
"mode='a' or don't set it.".format(mode)
)
elif mode is None:
mode = 'w-'
if mode not in ['w', 'w-', 'a']:
# TODO: figure out how to handle 'r+'
raise ValueError("The only supported options for mode are 'w',"
Expand Down
25 changes: 17 additions & 8 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -1654,7 +1654,7 @@ def test_write_persistence_modes(self):
# check append mode for append write
with self.create_zarr_target() as store_target:
ds.to_zarr(store_target, mode='w')
ds_to_append.to_zarr(store_target, mode='a', append_dim='time')
ds_to_append.to_zarr(store_target, append_dim='time')
original = xr.concat([ds, ds_to_append], dim='time')
assert_identical(original, xr.open_zarr(store_target))

Expand Down Expand Up @@ -1690,7 +1690,7 @@ def test_append_write(self):
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, mode='a', append_dim='time')
ds_to_append.to_zarr(store_target, append_dim='time')
original = xr.concat([ds, ds_to_append], dim='time')
assert_identical(original, xr.open_zarr(store_target))

Expand All @@ -1706,8 +1706,7 @@ def test_append_with_invalid_dim_raises(self):
with pytest.raises(ValueError):
with self.create_zarr_target() as store_target:
ds.to_zarr(store_target, mode='w')
ds_to_append.to_zarr(store_target, mode='a',
append_dim='notvalid')
ds_to_append.to_zarr(store_target, append_dim='notvalid')

def test_append_with_append_dim_not_set_raises(self):

Expand All @@ -1719,6 +1718,17 @@ def test_append_with_append_dim_not_set_raises(self):
ds.to_zarr(store_target, mode='w')
ds_to_append.to_zarr(store_target, mode='a')

def test_append_with_mode_not_a_raises(self):

ds, ds_to_append, _ = create_append_test_data()

# check failure when append_dim is set and mode != 'a'
with pytest.raises(ValueError):
with self.create_zarr_target() as store_target:
ds.to_zarr(store_target, mode='w')
ds_to_append.to_zarr(store_target, mode='w',
append_dim='time')

def test_append_with_existing_encoding_raises(self):

ds, ds_to_append, _ = create_append_test_data()
Expand All @@ -1727,8 +1737,7 @@ def test_append_with_existing_encoding_raises(self):
with pytest.raises(ValueError):
with self.create_zarr_target() as store_target:
ds.to_zarr(store_target, mode='w')
ds_to_append.to_zarr(store_target, mode='a',
append_dim='time',
ds_to_append.to_zarr(store_target, append_dim='time',
encoding={'da': {'compressor': None}})

def test_check_encoding_is_consistent_after_append(self):
Expand All @@ -1741,7 +1750,7 @@ def test_check_encoding_is_consistent_after_append(self):
compressor = zarr.Blosc()
encoding = {'da': {'compressor': compressor}}
ds.to_zarr(store_target, mode='w', encoding=encoding)
ds_to_append.to_zarr(store_target, mode='a', append_dim='time')
ds_to_append.to_zarr(store_target, append_dim='time')
actual_ds = xr.open_zarr(store_target)
actual_encoding = actual_ds['da'].encoding['compressor']
assert actual_encoding.get_config() == compressor.get_config()
Expand Down Expand Up @@ -1802,7 +1811,7 @@ def test_to_zarr_append_compute_false_roundtrip(self):
assert_identical(ds, actual)

delayed_obj = self.save(ds_to_append, store, compute=False,
mode='a', append_dim='time')
append_dim='time')
assert isinstance(delayed_obj, Delayed)

with pytest.raises(AssertionError):
Expand Down