Description
If masked arrays are compared (through the >
, >=
, <
and <=
operators to constants, arrays or other masked arrays they access the masked out elements for comparison. In some extreme cases this fails, often causing a warning.
Reproducing code example:
This example
import numpy as np
d = np.ma.array([1, None], mask=[False, True])
d > 0
fails with a TypeError: '>' not supported between instances of 'NoneType' and 'int'
.
This example
import numpy as np
d = np.ma.array([1, np.nan], mask=[False, True])
d > 0
throws a warning __main__:1: RuntimeWarning: invalid value encountered in greater
.
I think masked out elements should be set to False
by default and not compared, so that operations such as:
d[d > 0] = 0
have the expected behavior. However that might cause issues in other cases such as:
np.all(d > 0)
This issue has been brought up before here: #4959, but the initial issue there seems more about using default functions such as np.log
with masked array insted of the np.ma
relative.
Even adding a fill_value
seems not to fix this. At least in that case I would assume that for elements that are masked out the comparison happens with respect to the fill value.
import numpy as np
d = np.ma.array([1, None], mask=[False, True], fill_value=-1)
d > 0
creates the same error as mentioned above.
Numpy/Python version information:
- Numpy: '1.18.2'
- System: '3.7.6 (default, Jan 30 2020, 09:44:41) [GCC 9.2.1 20190827 (Red Hat 9.2.1-1)]'