8000 REV: Add MaskedArray creation from non nd-array back in by greglucas · Pull Request #20386 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

REV: Add MaskedArray creation from non nd-array back in #20386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit< 8000 /tool-tip>
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.
  • Loading branch information
greglucas committed Nov 16, 2021
commit 90ca9be7ecbb9ddd27b8c2d25a23510422e7aa4d
6 changes: 6 additions & 0 deletions numpy/ma/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2837,6 +2837,12 @@ def __new__(cls, data=None, mask=nomask, dtype=None, copy=False,
_data = ndarray.view(_data, type(data))
else:
_data = ndarray.view(_data, cls)

# Handle the case where data is not a subclass of ndarray, but
# still has the _mask attribute like MaskedArrays
if hasattr(data, '_mask') and not isinstance(data, ndarray):
_data._mask = data._mask

# Process mask.
# Type of the mask
mdtype = make_mask_descr(_data.dtype)
Expand Down
42 changes: 42 additions & 0 deletions numpy/ma/tests/test_subclassing.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,45 @@ def test_pure_subclass_info_preservation(self):
diff2 = arr1 - arr2
assert_('info' in diff2._optinfo)
assert_(diff2._optinfo['info'] == 'test')


class ArrayNoInheritance:
"""Quantity-like class that does not inherit from ndarray"""
def __init__(self, data, units):
self.magnitude = data
self.units = units

def __getattr__(self, attr):
return getattr(self.magnitude, attr)


def test_array_no_inheritance():
data_masked = np.ma.array([1, 2, 3], mask=[True, False, True])
data_masked_units = ArrayNoInheritance(data_masked, 'meters')

# Get the masked representation of the Quantity-like class
new_array = np.ma.array(data_masked_units)
assert_equal(data_masked.data, new_array.data)
assert_equal(data_masked.mask, new_array.mask)
# Test sharing the mask
data_masked.mask = [True, False, False]
assert_equal(data_masked.mask, new_array.mask)
assert_(new_array.sharedmask) 60BD

# Get the masked representation of the Quantity-like class
new_array = np.ma.array(data_masked_units, copy=True)
assert_equal(data_masked.data, new_array.data)
assert_equal(data_masked.mask, new_array.mask)
# Test that the mask is not shared when copy=True
data_masked.mask = [True, False, True]
assert_equal([True, False, False], new_array.mask)
assert_(not new_array.sharedmask)

# Get the masked representation of the Quantity-like class
new_array = np.ma.array(data_masked_units, keep_mask=False)
assert_equal(data_masked.data, new_array.data)
# The change did not affect the original mask
assert_equal(data_masked.mask, [True, False, True])
# Test that the mask is False and not shared when keep_mask=False
assert_(not new_array.mask)
assert_(not new_array.sharedmask)
0