Skip to content

Fix Cube.rolling_window with lazy auxiliary coordinates (#6480)#7123

Open
gaoflow wants to merge 1 commit into
SciTools:mainfrom
gaoflow:fix/rolling-window-lazy-aux-coord
Open

Fix Cube.rolling_window with lazy auxiliary coordinates (#6480)#7123
gaoflow wants to merge 1 commit into
SciTools:mainfrom
gaoflow:fix/rolling-window-lazy-aux-coord

Conversation

@gaoflow
Copy link
Copy Markdown

@gaoflow gaoflow commented Jun 1, 2026

🚀 Pull Request

Description

Closes #6480 (and the duplicate #6639).

:meth:iris.cube.Cube.rolling_window raised a TypeError when the cube had a lazy auxiliary coordinate running along the windowed dimension:

import numpy as np, dask.array as da
from iris.coords import DimCoord, AuxCoord
from iris.cube import Cube
from iris.analysis import MEAN

dimco = DimCoord(np.arange(12), standard_name="time", units="s")
auxco = AuxCoord(da.arange(12), long_name="x")   # lazy
cube = Cube(np.arange(12),
            dim_coords_and_dims=[(dimco, 0)],
            aux_coords_and_dims=[(auxco, 0)])
cube.rolling_window(coord="time", window=3, aggregator=MEAN)
TypeError: '>=' not supported between instances of 'tuple' and 'int'

Cause

When building the new coordinate bounds, rolling_window selects the first and last point of each window with:

new_bounds = new_bounds[:, (0, -1)]

A tuple index is interpreted by Dask as a multidimensional index (and so it tries to use 0 and -1 as indices into successive axes), which fails for a lazy array. NumPy happens to tolerate the tuple here, so the bug only surfaces for lazy coordinates.

Fix

Index with the list [0, -1] instead. NumPy and Dask both treat a list as selecting the first and last column, so the result is identical for real arrays and now also works (and stays lazy) for lazy ones. This is the fix hinted at by @pp-mo on the issue. Both the numeric and string-coordinate branches are updated.

Verification

  • New test Test_rolling_window::test_lazy_aux_coord — asserts a lazy aux-coord no longer fails, stays lazy (points and bounds), and equals the real-array result. It fails on main and passes here.
  • Manually confirmed identical results for lazy vs. real, numeric vs. string coordinates.
  • Full Test_rolling_window and tests/unit/util/test_rolling_window.py suites pass (25 tests).
  • ruff check / ruff format clean.

rolling_window built each window's coordinate bounds with new_bounds[:,
(0, -1)]. A tuple index is treated by Dask as a multidimensional index,
so a lazy coordinate along the windowed dimension raised a TypeError.
Index with the list [0, -1] instead, which numpy and Dask both treat as
selecting the first and last column, giving an identical (and still lazy)
result.

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

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Failing rolling window

1 participant