diff --git a/lib/iris/tests/unit/util/test__coord_regular.py b/lib/iris/tests/unit/util/test__coord_regular.py index e5141419ec..a5e9aca9ed 100644 --- a/lib/iris/tests/unit/util/test__coord_regular.py +++ b/lib/iris/tests/unit/util/test__coord_regular.py @@ -94,6 +94,18 @@ def test_irregular_points(self): self.assertEqual(exp_avdiff, result_avdiff) self.assertFalse(result) + def test_single_point(self): + lone_point = np.array([4]) + result_avdiff, result = points_step(lone_point) + self.assertTrue(np.isnan(result_avdiff)) + self.assertTrue(result) + + def test_no_points(self): + no_points = np.array([]) + result_avdiff, result = points_step(no_points) + self.assertTrue(np.isnan(result_avdiff)) + self.assertTrue(result) + if __name__ == "__main__": tests.main() diff --git a/lib/iris/util.py b/lib/iris/util.py index 826162fa7f..bc8d27be8f 100644 --- a/lib/iris/util.py +++ b/lib/iris/util.py @@ -1397,11 +1397,28 @@ def regular_step(coord): def points_step(points): - """Determine whether a NumPy array has a regular step.""" - diffs = np.diff(points) - avdiff = np.mean(diffs) - # TODO: This value for `rtol` is set for test_analysis to pass... - regular = np.allclose(diffs, avdiff, rtol=0.001) + """Determine whether `points` has a regular step. + + Parameters + ---------- + points : numeric, array-like + The sequence of values to check for a regular difference. + + Returns + ------- + numeric, bool + A tuple containing the average difference between values, and whether the difference is regular. + """ + # Calculations only make sense with multiple points + points = np.asanyarray(points) + if points.size >= 2: + diffs = np.diff(points) + avdiff = np.mean(diffs) + # TODO: This value for `rtol` is set for test_analysis to pass... + regular = np.allclose(diffs, avdiff, rtol=0.001) + else: + avdiff = np.nan + regular = True return avdiff, regular