Skip to content

Commit b703102

Browse files
dcherianandersy005
andauthored
Minor to_zarr optimizations (#8489)
Co-authored-by: Anderson Banihirwe <13301940+andersy005@users.noreply.github.com>
1 parent d46c5b6 commit b703102

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

xarray/backends/zarr.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,9 @@ def store(
599599
"""
600600
import zarr
601601

602+
existing_keys = tuple(self.zarr_group.array_keys())
602603
existing_variable_names = {
603-
vn for vn in variables if _encode_variable_name(vn) in self.zarr_group
604+
vn for vn in variables if _encode_variable_name(vn) in existing_keys
604605
}
605606
new_variables = set(variables) - existing_variable_names
606607
variables_without_encoding = {vn: variables[vn] for vn in new_variables}
@@ -665,6 +666,8 @@ def set_variables(self, variables, check_encoding_set, writer, unlimited_dims=No
665666

666667
import zarr
667668

669+
existing_keys = tuple(self.zarr_group.array_keys())
670+
668671
for vn, v in variables.items():
669672
name = _encode_variable_name(vn)
670673
check = vn in check_encoding_set
@@ -677,7 +680,7 @@ def set_variables(self, variables, check_encoding_set, writer, unlimited_dims=No
677680
if v.encoding == {"_FillValue": None} and fill_value is None:
678681
v.encoding = {}
679682

680-
if name in self.zarr_group:
683+
if name in existing_keys:
681684
# existing variable
682685
# TODO: if mode="a", consider overriding the existing variable
683686
# metadata. This would need some case work properly with region

0 commit comments

Comments
 (0)