diff --git a/docs/src/whatsnew/dev.rst b/docs/src/whatsnew/dev.rst index 9ed3cb23c2..0ef6c7f65e 100644 --- a/docs/src/whatsnew/dev.rst +++ b/docs/src/whatsnew/dev.rst @@ -54,6 +54,10 @@ This document explains the changes made to Iris for this release #. `@rcomer`_ ensured that a :class:`matplotlib.axes.Axes`'s position is preserved when Iris replaces it with a :class:`cartopy.mpl.geoaxes.GeoAxes`, fixing :issue:`1157`. (:pull:`4273`) + +#. `@rcomer`_ fixed :meth:`~iris.coords.Coord.nearest_neighbour_index` for edge + cases where the requested point is float and the coordinate has integer + bounds, reported at :issue:`2969`. (:pull:`4245`) 💣 Incompatible Changes diff --git a/lib/iris/coords.py b/lib/iris/coords.py index b236d407da..8bcba776fd 100644 --- a/lib/iris/coords.py +++ b/lib/iris/coords.py @@ -2440,7 +2440,9 @@ def nearest_neighbour_index(self, point): if self.has_bounds(): # make bounds ranges complete+separate, so point is in at least one increasing = self.bounds[0, 1] > self.bounds[0, 0] - bounds = bounds.copy() + # identify data type that bounds and point can safely cast to + dtype = np.result_type(bounds, point) + bounds = bounds.astype(dtype) # sort the bounds cells by their centre values sort_inds = np.argsort(np.mean(bounds, axis=1)) bounds = bounds[sort_inds] diff --git a/lib/iris/tests/unit/coords/test_Coord.py b/lib/iris/tests/unit/coords/test_Coord.py index 43170b6c4e..5f707f91db 100644 --- a/lib/iris/tests/unit/coords/test_Coord.py +++ b/lib/iris/tests/unit/coords/test_Coord.py @@ -74,6 +74,11 @@ def test_scalar(self): target = [0, 0, 0, 0, 0] self._test_nearest_neighbour_index(target) + def test_bounded_float_point(self): + coord = DimCoord(1, bounds=[0, 2]) + result = coord.nearest_neighbour_index(2.5) + self.assertEqual(result, 0) + class Test_nearest_neighbour_index__descending(tests.IrisTest): def setUp(self):