Skip to content

Commit b313ffc

Browse files
CarlAnderssonmax-sixtyandersy005
authored
Properly closes zarr groups in zarr store (#8425)
Co-authored-by: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> Co-authored-by: Anderson Banihirwe <13301940+andersy005@users.noreply.github.com>
1 parent b703102 commit b313ffc

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ Bug fixes
174174
- Fix a bug where :py:meth:`DataArray.to_dataset` silently drops a variable
175175
if a coordinate with the same name already exists (:pull:`8433`, :issue:`7823`).
176176
By `András Gunyhó <https://github.com/mgunyho>`_.
177+
- Fix for :py:meth:`DataArray.to_zarr` & :py:meth:`Dataset.to_zarr` to close
178+
the created zarr store when passing a path with `.zip` extension (:pull:`8425`).
179+
By `Carl Andersson <https://github.com/CarlAndersson>_`.
177180

178181
Documentation
179182
~~~~~~~~~~~~~

xarray/backends/zarr.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ class ZarrStore(AbstractWritableDataStore):
381381
"_write_region",
382382
"_safe_chunks",
383383
"_write_empty",
384+
"_close_store_on_close",
384385
)
385386

386387
@classmethod
@@ -464,6 +465,7 @@ def open_group(
464465
zarr_group = zarr.open_consolidated(store, **open_kwargs)
465466
else:
466467
zarr_group = zarr.open_group(store, **open_kwargs)
468+
close_store_on_close = zarr_group.store is not store
467469
return cls(
468470
zarr_group,
469471
mode,
@@ -472,6 +474,7 @@ def open_group(
472474
write_region,
473475
safe_chunks,
474476
write_empty,
477+
close_store_on_close,
475478
)
476479

477480
def __init__(
@@ -483,6 +486,7 @@ def __init__(
483486
write_region=None,
484487
safe_chunks=True,
485488
write_empty: bool | None = None,
489+
close_store_on_close: bool = False,
486490
):
487491
self.zarr_group = zarr_group
488492
self._read_only = self.zarr_group.read_only
@@ -494,6 +498,7 @@ def __init__(
494498
self._write_region = write_region
495499
self._safe_chunks = safe_chunks
496500
self._write_empty = write_empty
501+
self._close_store_on_close = close_store_on_close
497502

498503
@property
499504
def ds(self):
@@ -762,7 +767,8 @@ def set_variables(self, variables, check_encoding_set, writer, unlimited_dims=No
762767
writer.add(v.data, zarr_array, region)
763768

764769
def close(self):
765-
pass
770+
if self._close_store_on_close:
771+
self.zarr_group.store.close()
766772

767773

768774
def open_zarr(

xarray/tests/test_backends.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5251,6 +5251,16 @@ def test_pickle_open_mfdataset_dataset():
52515251
assert_identical(ds, pickle.loads(pickle.dumps(ds)))
52525252

52535253

5254+
@requires_zarr
5255+
def test_zarr_closing_internal_zip_store():
5256+
store_name = "tmp.zarr.zip"
5257+
original_da = DataArray(np.arange(12).reshape((3, 4)))
5258+
original_da.to_zarr(store_name, mode="w")
5259+
5260+
with open_dataarray(store_name, engine="zarr") as loaded_da:
5261+
assert_identical(original_da, loaded_da)
5262+
5263+
52545264
@requires_zarr
52555265
class TestZarrRegionAuto:
52565266
def test_zarr_region_auto_all(self, tmp_path):

0 commit comments

Comments
 (0)