From 3ea1af7b0f18a1fc72ef622f48d33e72a26883ef Mon Sep 17 00:00:00 2001 From: Ruth Comer Date: Sun, 14 Nov 2021 12:39:22 +0000 Subject: [PATCH 1/3] add failing test --- lib/iris/tests/unit/cube/test_Cube.py | 9 +++++++++ 1 file changed, 9 insertions(+) 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")) From b8b3423f54315db3b009a3fa7b28f2fa61b147c8 Mon Sep 17 00:00:00 2001 From: Ruth Comer Date: Sun, 14 Nov 2021 12:59:15 +0000 Subject: [PATCH 2/3] pass test --- lib/iris/cube.py | 4 ++++ 1 file changed, 4 insertions(+) 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 From bb85d17c5651619b1c865c617036e2af77fe4893 Mon Sep 17 00:00:00 2001 From: Ruth Comer Date: Sun, 14 Nov 2021 13:07:02 +0000 Subject: [PATCH 3/3] whatsnew --- docs/src/whatsnew/latest.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/src/whatsnew/latest.rst b/docs/src/whatsnew/latest.rst index 33f2e7a699..b8dae9f9f2 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`) + 💣 Incompatible Changes =======================