Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ This document explains the changes made to Iris for this release
🐛 Bugs Fixed
=============

#. N/A
#. :user:`gaoflow` fixed :meth:`iris.coords.Coord.cell` so that a masked bound
is preserved as masked, instead of revealing the value stored underneath the
mask. This also corrects the cube/coordinate printout for such bounds.
(:issue:`5158`)


💣 Incompatible Changes
Expand Down
5 changes: 4 additions & 1 deletion lib/iris/coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -2235,7 +2235,10 @@ def cell(self, index):

bound = None
if self.has_bounds():
bound = tuple(np.array(self.core_bounds()[index], ndmin=1).flatten())
# Use `np.asanyarray` to preserve any masked values (see #5158):
bound = tuple(
np.atleast_1d(np.asanyarray(self.core_bounds()[index])).flatten()
)

if self.units.is_time_reference():
point = self.units.num2date(point)
Expand Down
16 changes: 16 additions & 0 deletions lib/iris/tests/unit/coords/test_Coord.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,22 @@ def test_time_as_object(self, mocker):
mocker.call((mocker.sentinel.lower, mocker.sentinel.upper)),
]

def test_masked_point(self):
# A masked point should be preserved as masked in the cell (#5158).
coord = AuxCoord(ma.masked_array([3.0], mask=[True]))
cell = coord.cell(0)
assert cell.point is ma.masked

def test_masked_bound(self):
# A masked bound should be preserved as masked in the cell, rather
# than revealing the value underneath the mask (#5158).
coord = AuxCoord(
[5.0], bounds=ma.masked_array([[3.0, 7.0]], mask=[[True, False]])
)
cell = coord.cell(0)
assert cell.bound[0] is ma.masked
assert cell.bound[1] == 7.0


class Test_collapsed(CoordTestMixin):
def test_serialize(self):
Expand Down
Loading