Skip to content
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

Raise ValueError for unmatching chunks length in DataArray.chunk() #9689

Merged
merged 2 commits into from
Oct 28, 2024
Merged
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
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
Loading