Unexpected masked element in numpy's isin() with masked arrays. Bug?

Viewed 104

Using numpy and the following masked arrays

import numpy.ma as ma
a = ma.MaskedArray([[1,2,3],[4,5,6]], [[True,False,False],[False,False,False]])
ta = ma.array([1,4,5])
>>> a
masked_array(
  data=[[--, 2, 3],
        [4, 5, 6]],
  mask=[[ True, False, False],
        [False, False, False]],
  fill_value=999999)
>>> ta
masked_array(data=[1, 4, 5],
             mask=False,
       fill_value=999999)

to check for each element in a if it is in ta, I use

ma.isin(a, ta)

This command gives

masked_array(
  data=[[False, False, False],
        [True, True, --]],
  mask=[[False, False, False],
        [False, False,  True]],
  fill_value=True)

Why is the last element in the result masked? Neither of the input arrays is masked at this point.

Using the the standard numpy version produces to be expected results:

>>> import numpy as np
>>> np.isin(a, ta)
array([[ True, False, False],
       [ True,  True, False]])

Here, however, the very first element is True because the mask of a was ignored.

Tested with Python 3.9.4 and numpy 1.20.3.

0 Answers
Related