Home > Software design >  how to set a NumPy MaskedArray's mask to False?
how to set a NumPy MaskedArray's mask to False?

Time:12-21

If I have a MaskedArray with a mask that has all False values in the mask, I want to collapse it into a single boolean False value. For example:

    >>> import numpy.ma as npma
    >>> ma = npma.MaskedArray([1,2,3], mask=[False, False, False])
    >>> ma
    masked_array(data=[1, 2, 3],
         mask=[False, False, False],
   fill_value=999999)
   >>> ma.mask = npma.nomask
   >>> ma
   masked_array(data=[1, 2, 3],
         mask=[False, False, False],
   fill_value=999999)
   >>> mas.mask = False
   >>> ma
   masked_array(data=[1, 2, 3],
         mask=[False, False, False],
   fill_value=999999)

No matter what I've tried, I can't get an already-created MaskedArray to go back to having just a single False value instead of an array of False values. The only work around I've found is to create a new MaskedArray from the existing MaskedArray's data property, and leave out the mask argument. Note how mask is now just False, which is what I want.

   >>> ma = npma.MaskedArray(ma.data)
   >>> ma
   masked_array(data=[1, 2, 3],
         mask=False,
   fill_value=999999)

Is there a way to set the mask property to False?

CodePudding user response:

You can call m.shrink_mask() after you set the mask:

>>> m
masked_array(data=[1, 2, 3],
             mask=False,
       fill_value=999999)
       
>>> m.mask = False
masked_array(data=[1, 2, 3],
             mask=[False, False, False],
       fill_value=999999)

>>> m.shrink_mask()  # <-------------- Here
>>> m
masked_array(data=[1, 2, 3],
             mask=False,
       fill_value=999999)
  • Related