diff --git a/docs/src/whatsnew/latest.rst b/docs/src/whatsnew/latest.rst index 6e6b1b5732..831f24bfab 100644 --- a/docs/src/whatsnew/latest.rst +++ b/docs/src/whatsnew/latest.rst @@ -111,6 +111,8 @@ This document explains the changes made to Iris for this release :func:`~iris.analysis.stats.pearsonr` so it preserves lazy data in all cases and also runs a little faster. (:pull:`5638`) +#. `@bouweandela`_ made comparing coordinates and arrays to themselves faster. (:pull:`5691`) + 🔥 Deprecations =============== diff --git a/lib/iris/coords.py b/lib/iris/coords.py index 89c7434a15..d4d070921c 100644 --- a/lib/iris/coords.py +++ b/lib/iris/coords.py @@ -575,6 +575,9 @@ def __repr__(self): return self.summary(shorten=True) def __eq__(self, other): + if other is self: + return True + # Note: this method includes bounds handling code, but it only runs # within Coord type instances, as only these allow bounds to be set. diff --git a/lib/iris/tests/unit/util/test_array_equal.py b/lib/iris/tests/unit/util/test_array_equal.py index 38b9652443..f63092587c 100644 --- a/lib/iris/tests/unit/util/test_array_equal.py +++ b/lib/iris/tests/unit/util/test_array_equal.py @@ -101,12 +101,15 @@ def test_string_arrays_0d_and_scalar(self): self.assertFalse(array_equal(array_a, "foobar.")) def test_nan_equality_nan_ne_nan(self): - array = np.array([1.0, np.nan, 2.0, np.nan, 3.0]) - self.assertFalse(array_equal(array, array)) + array_a = np.array([1.0, np.nan, 2.0, np.nan, 3.0]) + array_b = array_a.copy() + self.assertFalse(array_equal(array_a, array_a)) + self.assertFalse(array_equal(array_a, array_b)) def test_nan_equality_nan_naneq_nan(self): array_a = np.array([1.0, np.nan, 2.0, np.nan, 3.0]) array_b = np.array([1.0, np.nan, 2.0, np.nan, 3.0]) + self.assertTrue(array_equal(array_a, array_a, withnans=True)) self.assertTrue(array_equal(array_a, array_b, withnans=True)) def test_nan_equality_nan_nanne_a(self): diff --git a/lib/iris/util.py b/lib/iris/util.py index 878c62e2f1..4c896dc6dd 100644 --- a/lib/iris/util.py +++ b/lib/iris/util.py @@ -373,6 +373,8 @@ def array_equal(array1, array2, withnans=False): This function maintains laziness when called; it does not realise data. See more at :doc:`/userguide/real_and_lazy_data`. """ + if withnans and (array1 is array2): + return True def normalise_array(array): if not is_lazy_data(array):