Skip to content
Open
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
14 changes: 14 additions & 0 deletions xarray/backends/zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,20 @@ def _create_new_array(
if c in encoding:
encoding["config"][c] = encoding.pop(c)

else:
from zarr.util import normalize_dtype

_, object_codec = normalize_dtype(dtype, None)

if object_codec is not None:
existing_filters = encoding.get("filters")
if (
existing_filters is not None
and len(existing_filters) == 1
and existing_filters[0] == object_codec
):
del encoding["filters"]

zarr_array = self.zarr_group.create(
name,
shape=shape,
Expand Down
12 changes: 11 additions & 1 deletion xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@
except ImportError:
pass


if has_zarr:
import zarr
import zarr.codecs
Expand Down Expand Up @@ -3075,6 +3074,17 @@ def test_zarr_mode_w_overwrites_encoding(self) -> None:
zarr.open_group(store, **self.version_kwargs)["foo"], data.foo.data
)

def test_object_codec(self) -> None:
data = xr.DataArray(
data=np.zeros((2, 2)),
dims=["x", "y"],
coords=dict(y=np.array(["a", "b"], dtype=object)),
)
with create_tmp_file() as path1:
data.to_zarr(path1, mode="w")
data = xr.open_zarr(path1)
data.to_zarr(path1, mode="w")

def test_encoding_kwarg_fixed_width_string(self) -> None:
# not relevant for zarr, since we don't use EncodedStringCoder
pass
Expand Down
Loading