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

Commit

Permalink
Change default write mode of to_zarr() to 'w-' (#275)
Browse files Browse the repository at this point in the history
* change default to_zarr mode to w-

* regression test

* add whats new entry
  • Loading branch information
slevang authored Nov 10, 2023
1 parent 5049e44 commit 433f78d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
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

0 comments on commit 433f78d

Please sign in to comment.