Skip to content

Commit f4afe1b

Browse files
committed
consider masked case
1 parent 4b92b2f commit f4afe1b

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

lib/iris/coords.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1950,7 +1950,7 @@ def collapsed(self, dims_to_collapse=None):
19501950
# bounds as strings.
19511951
def serialize(x, axis):
19521952
if axis is None:
1953-
return "|".join(x.flatten())
1953+
return "|".join(str(i) for i in x.flatten())
19541954
# np.apply_along_axis does not work with str.join, so we
19551955
# need to loop through the array directly. First move (possibly
19561956
# multiple) axis of interest to trailing dim(s), then make a 2D
@@ -1961,7 +1961,7 @@ def serialize(x, axis):
19611961

19621962
joined = []
19631963
for arr_slice in work_array:
1964-
joined.append("|".join(arr_slice))
1964+
joined.append(serialize(arr_slice, None))
19651965

19661966
return np.array(joined).reshape(out_shape)
19671967

lib/iris/tests/unit/coords/test_Coord.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import dask.array as da
1717
import numpy as np
18+
import numpy.ma as ma
1819

1920
import iris
2021
from iris.coords import AuxCoord, Coord, DimCoord
@@ -483,6 +484,15 @@ def test_string_nd_first(self):
483484

484485
self.assertArrayEqual(collapsed_coord.points, expected)
485486

487+
def test_string_masked(self):
488+
points = ma.array(["foo", "bar", "bing"], mask=[0, 1, 0], dtype=str)
489+
coord = AuxCoord(points)
490+
491+
collapsed_coord = coord.collapsed()
492+
493+
expected = "foo|--|bing"
494+
self.assertEqual(collapsed_coord.points, expected)
495+
486496
def test_string_nd_second(self):
487497
self.setupTestArrays((3, 4))
488498
coord = AuxCoord(self.pts_real.astype(str))

0 commit comments

Comments
 (0)