Skip to content

Commit d54c461

Browse files
Fix Zarr region transpose (#8484)
* Fix Zarr region transpose This wasn't working on an unregion-ed write; I think because `new_var` was being lost. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 633e66a commit d54c461

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ Bug fixes
4343
~~~~~~~~~
4444

4545
- Fix dtype inference for ``pd.CategoricalIndex`` when categories are backed by a ``pd.ExtensionDtype`` (:pull:`8481`)
46+
- Fix writing a variable that requires transposing when not writing to a region (:pull:`8484`)
47+
By `Maximilian Roos <https://github.com/max-sixty>`_.
4648

4749

4850
Documentation

xarray/backends/zarr.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -624,12 +624,10 @@ def store(
624624
variables_encoded.update(vars_with_encoding)
625625

626626
for var_name in existing_variable_names:
627-
new_var = variables_encoded[var_name]
628-
existing_var = existing_vars[var_name]
629-
new_var = _validate_and_transpose_existing_dims(
627+
variables_encoded[var_name] = _validate_and_transpose_existing_dims(
630628
var_name,
631-
new_var,
632-
existing_var,
629+
variables_encoded[var_name],
630+
existing_vars[var_name],
633631
self._write_region,
634632
self._append_dim,
635633
)

xarray/tests/test_backends.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5423,7 +5423,7 @@ def test_zarr_region_append(self, tmp_path):
54235423

54245424

54255425
@requires_zarr
5426-
def test_zarr_region_transpose(tmp_path):
5426+
def test_zarr_region(tmp_path):
54275427
x = np.arange(0, 50, 10)
54285428
y = np.arange(0, 20, 2)
54295429
data = np.ones((5, 10))
@@ -5438,7 +5438,12 @@ def test_zarr_region_transpose(tmp_path):
54385438
)
54395439
ds.to_zarr(tmp_path / "test.zarr")
54405440

5441-
ds_region = 1 + ds.isel(x=[0], y=[0]).transpose()
5441+
ds_transposed = ds.transpose("y", "x")
5442+
5443+
ds_region = 1 + ds_transposed.isel(x=[0], y=[0])
54425444
ds_region.to_zarr(
54435445
tmp_path / "test.zarr", region={"x": slice(0, 1), "y": slice(0, 1)}
54445446
)
5447+
5448+
# Write without region
5449+
ds_transposed.to_zarr(tmp_path / "test.zarr", mode="r+")

0 commit comments

Comments
 (0)