Skip to content

Commit cf87ba4

Browse files
authored
Raise ValueError for unmatching chunks length in DataArray.chunk() (#9689)
* add `ValueError` * add test
1 parent dbb98b4 commit cf87ba4

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

xarray/core/dataarray.py

+5
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,11 @@ def chunk(
14441444
"It will raise an error in the future. Instead use a dict with dimension names as keys.",
14451445
category=DeprecationWarning,
14461446
)
1447+
if len(chunks) != len(self.dims):
1448+
raise ValueError(
1449+
f"chunks must have the same number of elements as dimensions. "
1450+
f"Expected {len(self.dims)} elements, got {len(chunks)}."
1451+
)
14471452
chunk_mapping = dict(zip(self.dims, chunks, strict=True))
14481453
else:
14491454
chunk_mapping = either_dict_or_kwargs(chunks, chunks_kwargs, "chunk")

xarray/tests/test_dataarray.py

+3
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,9 @@ def test_chunk(self) -> None:
897897
assert blocked.chunks == ((3,), (3, 1))
898898
assert blocked.data.name != first_dask_name
899899

900+
with pytest.raises(ValueError):
901+
blocked.chunk(chunks=(3, 3, 3))
902+
900903
# name doesn't change when rechunking by same amount
901904
# this fails if ReprObject doesn't have __dask_tokenize__ defined
902905
assert unblocked.chunk(2).data.name == unblocked.chunk(2).data.name

0 commit comments

Comments
 (0)