Numpy MaskedArray Instance Equivalence Testing Raises Attribute Error

Change 1:

It still seems like this is a bug that was added somewhere between versions 1.8.1 and 1.9.2. I have yet to find out the reason. In 1.8.1, np.ma.MaskedArray.mean() will return a scalar. In 1.9.2, it returns the zero size of the MaskedArray. This only happens if foo.mask installed on np.ma.nomask .

Edit 2:

I presented this as an error here.

I tried to track this issue, but I'm having difficulty inheriting for MaskedArray.mean() . In particular, I am having difficulty with line 2711 in numpy / core / fromnumeric.py, which seems to refer to MaskedArray.mean() when called with an instance of MaskedArray .

The original question:

I'm probably doing something funny here, but I can't find the problem. When I check the equivalence of means of two different instances of np.ma.MaskedArray a AttributeError .

Creating Arrays:

 In [1]: import numpy as np In [2]: foo = np.ma.array([1,2,3,4]) In [3]: bar = np.ma.array([1,2,3,4]) In [4]: foo.mean() Out[4]: masked_array(data = 2.5, mask = False, fill_value = 1e+20) In [5]: bar.mean() Out[5]: masked_array(data = 2.5, mask = False, fill_value = 1e+20) 

Comparing arrays works great:

 In [6]: foo == bar Out[6]: masked_array(data = [ True True True True], mask = False, fill_value = True) 

Equivalence testing is not performed:

 In [7]: foo.mean() == bar.mean() --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-7-3b824b0972e3> in <module>() ----> 1 foo.mean() == bar.mean() /users/___/.local/lib/python2.7/site-packages/numpy/ma/core.pyc in __eq__(self, other) 3705 mask = np.all([[f[n].all() for n in mask.dtype.names] 3706 for f in mask], axis=axis) -> 3707 check._mask = mask 3708 return check 3709 # AttributeError: 'numpy.bool_' object has no attribute '_mask' In [8]: foo.mean() != bar.mean() --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-8-0947fa5da1ed> in <module>() ----> 1 foo.mean() != bar.mean() /users/___/.local/lib/python2.7/site-packages/numpy/ma/core.pyc in __ne__(self, other) 3738 mask = np.all([[f[n].all() for n in mask.dtype.names] 3739 for f in mask], axis=axis) -> 3740 check._mask = mask 3741 return check 3742 # 

Testing for larger or smaller tools works great:

 AttributeError: 'numpy.bool_' object has no attribute '_mask' In [9]: foo.mean() >= bar.mean() Out[9]: masked_array(data = True, mask = False, fill_value = True) In [10]: foo.mean() <= bar.mean() Out[10]: masked_array(data = True, mask = False, fill_value = True) 

Python version 2.7.2 with version Nump 1.9.2:

 In [11]: np.__version__ Out[11]: '1.9.2' 

It looks like in np.ma.MaskedArray.__ne__() check variable is created by a line that looks like this:

 check = np.ndarray.__eq__(foo.filled(0), bar.filled(0)).view(type(foo)) 

which I would expect to return a new masked array. However, since np.ndarray.__eq__() returns an instance of np.bool_ . trying to make a MaskedArray for check just creates another instance of np.bool_ . The error occurs when the program tries to assign check._mask , because np.bool_ does not have the _mask attribute.

Any ideas what is going on here? Error or stupidity on my part?

+3
source share

All Articles