diff --git a/cf/data/data.py b/cf/data/data.py index ffc1c03c65..a5309e2fe7 100644 --- a/cf/data/data.py +++ b/cf/data/data.py @@ -10492,6 +10492,7 @@ def in_memory(self): return True + @daskified(_DASKIFIED_VERBOSE) def datum(self, *index): """Return an element of the data array as a standard Python scalar. @@ -10607,15 +10608,13 @@ def datum(self, *index): index = tuple(index) else: raise ValueError( - "Incorrect number of indices for {} array".format( - self.__class__.__name__ - ) + f"Incorrect number of indices ({n_index}) for " + f"{self.ndim}-d {self.__class__.__name__} data" ) elif n_index != self.ndim: raise ValueError( - "Incorrect number of indices for {} array".format( - self.__class__.__name__ - ) + f"Incorrect number of indices ({n_index}) for " + f"{self.ndim}-d {self.__class__.__name__} data" ) array = self[index].array @@ -10625,8 +10624,8 @@ def datum(self, *index): else: raise ValueError( - "Can only convert a {} array of size 1 to a " - "Python scalar".format(self.__class__.__name__) + f"For size {self.size} data, must provide an index of " + "the element to be converted to a Python scalar" ) if not np.ma.isMA(array): diff --git a/cf/test/test_Data.py b/cf/test/test_Data.py index a68ab17555..8f4323d364 100644 --- a/cf/test/test_Data.py +++ b/cf/test/test_Data.py @@ -1811,12 +1811,7 @@ def test_Data_datum(self): self.assertEqual(d.datum(-1), 3) for index in d.ndindex(): self.assertEqual(d.datum(index), d.array[index].item()) - self.assertEqual( - d.datum(*index), - d.array[index].item(), - "{}, {}".format(d.datum(*index), d.array[index].item()), - ) - # --- End: for + self.assertEqual(d.datum(*index), d.array[index].item()) d = cf.Data(5, "metre") d[()] = cf.masked @@ -1835,6 +1830,20 @@ def test_Data_datum(self): self.assertIs(d.datum([0, -1]), cf.masked) self.assertIs(d.datum(-1, -1), cf.masked) + d = cf.Data([1, 2]) + with self.assertRaises(ValueError): + d.datum() + + with self.assertRaises(ValueError): + d.datum(3) + + with self.assertRaises(ValueError): + d.datum(0, 0) + + d = cf.Data([[1, 2]]) + with self.assertRaises(ValueError): + d.datum((0,)) + def test_Data_flip(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return