Skip to content

fix: dask divide by zero error when empty dimensions are passed into partial autochunk spec - #11486

Open
charles-turner-1 wants to merge 5 commits into
pydata:mainfrom
charles-turner-1:empty-partial-chunk
Open

fix: dask divide by zero error when empty dimensions are passed into partial autochunk spec#11486
charles-turner-1 wants to merge 5 commits into
pydata:mainfrom
charles-turner-1:empty-partial-chunk

Conversation

@charles-turner-1

@charles-turner-1 charles-turner-1 commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Description

I discovered this whilst playing with charles-turner-1/xrexpr#121 (expression rewriting).

TLDR; auto-chunking on zero length dimensions fails, because dask divides by zero:

import numpy as np

import xarray as xr

da = xr.DataArray(
    np.zeros((5, 1)),
    dims=("lat", "lon"),
).isel(lat=[])

# This is fine
da.chunk("auto")

# This raises
da.chunk({"lon": "auto"})

I've hidden the traceback in a detail block because it's pretty long.

Details
Traceback (most recent call last):
  File "/Users/u1166368/xarray/bug_repro/exercise_broken_auto.py", line 14, in <module>
    da.chunk({"lon": "auto"})
    ~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/Users/u1166368/dask/.pixi/envs/default/lib/python3.14/site-packages/xarray/core/dataarray.py", line 1498, in chunk
    ds = self._to_temp_dataset().chunk(
        chunk_mapping,
    ...<5 lines>...
        from_array_kwargs=from_array_kwargs,
    )
  File "/Users/u1166368/dask/.pixi/envs/default/lib/python3.14/site-packages/xarray/core/dataset.py", line 2638, in chunk
    k: _maybe_chunk(
       ~~~~~~~~~~~~^
        k,
        ^^
    ...<7 lines>...
        from_array_kwargs=from_array_kwargs.copy(),
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/Users/u1166368/dask/.pixi/envs/default/lib/python3.14/site-packages/xarray/structure/chunks.py", line 105, in _maybe_chunk
    var = var.chunk(
        chunks,
        chunked_array_type=chunked_array_type,
        from_array_kwargs=from_array_kwargs,
    )
  File "/Users/u1166368/dask/.pixi/envs/default/lib/python3.14/site-packages/xarray/core/variable.py", line 2740, in chunk
    return super().chunk(
           ~~~~~~~~~~~~~^
        chunks=chunks,
        ^^^^^^^^^^^^^^
    ...<2 lines>...
        **chunks_kwargs,
        ^^^^^^^^^^^^^^^^
    )
    ^
  File "/Users/u1166368/dask/.pixi/envs/default/lib/python3.14/site-packages/xarray/namedarray/core.py", line 851, in chunk
    data_chunked = chunkmanager.from_array(ndata, chunks, **from_array_kwargs)  # type: ignore[arg-type]
  File "/Users/u1166368/dask/.pixi/envs/default/lib/python3.14/site-packages/xarray/namedarray/daskmanager.py", line 74, in from_array
    return da.from_array(
           ~~~~~~~~~~~~~^
        data,
        ^^^^^
        chunks,
        ^^^^^^^
        **kwargs,
        ^^^^^^^^^
    )  # type: ignore[no-untyped-call]
    ^
  File "/Users/u1166368/dask/dask/array/core.py", line 3669, in from_array
    chunks = normalize_chunks(
        chunks, x.shape, dtype=x.dtype, previous_chunks=previous_chunks
    )
  File "/Users/u1166368/dask/dask/array/core.py", line 3200, in normalize_chunks
    chunks = auto_chunks(chunks, shape, limit, dtype, previous_chunks)
  File "/Users/u1166368/dask/dask/array/core.py", line 3414, in auto_chunks
    size = (limit / dtype.itemsize / largest_block) ** (1 / len(autos))
            ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
ZeroDivisionError: division by zero

I haven't opened an issue for this - I could see a straightforward fix so I just got cracking on it, but I did have a look and I couldn't find any extant issues mentioning it. Happy to open one for clarity!

I also stuck the test right below the chunking tests in the dataarray section - not totally sure it's the best place for it, open to suggestions!

  • Not sure if this really counts as user facing?

Checklist

  • Closes #xxxx
  • Tests added
  • User visible changes (including notable bug fixes) are documented in whats-new.rst
  • New functions/methods are listed in api.rst N/A

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR. N/A
    • I take responsibility for any AI-generated content in my PR. N/A

@welcome

welcome Bot commented Jul 31, 2026

Copy link
Copy Markdown

Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient.
If you have questions, some answers may be found in our contributing guidelines.

@charles-turner-1 charles-turner-1 changed the title Empty partial chunk Fix dask divide by zero error when empty dimensions are passed into partial autochunk spec Jul 31, 2026
@charles-turner-1 charles-turner-1 changed the title Fix dask divide by zero error when empty dimensions are passed into partial autochunk spec fix: dask divide by zero error when empty dimensions are passed into partial autochunk spec Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant