From 0b2a47df78edd7a9b71807243d03b32a5b8bc93e Mon Sep 17 00:00:00 2001 From: Sam Levang Date: Mon, 4 Mar 2024 09:03:54 -0500 Subject: [PATCH 1/5] pass name through encode_variable --- xarray/backends/common.py | 6 +++--- xarray/backends/h5netcdf_.py | 2 +- xarray/backends/netCDF4_.py | 2 +- xarray/backends/scipy_.py | 2 +- xarray/backends/zarr.py | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/xarray/backends/common.py b/xarray/backends/common.py index 7d3cc00a52d..aa75dc56765 100644 --- a/xarray/backends/common.py +++ b/xarray/backends/common.py @@ -329,11 +329,11 @@ def encode(self, variables, attributes): attributes : dict-like """ - variables = {k: self.encode_variable(v) for k, v in variables.items()} + variables = {k: self.encode_variable(v, k) for k, v in variables.items()} attributes = {k: self.encode_attribute(v) for k, v in attributes.items()} return variables, attributes - def encode_variable(self, v): + def encode_variable(self, v, name): """encode one variable""" return v @@ -480,7 +480,7 @@ def encode(self, variables, attributes): # All NetCDF files get CF encoded by default, without this attempting # to write times, for example, would fail. variables, attributes = cf_encoder(variables, attributes) - variables = {k: self.encode_variable(v) for k, v in variables.items()} + variables = {k: self.encode_variable(v, k) for k, v in variables.items()} attributes = {k: self.encode_attribute(v) for k, v in attributes.items()} return variables, attributes diff --git a/xarray/backends/h5netcdf_.py b/xarray/backends/h5netcdf_.py index b7c1b2a5f03..3616848bb26 100644 --- a/xarray/backends/h5netcdf_.py +++ b/xarray/backends/h5netcdf_.py @@ -260,7 +260,7 @@ def set_dimension(self, name, length, is_unlimited=False): def set_attribute(self, key, value): self.ds.attrs[key] = value - def encode_variable(self, variable): + def encode_variable(self, variable, name): return _encode_nc4_variable(variable) def prepare_variable( diff --git a/xarray/backends/netCDF4_.py b/xarray/backends/netCDF4_.py index 6720a67ae2f..f4356f3dfb0 100644 --- a/xarray/backends/netCDF4_.py +++ b/xarray/backends/netCDF4_.py @@ -490,7 +490,7 @@ def set_attribute(self, key, value): else: self.ds.setncattr(key, value) - def encode_variable(self, variable): + def encode_variable(self, variable, name): variable = _force_native_endianness(variable) if self.format == "NETCDF4": variable = _encode_nc4_variable(variable) diff --git a/xarray/backends/scipy_.py b/xarray/backends/scipy_.py index 1ecc70cf376..81f8f5c20dd 100644 --- a/xarray/backends/scipy_.py +++ b/xarray/backends/scipy_.py @@ -209,7 +209,7 @@ def set_attribute(self, key, value): value = encode_nc3_attr_value(value) setattr(self.ds, key, value) - def encode_variable(self, variable): + def encode_variable(self, variable, name): variable = encode_nc3_variable(variable) return variable diff --git a/xarray/backends/zarr.py b/xarray/backends/zarr.py index e9465dc0ba0..d3920c1e875 100644 --- a/xarray/backends/zarr.py +++ b/xarray/backends/zarr.py @@ -569,8 +569,8 @@ def set_dimensions(self, variables, unlimited_dims=None): def set_attributes(self, attributes): _put_attrs(self.zarr_group, attributes) - def encode_variable(self, variable): - variable = encode_zarr_variable(variable) + def encode_variable(self, variable, name): + variable = encode_zarr_variable(variable, name=name) return variable def encode_attribute(self, a): From c3f96debe0fd15fafe32c03c3adbc1e912d6ba8d Mon Sep 17 00:00:00 2001 From: Sam Levang Date: Wed, 6 Mar 2024 11:07:03 -0500 Subject: [PATCH 2/5] add test --- xarray/tests/test_backends.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 668d14b86c9..f30365cbf54 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -1251,6 +1251,16 @@ def test_multiindex_not_implemented(self) -> None: with self.roundtrip(ds_reset) as actual: assert_identical(actual, ds_reset) + def test_reset_multiindex_as_indexvariable(self) -> None: + # regression noted in https://github.com/xarray-contrib/xeofs/issues/148 + ds = xr.Dataset(coords={"dim1": [1, 2, 3]}) + ds = ds.stack(feature=["dim1"]) + ds_reset = ds.reset_index("feature") + # Convert dim1 back to an IndexVariable, should still be able to serialize + ds_reset["dim1"] = ds_reset.dim1.variable.to_index_variable() + with self.roundtrip(ds_reset) as actual: + assert_identical(actual, ds_reset) + class NetCDFBase(CFEncodedBase): """Tests for all netCDF3 and netCDF4 backends.""" From e3990e449172e560f17b4e803df48edd09d471df Mon Sep 17 00:00:00 2001 From: Sam Levang Date: Wed, 6 Mar 2024 11:58:38 -0500 Subject: [PATCH 3/5] fix conventions tests --- xarray/tests/test_conventions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_conventions.py b/xarray/tests/test_conventions.py index 91a8e368de5..38cf3816b5f 100644 --- a/xarray/tests/test_conventions.py +++ b/xarray/tests/test_conventions.py @@ -449,7 +449,7 @@ def test_decode_cf_time_kwargs(self) -> None: class CFEncodedInMemoryStore(WritableCFDataStore, InMemoryDataStore): - def encode_variable(self, var): + def encode_variable(self, var, name): """encode one variable""" coder = coding.strings.EncodedStringCoder(allows_unicode=True) var = coder.encode(var) From 3cfe948ec3093606aae3480f2992fdd717d7cf40 Mon Sep 17 00:00:00 2001 From: Sam Levang <39069044+slevang@users.noreply.github.com> Date: Tue, 26 Mar 2024 19:50:33 -0400 Subject: [PATCH 4/5] Update xarray/backends/common.py Co-authored-by: Joe Hamman --- xarray/backends/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/backends/common.py b/xarray/backends/common.py index aa75dc56765..281a43b4586 100644 --- a/xarray/backends/common.py +++ b/xarray/backends/common.py @@ -333,7 +333,7 @@ def encode(self, variables, attributes): attributes = {k: self.encode_attribute(v) for k, v in attributes.items()} return variables, attributes - def encode_variable(self, v, name): + def encode_variable(self, v, name: str | None = None): """encode one variable""" return v From 0fd34adefa43f2dc77ae39b875ef658d613b36f6 Mon Sep 17 00:00:00 2001 From: Sam Levang Date: Tue, 26 Mar 2024 19:56:38 -0400 Subject: [PATCH 5/5] add whats new --- doc/whats-new.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index c644f7615b7..00e969df160 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -71,7 +71,8 @@ Bug fixes - Warn and return bytes undecoded in case of UnicodeDecodeError in h5netcdf-backend (:issue:`5563`, :pull:`8874`). By `Kai Mühlbauer `_. - +- Fix a regression when saving dropped multi-indexes with the zarr backend (:pull:`8809`). + By `Sam Levang `_. Documentation ~~~~~~~~~~~~~