diff --git a/xarray/namedarray/core.py b/xarray/namedarray/core.py index c535fcb0bc4..42950e1735c 100644 --- a/xarray/namedarray/core.py +++ b/xarray/namedarray/core.py @@ -835,6 +835,18 @@ def chunk( for dim_number, dim in enumerate(self.dims) if dim in chunks } + # Runs the same iteration as above, could extract to a shared helper + if any(val == "auto" for val in chunks.values()): + # If any chunks are "auto", we can add extra "auto" chunks for any + # zero-length dimensions to avoid dask errors. Need at least one existing + # auto or we might try to auto object dtype arrays incorrectly. + extra_autos = { + dim_number: "auto" + for dim_number, dim in enumerate(self.dims) + if dim_number not in chunks # keys are dim numbers now + and self.sizes[dim] == 0 + } + chunks = {**chunks, **extra_autos} chunkmanager = guess_chunkmanager(chunked_array_type) diff --git a/xarray/tests/test_dask.py b/xarray/tests/test_dask.py index 6b871aa79fb..3c0a4a19550 100644 --- a/xarray/tests/test_dask.py +++ b/xarray/tests/test_dask.py @@ -1210,6 +1210,18 @@ def test_auto_chunk_da(obj): assert actual.chunks == expected.chunks +@pytest.mark.parametrize("obj", [make_da(), make_ds()]) +def test_partial_auto_chunk_zero_len_dim(obj): + obj = obj.isel(x=[]) + obj.chunk({"y": "auto"}) + + +@pytest.mark.parametrize("obj", [make_ds()]) +def test_partial_auto_chunk_zero_len_dim_variable(obj): + obj = obj.isel(x=[])["a"].variable + obj.chunk({"y": "auto"}) + + def test_auto_chunk_da_cftime(): yrs = np.arange(2000, 2120) cftime_dates = xr.date_range(