From 8534cadfb0d3184f1faa9ce8dbe0ea69fdca4f84 Mon Sep 17 00:00:00 2001 From: Joshua Gould Date: Thu, 11 Sep 2025 10:14:58 -0400 Subject: [PATCH] Fix for encoding objects for zarr<3 --- xarray/backends/zarr.py | 14 ++++++++++++++ xarray/tests/test_backends.py | 12 +++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/xarray/backends/zarr.py b/xarray/backends/zarr.py index f0578ca9352..773f9f218e9 100644 --- a/xarray/backends/zarr.py +++ b/xarray/backends/zarr.py @@ -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, diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 0f4debada29..0d8c1dd01c9 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -121,7 +121,6 @@ except ImportError: pass - if has_zarr: import zarr import zarr.codecs @@ -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