diff --git a/changelog/7123.bugfix.rst b/changelog/7123.bugfix.rst new file mode 100644 index 0000000000..d416a902e3 --- /dev/null +++ b/changelog/7123.bugfix.rst @@ -0,0 +1,3 @@ +:user:`gaoflow` fixed :meth:`iris.cube.Cube.rolling_window` so that it no +longer raises a ``TypeError`` when the cube has a lazy auxiliary coordinate +along the windowed dimension. (:issue:`6480`) diff --git a/lib/iris/cube.py b/lib/iris/cube.py index 44be3a63d7..4ddba3781f 100644 --- a/lib/iris/cube.py +++ b/lib/iris/cube.py @@ -5243,12 +5243,12 @@ def rolling_window( # window and the bounds are the first and last points in the # window as with numeric coordinates. new_points = np.apply_along_axis(lambda x: "|".join(x), -1, new_bounds) - new_bounds = new_bounds[:, (0, -1)] + new_bounds = new_bounds[:, [0, -1]] else: # Take the first and last element of the rolled window (i.e. # the bounds) and the new points are the midpoints of these # bounds. - new_bounds = new_bounds[:, (0, -1)] + new_bounds = new_bounds[:, [0, -1]] new_points = np.mean(new_bounds, axis=-1) # wipe the coords points and set the bounds diff --git a/lib/iris/tests/unit/cube/test_Cube.py b/lib/iris/tests/unit/cube/test_Cube.py index d91c7e81c0..103aedd558 100644 --- a/lib/iris/tests/unit/cube/test_Cube.py +++ b/lib/iris/tests/unit/cube/test_Cube.py @@ -1086,6 +1086,20 @@ def test_lazy(self): ) _shared_utils.assert_masked_array_equal(expected_result, res_cube.data) + def test_lazy_aux_coord(self): + window = 2 + self.cube.add_aux_coord(AuxCoord(da.arange(6), long_name="lazy_extra"), 0) + res_cube = self.cube.rolling_window("val", iris.analysis.MEAN, window, mdtol=0) + result_coord = res_cube.coord("lazy_extra") + assert result_coord.has_lazy_points() + assert result_coord.has_lazy_bounds() + expected = AuxCoord( + np.array([0.5, 1.5, 2.5, 3.5, 4.5]), + bounds=np.array([[0, 1], [1, 2], [2, 3], [3, 4], [4, 5]]), + long_name="lazy_extra", + ) + assert result_coord == expected + def test_ancillary_variables_and_cell_measures_kept(self, dataless): if dataless: self.cube.data = None