From cf87ba4ba7a43727b94eaafa25bf9fcd368b43c2 Mon Sep 17 00:00:00 2001 From: Lukas Trippe Date: Mon, 28 Oct 2024 22:57:12 +0100 Subject: [PATCH] Raise `ValueError` for unmatching chunks length in `DataArray.chunk()` (#9689) * add `ValueError` * add test --- xarray/core/dataarray.py | 5 +++++ xarray/tests/test_dataarray.py | 3 +++ 2 files changed, 8 insertions(+) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index db7824d8c90..c6bc082f5ed 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -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") diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 78db39c194e..bff5ca8298d 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -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