From a2a5236a7a7d96ea018ad5d2d56cd0e32e954835 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sat, 25 Nov 2023 13:00:16 -0800 Subject: [PATCH 1/4] Fix Zarr region transpose This wasn't working on an unregion-ed write; I think because `new_var` was being lost. --- xarray/backends/zarr.py | 8 +++----- xarray/tests/test_backends.py | 9 ++++++++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/xarray/backends/zarr.py b/xarray/backends/zarr.py index 6632e40cf6f..1f1f6f03387 100644 --- a/xarray/backends/zarr.py +++ b/xarray/backends/zarr.py @@ -624,12 +624,10 @@ def store( variables_encoded.update(vars_with_encoding) for var_name in existing_variable_names: - new_var = variables_encoded[var_name] - existing_var = existing_vars[var_name] - new_var = _validate_and_transpose_existing_dims( + variables_encoded[var_name] = _validate_and_transpose_existing_dims( var_name, - new_var, - existing_var, + variables_encoded[var_name], + existing_vars[var_name], self._write_region, self._append_dim, ) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 85248b5c40a..2689f4b73db 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -5438,7 +5438,14 @@ def test_zarr_region_transpose(tmp_path): ) ds.to_zarr(tmp_path / "test.zarr") - ds_region = 1 + ds.isel(x=[0], y=[0]).transpose() + ds_transposed = ds.transpose('y','x') + + ds_region = 1 + ds_transposed.isel(x=[0], y=[0]) ds_region.to_zarr( tmp_path / "test.zarr", region={"x": slice(0, 1), "y": slice(0, 1)} ) + + # Write without region + ds_transposed.to_zarr(tmp_path / "test.zarr", mode="r+") + + From 4e3533185c6e64e992f9feb27e1a80acc8d792d1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 25 Nov 2023 21:02:10 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- xarray/tests/test_backends.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 2689f4b73db..ecf1f8595d6 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -5438,7 +5438,7 @@ def test_zarr_region_transpose(tmp_path): ) ds.to_zarr(tmp_path / "test.zarr") - ds_transposed = ds.transpose('y','x') + ds_transposed = ds.transpose("y", "x") ds_region = 1 + ds_transposed.isel(x=[0], y=[0]) ds_region.to_zarr( @@ -5447,5 +5447,3 @@ def test_zarr_region_transpose(tmp_path): # Write without region ds_transposed.to_zarr(tmp_path / "test.zarr", mode="r+") - - From 7f90afc59fcbdd0d152b5119bdd2996d86223959 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sat, 25 Nov 2023 13:03:30 -0800 Subject: [PATCH 3/4] --- doc/whats-new.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index d92f3239f60..6d769bbc567 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -43,6 +43,8 @@ Bug fixes ~~~~~~~~~ - Fix dtype inference for ``pd.CategoricalIndex`` when categories are backed by a ``pd.ExtensionDtype`` (:pull:`8481`) +- Fix writing a variable that requires transposing when not writing to a region (:pull:`8484`) + By `Maximilian Roos `_. Documentation From 96478fdd20aff7db8a150c48110de71a55bf7d5c Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Mon, 27 Nov 2023 11:26:29 -0800 Subject: [PATCH 4/4] --- xarray/tests/test_backends.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index ecf1f8595d6..0704dd835c0 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -5423,7 +5423,7 @@ def test_zarr_region_append(self, tmp_path): @requires_zarr -def test_zarr_region_transpose(tmp_path): +def test_zarr_region(tmp_path): x = np.arange(0, 50, 10) y = np.arange(0, 20, 2) data = np.ones((5, 10))