Skip to content

Commit

Permalink
Fix Zarr region transpose (pydata#8484)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
max-sixty and pre-commit-ci[bot] authored Nov 27, 2023
1 parent 633e66a commit d54c461
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/max-sixty>`_.


Documentation
Expand Down
8 changes: 3 additions & 5 deletions xarray/backends/zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
9 changes: 7 additions & 2 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -5438,7 +5438,12 @@ 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+")

0 comments on commit d54c461

Please sign in to comment.