8000 REV: Add MaskedArray creation from non nd-array back in · numpy/numpy@90ca9be · GitHub
[go: up one dir, main page]

Skip to content

Commit 90ca9be

Browse files
committed
REV: Add MaskedArray creation from non nd-array back in
This code path was removed in beacb39. This adds back in the MaskedArray portion of that commit. A test with a Quantity-like (non inherited, but still acts like a MaskedArray) class for this case.
1 parent 444a721 commit 90ca9be

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

numpy/ma/core.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2837,6 +2837,12 @@ def __new__(cls, data=None, mask=nomask, dtype=None, copy=False,
28372837
_data = ndarray.view(_data, type(data))
28382838
else:
28392839
_data = ndarray.view(_data, cls)
2840+
2841+
# Handle the case where data is not a subclass of ndarray, but
2842+
# still has the _mask attribute like MaskedArrays
2843+
if hasattr(data, '_mask') and not isinstance(data, ndarray):
2844+
_data._mask = data._mask
2845+
28402846
# Process mask.
28412847
# Type of the mask
28422848
mdtype = make_mask_descr(_data.dtype)

numpy/ma/tests/test_subclassing.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,45 @@ def test_pure_subclass_info_preservation(self):
343343
diff2 = arr1 - arr2
344344
assert_('info' in diff2._optinfo)
345345
assert_(diff2._optinfo['info'] == 'test')
346+
347+
348+
class ArrayNoInheritance:
349+
"""Quantity-like class that does not inherit from ndarray"""
350+
def __init__(self, data, units):
351+
self.magnitude = data
352+
self.units = units
353+
354+
def __getattr__(self, attr):
355+
return getattr(self.magnitude, attr)
356+
357+
358+
def test_array_no_inheritance():
359+
data_masked = np.ma.array([1, 2, 3], mask=[True, False, True])
360+
data_masked_units = ArrayNoInheritance(data_masked, 'meters')
361+
362+
# Get the masked representation of the Quantity-like class
363+
new_array = np.ma.array(data_masked_units)
364+
assert_equal(data_masked.data, new_array.data)
365+
assert_equal(data_masked.mask, new_array.mask)
366+
# Test sharing the mask
367+
data_masked.mask = [True, False, False]
368+
assert_equal(data_masked.mask, new_array.mask)
369+
assert_(new_array.sharedmask)
370+
371+
# Get the masked representation of the Quantity-like class
372+
new_array = np.ma.array(data_masked_units, copy=True)
373+
assert_equal(data_masked.data, new_array.data)
374+
assert_equal(data_masked.mask, new_array.mask)
375+
# Test that the mask is not shared when copy=True
376+
data_masked.mask = [True, False, True]
377+
assert_equal([True, False, False], new_array.mask)
378+
assert_(not new_array.sharedmask)
379+
380+
# Get the masked representation of the Quantity-like class
381+
new_array = np.ma.array(data_masked_units, keep_mask=False)
382+
assert_equal(data_masked.data, new_array.data)
383+
# The change did not affect the original mask
384+
assert_equal(data_masked.mask, [True, False, True])
385+
# Test that the mask is False and not shared when keep_mask=False
386+
assert_(not new_array.mask)
387+
assert_(not new_array.sharedmask)

0 commit comments

Comments
 (0)
0