Skip to content

fix KeyError when append_dim is not None in to_zarr for empty datastore #4531

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

Closed
wants to merge 1 commit into from
Closed
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
30 changes: 15 additions & 15 deletions xarray/backends/zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,22 +499,22 @@ def set_variables(self, variables, check_encoding_set, writer, unlimited_dims=No
if v.encoding == {"_FillValue": None} and fill_value is None:
v.encoding = {}

if self.append_dim is not None and self.append_dim in dims:
# resize existing variable
if name in self.ds:
zarr_array = self.ds[name]
append_axis = dims.index(self.append_dim)

new_region = [slice(None)] * len(dims)
new_region[append_axis] = slice(zarr_array.shape[append_axis], None)
region = tuple(new_region)

new_shape = list(zarr_array.shape)
new_shape[append_axis] += v.shape[append_axis]
zarr_array.resize(new_shape)
elif name in self.ds:
# override existing variable
zarr_array = self.ds[name]
region = None
if self.append_dim is not None and self.append_dim in dims:
# resize existing variable
append_axis = dims.index(self.append_dim)

new_region = [slice(None)] * len(dims)
new_region[append_axis] = slice(zarr_array.shape[append_axis], None)
region = tuple(new_region)

new_shape = list(zarr_array.shape)
new_shape[append_axis] += v.shape[append_axis]
zarr_array.resize(new_shape)
else:
# override existing variable
region = None
else:
# new variable
encoding = extract_zarr_variable_encoding(
Expand Down