Skip to content

allow coordinates to be independent of region selection in to_zarr #6260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ Deprecations
Bug fixes
~~~~~~~~~

- Fix :py:meth:`Dataset.to_zarr` to allow writing datasets when some coordinates of the dataset do not
share a dimension with those given in ``region`` (:issue: `6069`, :pull:`6260`).
By `Hauke Schulz <https://github.com/observingClouds>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion xarray/backends/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ def _validate_region(ds, region):
)

non_matching_vars = [
k for k, v in ds.variables.items() if not set(region).intersection(v.dims)
k for k, v in ds.data_vars.items() if not set(region).intersection(v.dims)
]
if non_matching_vars:
raise ValueError(
Expand Down
26 changes: 26 additions & 0 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -2347,6 +2347,32 @@ def setup_and_verify_store(expected=data):
):
data2.to_zarr(store, region={"x": slice(3)})

def test_write_region_w_coords(self):
data = Dataset(
{"u": (("x", "y"), np.array([[10], [11], [12]]))},
coords={"x": [0, 1, 2], "y": [0], "z": ("x", [10, 11, 12])},
)
data2 = Dataset(
{"u": (("x", "y"), np.array([[13], [14]]))},
coords={"x": [3, 4], "y": [0], "z": ("x", [13, 14])},
)

@contextlib.contextmanager
def setup_and_verify_store(expected=data):
with self.create_zarr_target() as store:
data.to_zarr(store)
yield store
with self.open(store) as actual:
assert_identical(actual, expected)

# verify the base case works
expected = Dataset(
{"u": (("x", "y"), np.array([[13], [14], [12]]))},
coords={"x": [3, 4, 2], "y": [0], "z": ("x", [13, 14, 12])},
)
with setup_and_verify_store(expected) as store:
data2.to_zarr(store, region={"x": slice(2)})

@requires_dask
def test_encoding_chunksizes(self):
# regression test for GH2278
Expand Down