Skip to content
This repository was archived by the owner on Oct 24, 2024. It is now read-only.

Change default write mode of to_zarr() to 'w-' #275

Merged
merged 3 commits into from
Nov 10, 2023
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
9 changes: 7 additions & 2 deletions datatree/datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,12 @@ def to_netcdf(
)

def to_zarr(
self, store, mode: str = "w", encoding=None, consolidated: bool = True, **kwargs
self,
store,
mode: str = "w-",
encoding=None,
consolidated: bool = True,
**kwargs,
):
"""
Write datatree contents to a Zarr store.
Expand All @@ -1505,7 +1510,7 @@ def to_zarr(
----------
store : MutableMapping, str or Path, optional
Store or path to directory in file system
mode : {{"w", "w-", "a", "r+", None}, default: "w"
mode : {{"w", "w-", "a", "r+", None}, default: "w-"
Persistence mode: “w” means create (overwrite if exists); “w-” means create (fail if exists);
“a” means override existing variables (create if does not exist); “r+” means modify existing
array values only (raise an error if any metadata or shapes would change). The default mode
Expand Down
2 changes: 1 addition & 1 deletion datatree/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def _create_empty_zarr_group(store, group, mode):
def _datatree_to_zarr(
dt: DataTree,
store,
mode: str = "w",
mode: str = "w-",
encoding=None,
consolidated: bool = True,
**kwargs,
Expand Down
9 changes: 9 additions & 0 deletions datatree/tests/test_io.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import zarr.errors

from datatree.io import open_datatree
from datatree.testing import assert_equal
Expand Down Expand Up @@ -109,3 +110,11 @@ def test_to_zarr_not_consolidated(self, tmpdir, simple_datatree):
with pytest.warns(RuntimeWarning, match="consolidated"):
roundtrip_dt = open_datatree(filepath, engine="zarr")
assert_equal(original_dt, roundtrip_dt)

@requires_zarr
def test_to_zarr_default_write_mode(self, tmpdir, simple_datatree):
simple_datatree.to_zarr(tmpdir)

# with default settings, to_zarr should not overwrite an existing dir
with pytest.raises(zarr.errors.ContainsGroupError):
simple_datatree.to_zarr(tmpdir)
4 changes: 4 additions & 0 deletions docs/source/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ New Features
Breaking changes
~~~~~~~~~~~~~~~~

- Change default write mode of :py:meth:`DataTree.to_zarr` to ``'w-'`` to match ``xarray``
default and prevent accidental directory overwrites. (:issue:`274`, :pull:`275`)
By `Sam Levang <https://github.com/slevang>`_.

Deprecations
~~~~~~~~~~~~

Expand Down