Default Missing Values In Masked Arrays (MA)

Question

Does MA have a default missing value?

Answer

Yes, it does. This behavior can be good, if you're expecting it and a problem if not. Consider the following two Numeric arrays which we convert to MA arrays. Since there are no missing values in the arrays, we assign fill_value to None:

>>> import MA, Numeric
>>> a = Numeric.array([2, 3, -3, 1, 0, 8], typecode='f')
>>> b = Numeric.array([1, 9, 4, 1, 0, -3], typecode='f')
>>> a_MA = MA.masked_array(a, fill_value=None)
>>> b_MA = MA.masked_array(b, fill_value=None)

But if we examine a_MA, we find while we don't have a mask, we do have a fill_value!:

>>> print a_MA.mask()
None
>>> print a_MA.fill_value()
[ 1.00000002e+20,]

This is not what we had explicitly set in the masked_array call!

If we divide a by b we get a zero division error, since the 5th element in b is 0. But if we divide a_MA by b_MA we get:

>>> c_MA = a_MA/b_MA
>>> c
array(data =
  [ 2.00000000e+00, 3.33333333e-01, -7.50000000e-01, 1.00000000e+00,
        1.00000002e+20, -2.66666667e+00,],
      mask =
 [0,0,0,0,1,0,],
      fill_value=[ 1.00000002e+20,])

Instead of returning an error, Python automatically applies a mask to the output where the zero division error occurred. And the new array c_MA also has an automatically assigned fill_value of 1e+20. The default fill_value for an integer array is 0.

If we export c_MA out as a regular Numeric array the missing value is automatically filled with this default fill_value:

>>> c = MA.filled(c_MA)
>>> c
[ 2.00000000e+00, 3.33333333e-01, -7.50000000e-01, 1.00000000e+00,
        1.00000002e+20, -2.66666667e+00,]

Because a and b had started out as regular Numeric arrays with no missing values, you might assume any operations with masked versions of these arrays would behave like Numeric arrays. But this isn't the case.

The bottom line is this: if you use masked arrays, always explicitly set a fill_value, or at least remember that the result of operations of those masked arrays will convert to Numeric using the masked array object's default fill_value.


Notes: This discussion applies to the version of MA with Numeric 23.0.

Return to the Tips and Examples index page.

Updated: November 24, 2003 by Johnny Lin <email address>. License.