Skip to content

Commit bf89829

Browse files
authored
Merge pull request #2856 from djkirkham/remove-mask-checks
When indexing, prevent the removal of masks on no-mask masked arrays
2 parents c93132b + f56b303 commit bf89829

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

lib/iris/_merge.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,10 +1239,6 @@ def merge(self, unique=True):
12391239
# normal array.
12401240
dtype = self._cube_signature.data_type
12411241
merged_data = as_concrete_data(merged_data)
1242-
# Unmask the array if it has no masked points.
1243-
if (ma.isMaskedArray(merged_data) and
1244-
not ma.is_masked(merged_data)):
1245-
merged_data = merged_data.data
12461242
merged_cube = self._get_cube(merged_data)
12471243
merged_cubes.append(merged_cube)
12481244

lib/iris/cube.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2094,11 +2094,6 @@ def new_cell_measure_dims(cm_):
20942094
# We don't want a view of the data, so take a copy of it.
20952095
data = deepcopy(data)
20962096

2097-
# We can turn a masked array into a normal array if it's full.
2098-
if ma.isMaskedArray(data):
2099-
if ma.count_masked(data) == 0:
2100-
data = data.filled()
2101-
21022097
# XXX: Slicing a single item from a masked array that is masked,
21032098
# results in numpy (v1.11.1) *always* returning a MaskedConstant
21042099
# with a dtype of float64, regardless of the original masked

lib/iris/tests/test_merge.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,24 @@ def test__masked_ndarray(self):
248248
self.assertEqual(result.dtype, self.dtype)
249249
self._check_fill_value(result, fill0=fill)
250250

251+
def test_maksed_array_preserved(self):
252+
for (lazy0, lazy1), (fill,) in self.mixed_combos:
253+
cubes = iris.cube.CubeList()
254+
mask = False
255+
cubes.append(self._make_cube(0, mask=mask, lazy=lazy0,
256+
dtype=self.dtype,
257+
fill_value=fill))
258+
cubes.append(self._make_cube(1, lazy=lazy1, dtype=self.dtype))
259+
result = cubes.merge_cube()
260+
mask = False
261+
expected_fill_value = self._expected_fill_value(fill)
262+
expected = self._make_data([0, 1], mask=mask, dtype=self.dtype,
263+
fill_value=expected_fill_value)
264+
self.assertEqual(type(result.data), ma.MaskedArray)
265+
self.assertMaskedArrayEqual(result.data, expected)
266+
self.assertEqual(result.dtype, self.dtype)
267+
self._check_fill_value(result, fill0=fill)
268+
251269
def test_fill_value_invariant_to_order__same_non_None(self):
252270
fill_value = 1234
253271
cubes = [self._make_cube(i, mask=True,

lib/iris/tests/unit/cube/test_Cube.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ def test_masked(self):
5959
self.assertEqual(type(cube.data), ma.MaskedArray)
6060
self.assertMaskedArrayEqual(cube.data, data)
6161

62+
def test_masked_no_mask(self):
63+
# ma.MaskedArray should be allowed through even if it has no mask
64+
data = ma.masked_array(np.arange(12).reshape(3, 4), False)
65+
cube = Cube(data)
66+
self.assertEqual(type(cube.data), ma.MaskedArray)
67+
self.assertMaskedArrayEqual(cube.data, data)
68+
6269
def test_matrix(self):
6370
# Subclasses of np.ndarray should be coerced back to np.ndarray.
6471
# (Except for np.ma.MaskedArray.)

0 commit comments

Comments
 (0)