diff --git a/docs/src/whatsnew/latest.rst b/docs/src/whatsnew/latest.rst index 885be5d06e..e041c8810c 100644 --- a/docs/src/whatsnew/latest.rst +++ b/docs/src/whatsnew/latest.rst @@ -117,6 +117,9 @@ This document explains the changes made to Iris for this release as well as some long-standing bugs with vertical coordinates and number formats. (:pull:`4411`) +#. `@rcomer`_ fixed :meth:`~iris.cube.Cube.subset` to alway return ``None`` if + no value match is found. (:pull:`4417`) + #. `@wjbenfold`_ resolved an issue that previously caused regridding with lazy data to take significantly longer than with real data. Relevant benchmark shows a time decrease from >10s to 625ms. (:issue:`4280`, :pull:`4400`) diff --git a/lib/iris/cube.py b/lib/iris/cube.py index b64c4ed56a..c03d092f9f 100644 --- a/lib/iris/cube.py +++ b/lib/iris/cube.py @@ -2579,6 +2579,10 @@ def subset(self, coord): coord, return_indices=True ) + if coord_indices.size == 0: + # No matches found. + return + # Build up a slice which spans the whole of the cube full_slice = [slice(None, None)] * len(self.shape) # Update the full slice to only extract specific indices which diff --git a/lib/iris/tests/unit/cube/test_Cube.py b/lib/iris/tests/unit/cube/test_Cube.py index b6856bd5f2..b5ce2f420e 100644 --- a/lib/iris/tests/unit/cube/test_Cube.py +++ b/lib/iris/tests/unit/cube/test_Cube.py @@ -2585,6 +2585,15 @@ def test_different_coordinate(self): result = cube.subset(different_coord) self.assertEqual(result, None) + def test_different_coordinate_vector(self): + cube = Cube([0, 1], long_name="raspberry", units="1") + cube.add_dim_coord( + DimCoord([0, 1], long_name="loganberry", units="1"), 0 + ) + different_coord = DimCoord([2], long_name="loganberry", units="1") + result = cube.subset(different_coord) + self.assertEqual(result, None) + def test_not_coordinate(self): cube = Cube(0, long_name="peach", units="1") cube.add_aux_coord(DimCoord([0], long_name="crocodile", units="1"))