Skip to content

Commit

Permalink
Raise ValueError for unmatching chunks length in DataArray.chunk() (
Browse files Browse the repository at this point in the history
#9689)

* add `ValueError`

* add test
  • Loading branch information
lkstrp authored Oct 28, 2024
1 parent dbb98b4 commit cf87ba4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
5 changes: 5 additions & 0 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,11 @@ def chunk(
"It will raise an error in the future. Instead use a dict with dimension names as keys.",
category=DeprecationWarning,
)
if len(chunks) != len(self.dims):
raise ValueError(
f"chunks must have the same number of elements as dimensions. "
f"Expected {len(self.dims)} elements, got {len(chunks)}."
)
chunk_mapping = dict(zip(self.dims, chunks, strict=True))
else:
chunk_mapping = either_dict_or_kwargs(chunks, chunks_kwargs, "chunk")
Expand Down
3 changes: 3 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,9 @@ def test_chunk(self) -> None:
assert blocked.chunks == ((3,), (3, 1))
assert blocked.data.name != first_dask_name

with pytest.raises(ValueError):
blocked.chunk(chunks=(3, 3, 3))

# name doesn't change when rechunking by same amount
# this fails if ReprObject doesn't have __dask_tokenize__ defined
assert unblocked.chunk(2).data.name == unblocked.chunk(2).data.name
Expand Down

0 comments on commit cf87ba4

Please sign in to comment.