Skip to content

Return an empty result from rolling and cumulative on a zero-length dimension - #11572

Open
chiruu12 wants to merge 3 commits into
pydata:mainfrom
chiruu12:fix/cumulative-empty-dimension
Open

Return an empty result from rolling and cumulative on a zero-length dimension#11572
chiruu12 wants to merge 3 commits into
pydata:mainfrom
chiruu12:fix/cumulative-empty-dimension

Conversation

@chiruu12

@chiruu12 chiruu12 commented Sep 7, 2026

Copy link
Copy Markdown

cumulative derives its window from the size of the dimension, so on a dimension of
length 0 it asks Rolling for a window of 0 and gets back `ValueError: window must be

0, about a window the caller never chose. cumsum` returns an empty result for the
same input.

da = xr.DataArray(np.arange(6.0).reshape(2, 3), dims=("x", "y"))
empty = da.isel(x=slice(0, 0))

empty.cumsum("x")               # <xarray.DataArray (x: 0, y: 3)>
empty.cumulative("x").sum()     # ValueError: window must be > 0

An empty selection is ordinary, so this shows up on data that happens to filter down to
nothing rather than on anything malformed.

The window guard is right for a window the caller picked, so cumulative now floors its
derived window at 1 instead. That alone was not enough. rolling_window pads by
win - 1, which leaves a zero-length dimension shorter than the window, and
sliding_window_view refuses that outright, so plain rolling failed on an empty
dimension too:

empty.rolling(x=1).mean()       # ValueError: window shape cannot be larger than input array shape

A zero-length dimension yields zero windows. Padding it out to one full window gives
sliding_window_view something it accepts, and the one window it produces is sliced back
off. Slicing rather than building a fresh empty array keeps the data in whatever
namespace it arrived in.

Not fixed here: the same call on a dask-backed array still fails, in dask.array.pad
with ZeroDivisionError for a window above 1 and The overlapping depth 1 is larger than your array 0 for a window of 1. Both reproduce on an unmodified main, so this is a dask
limitation upstream of the change rather than something it introduced. Happy to look at
it separately if you want it in scope.

Tests cover both entry points, both container types, dim as a string and as a list,
centered and not, windows of 1 and 3, and two empty dimensions at once. All eight fail on
the commit before the fix.

test_rolling.py test_dataarray.py test_dataset.py test_computation.py: 4300 passed. Three
failures in test_dataset.py (test_sel, two test_chunk_by_season_resampler) reproduce
on an unmodified main and look like a local pandas version. test_variable.py does not
collect on main here for the same reason. ruff check and ruff format --check clean.

Copilot AI lite review requested due to automatic review settings September 7, 2026 10:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants