Constants of the numpy.ma module

In addition to the MaskedArray class, the numpy.ma module defines several constants.

numpy.ma.masked

The masked constant is a special case of MaskedArray, with a float datatype and a null shape. It is used to test whether a specific entry of a masked array is masked, or to mask one or several entries of a masked array:

>>> x = ma.array([1, 2, 3], mask=[0, 1, 0])
>>> x[1] is ma.masked
True
>>> x[-1] = ma.masked
>>> x
masked_array(data = [1 -- --],
             mask = [False  True  True],
       fill_value = 999999)
numpy.ma.nomask
Value indicating that a masked array has no invalid entry. nomask is used internally to speed up computations when the mask is not needed.
numpy.ma.masked_print_options
String used in lieu of missing data when a masked array is printed. By default, this string is '--'.

The MaskedArray class

class numpy.ma.MaskedArray
A subclass of ndarray designed to manipulate numerical array with missing data.

An instance of MaskedArray can be thought as the combination of several elements:

  • The data, as a regular numpy.ndarray of any shape or datatype (the data).
  • A boolean mask with the same shape as the data, where a True value indicates that the corresponding element of the data is invalid. The special value nomask is also acceptable for arrays without named fields, and indicates that no data is invalid.
  • A fill_value, a value that may be used to replace the invalid entries in order to return a standard numpy.ndarray.

Attributes and properties of masked arrays

See also

Array Attributes

MaskedArray.data

Returns the underlying data, as a view of the masked array. If the underlying data is a subclass of numpy.ndarray, it is returned as such.

>>> x = ma.array(np.matrix([[1, 2], [3, 4]]), mask=[[0, 1], [1, 0]])
>>> x.data
matrix([[1, 2],
        [3, 4]])

The type of the data can be accessed through the baseclass attribute.

MaskedArray.mask
Returns the underlying mask, as an array with the same shape and structure as the data, but where all fields are atomically booleans. A value of True indicates an invalid entry.
MaskedArray.recordmask

Returns the mask of the array if it has no named fields. For structured arrays, returns a ndarray of booleans where entries are True if all the fields are masked, False otherwise:

>>> x = ma.array([(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)],
...         mask=[(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)],
...        dtype=[('a', int), ('b', int)])
>>> x.recordmask
array([False, False,  True, False, False], dtype=bool)
MaskedArray.fill_value

Returns the value used to fill the invalid entries of a masked array. The value is either a scalar (if the masked array has no named fields), or a 0d-ndarray with the same dtype as the masked array if it has named fields.

The default filling value depends on the datatype of the array:

datatype default
bool True
int 999999
float 1.e20
complex 1.e20+0j
object ‘?’
string ‘N/A’
MaskedArray.baseclass

Returns the class of the underlying data.

>>> x =  ma.array(np.matrix([[1, 2], [3, 4]]), mask=[[0, 0], [1, 0]])
>>> x.baseclass
<class 'numpy.core.defmatrix.matrix'>
MaskedArray.sharedmask
Returns whether the mask of the array is shared between several masked arrays. If this is the case, any modification to the mask of one array will be propagated to the others.
MaskedArray.hardmask
Returns whether the mask is hard (True) or soft (False). When the mask is hard, masked entries cannot be unmasked.

As MaskedArray is a subclass of ndarray, a masked array also inherits all the attributes and properties of a ndarray instance.

MaskedArray.base Base object if memory is from some other object.
MaskedArray.ctypes A ctypes interface object.
MaskedArray.dtype Data-type for the array.
MaskedArray.flags Information about the memory layout of the array.
MaskedArray.itemsize Length of one element in bytes.
MaskedArray.nbytes Number of bytes in the array.
MaskedArray.ndim Number of array dimensions.
MaskedArray.shape Tuple of array dimensions.
MaskedArray.size Number of elements in the array.
MaskedArray.strides Tuple of bytes to step in each dimension.
MaskedArray.imag Imaginary part.
MaskedArray.real Real part
MaskedArray.flat Flat version of the array.
MaskedArray.__array_priority__ int(x[, base]) -> integer

MaskedArray methods

See also

Array methods

Conversion

MaskedArray.__float__ (self) Convert to float.
MaskedArray.__hex__ () <])
MaskedArray.__int__ (self) Convert to int.
MaskedArray.__long__ () <])
MaskedArray.__oct__ () <])
MaskedArray.view ([dtype, type]) New view of array with the same data.
MaskedArray.astype (self, newtype) Returns a copy of the MaskedArray cast to given newtype.
MaskedArray.byteswap (inplace) Swap the bytes of the array elements
MaskedArray.compressed (self) Return a 1-D array of all the non-masked data.
MaskedArray.filled (self[, fill_value]) Return a copy of self, where masked values are filled with fill_value.
MaskedArray.tofile (self, fid[, sep, format])
MaskedArray.toflex (self) Transforms a MaskedArray into a flexible-type array with two fields:
MaskedArray.tolist (self[, fill_value]) Copy the data portion of the array to a hierarchical python list and returns that list.
MaskedArray.torecords (self) Transforms a MaskedArray into a flexible-type array with two fields:
MaskedArray.tostring (self[, fill_value, order]) Return a copy of array data as a Python string containing the raw bytes in the array. The array is filled beforehand.

Shape manipulation

For reshape, resize, and transpose, the single tuple argument may be replaced with n integers which will be interpreted as an n-tuple.

MaskedArray.flatten ([order]) Collapse an array into one dimension.
MaskedArray.ravel (self) Returns a 1D version of self, as a view.
MaskedArray.reshape (self, *s, **kwargs) Returns a masked array containing the data of a, but with a new shape. The result is a view to the original array; if this is not possible, a ValueError is raised.
MaskedArray.resize (self, newshape[, refcheck, order]) Change shape and size of array in-place.
MaskedArray.squeeze () Remove single-dimensional entries from the shape of a.
MaskedArray.swapaxes (axis1, axis2) Return a view of the array with axis1 and axis2 interchanged.
MaskedArray.transpose (*axes) Returns a view of ‘a’ with axes transposed. If no axes are given, or None is passed, switches the order of the axes. For a 2-d array, this is the usual matrix transpose. If axes are given, they describe how the axes are permuted.
MaskedArray.T

Item selection and manipulation

For array methods that take an axis keyword, it defaults to None. If axis is None, then the array is treated as a 1-D array. Any other value for axis represents the dimension along which the operation should proceed.

MaskedArray.argmax (self[, axis, fill_value, ...]) Returns array of indices of the maximum values along the given axis. Masked values are treated as if they had the value fill_value.
MaskedArray.argmin (self[, axis, fill_value, ...]) Return array of indices to the minimum values along the given axis.
MaskedArray.argsort (self[, axis, fill_value, ...]) Return an ndarray of indices that sort the array along the specified axis. Masked values are filled beforehand to fill_value.
MaskedArray.choose (choices[, out, mode]) Use an index array to construct a new array from a set of choices.
MaskedArray.compress (self, condition[, axis, out]) Return a where condition is True. If condition is a MaskedArray, missing values are considered as False.
MaskedArray.diagonal ([offset, axis1, axis2]) Return specified diagonals.
MaskedArray.fill (value) Fill the array with a scalar value.
MaskedArray.item () Copy the first element of array to a standard Python scalar and return it. The array must be of size one.
MaskedArray.nonzero (self) Return the indices of the elements of a that are not zero nor masked, as a tuple of arrays.
MaskedArray.put (self, indices, values[, mode]) Set storage-indexed locations to corresponding values.
MaskedArray.repeat (repeats[, axis]) Repeat elements of an array.
MaskedArray.searchsorted (v[, side]) Find indices where elements of v should be inserted in a to maintain order.
MaskedArray.sort (self[, axis, kind, order, ...]) Return a sorted copy of an array.
MaskedArray.take (indices[, axis, out, mode]) Return an array formed from the elements of a at the given indices.

Pickling and copy

MaskedArray.copy ([order]) Return a copy of the array.
MaskedArray.dump (file) Dump a pickle of the array to the specified file. The array can be read back with pickle.load or numpy.load.
MaskedArray.dumps () Returns the pickle of the array as a string. pickle.loads or numpy.loads will convert the string back to an array.

Calculations

MaskedArray.all (self[, axis, out]) Check if all of the elements of a are true.
MaskedArray.anom (self[, axis, dtype]) Return the anomalies (deviations from the average) along the given axis.
MaskedArray.any (self[, axis, out]) Check if any of the elements of a are true.
MaskedArray.clip (a_min, a_max[, out]) Return an array whose values are limited to [a_min, a_max].
MaskedArray.conj () Return an array with all complex-valued elements conjugated.
MaskedArray.conjugate () Return an array with all complex-valued elements conjugated.
MaskedArray.cumprod (self[, axis, dtype, out]) Return the cumulative product of the elements along the given axis. The cumulative product is taken over the flattened array by default, otherwise over the specified axis.
MaskedArray.cumsum (self[, axis, dtype, out]) Return the cumulative sum of the elements along the given axis. The cumulative sum is calculated over the flattened array by default, otherwise over the specified axis.
MaskedArray.max (self[, axis, out, fill_value]) Return the maximum along a given axis.
MaskedArray.mean (self[, axis, dtype, out]) Returns the average of the array elements along given axis. Refer to numpy.mean for full documentation.
MaskedArray.min (self[, axis, out, fill_value]) Return the minimum along a given axis.
MaskedArray.prod (self[, axis, dtype, out]) Return the product of the array elements over the given axis. Masked elements are set to 1 internally for computation.
MaskedArray.product (self[, axis, dtype, out]) Return the product of the array elements over the given axis. Masked elements are set to 1 internally for computation.
MaskedArray.ptp (self[, axis, out, fill_value]) Return (maximum - minimum) along the the given dimension (i.e. peak-to-peak value).
MaskedArray.round ([decimals, out]) Return an array rounded a to the given number of decimals.
MaskedArray.std (self[, axis, dtype, out, ...]) Compute the standard deviation along the specified axis.
MaskedArray.sum (self[, axis, dtype, out]) Return the sum of the array elements over the given axis. Masked elements are set to 0 internally.
MaskedArray.trace ([offset, axis1, axis2, ...]) Return the sum along diagonals of the array.
MaskedArray.var (self[, axis, dtype, out, ...]) Compute the variance along the specified axis.

Arithmetic and comparison operations

Comparison operators:

MaskedArray.__lt__ () x.__lt__(y) <==> x<y
MaskedArray.__le__ () x.__le__(y) <==> x<=y
MaskedArray.__gt__ () x.__gt__(y) <==> x>y
MaskedArray.__ge__ () x.__ge__(y) <==> x>=y
MaskedArray.__eq__ (self, other) Check whether other equals self elementwise
MaskedArray.__ne__ (self, other) Check whether other doesn’t equal self elementwise

Truth value of an array (bool):

MaskedArray.__nonzero__ () x.__nonzero__() <==> x != 0

Arithmetic:

MaskedArray.__abs__ () <])
MaskedArray.__add__ (self, other) Add other to self, and return a new masked array.
MaskedArray.__radd__ (self, other) Add other to self, and return a new masked array.
MaskedArray.__sub__ (self, other) Subtract other to self, and return a new masked array.
MaskedArray.__rsub__ (self, other) Subtract other to self, and return a new masked array.
MaskedArray.__mul__ (self, other) Multiply other by self, and return a new masked array.
MaskedArray.__rmul__ (self, other) Multiply other by self, and return a new masked array.
MaskedArray.__div__ (self, other) Divide other into self, and return a new masked array.
MaskedArray.__rdiv__ () x.__rdiv__(y) <==> y/x
MaskedArray.__truediv__ (self, other) Divide other into self, and return a new masked array.
MaskedArray.__rtruediv__ () x.__rtruediv__(y) <==> y/x
MaskedArray.__floordiv__ (self, other) Divide other into self, and return a new masked array.
MaskedArray.__rfloordiv__ () x.__rfloordiv__(y) <==> y//x
MaskedArray.__mod__ () x.__mod__(y) <==> x%y
MaskedArray.__rmod__ () x.__rmod__(y) <==> y%x
MaskedArray.__divmod__ (y) <, y)
MaskedArray.__rdivmod__ (y) <, x)
MaskedArray.__pow__ (self, other) Raise self to the power other, masking the potential NaNs/Infs
MaskedArray.__rpow__ (x[, z]) <, y[, z])
MaskedArray.__lshift__ () x.__lshift__(y) <==> x<<y
MaskedArray.__rlshift__ () x.__rlshift__(y) <==> y<<x
MaskedArray.__rshift__ () x.__rshift__(y) <==> x>>y
MaskedArray.__rrshift__ () x.__rrshift__(y) <==> y>>x
MaskedArray.__and__ () x.__and__(y) <==> x&y
MaskedArray.__rand__ () x.__rand__(y) <==> y&x
MaskedArray.__or__ () x.__or__(y) <==> x|y
MaskedArray.__ror__ () x.__ror__(y) <==> y|x
MaskedArray.__xor__ () x.__xor__(y) <==> x^y
MaskedArray.__rxor__ () x.__rxor__(y) <==> y^x

Arithmetic, in-place:

MaskedArray.__iadd__ (self, other) Add other to self in-place.
MaskedArray.__isub__ (self, other) Subtract other from self in-place.
MaskedArray.__imul__ (self, other) Multiply self by other in-place.
MaskedArray.__idiv__ (self, other) Divide self by other in-place.
MaskedArray.__itruediv__ () x.__itruediv__(y) <==> x/y
MaskedArray.__ifloordiv__ () x.__ifloordiv__(y) <==> x//y
MaskedArray.__imod__ () x.__imod__(y) <==> x%y
MaskedArray.__ipow__ (self, other) Raise self to the power other, in place.
MaskedArray.__ilshift__ () x.__ilshift__(y) <==> x<<y
MaskedArray.__irshift__ () x.__irshift__(y) <==> x>>y
MaskedArray.__iand__ () x.__iand__(y) <==> x&y
MaskedArray.__ior__ () x.__ior__(y) <==> x|y
MaskedArray.__ixor__ () x.__ixor__(y) <==> x^y

Representation

MaskedArray.__repr__ (self) Literal string representation.
MaskedArray.__str__ (self) String representation.
MaskedArray.ids (self) Return the addresses of the data and mask areas.
MaskedArray.iscontiguous (self) Is the data contiguous?

Special methods

For standard library functions:

MaskedArray.__copy__ ([order]) Return a copy of the array.
MaskedArray.__deepcopy__ (self[, memo])
MaskedArray.__getstate__ (self) Return the internal state of the masked array, for pickling purposes.
MaskedArray.__reduce__ (self) Return a 3-tuple for pickling a MaskedArray.
MaskedArray.__setstate__ (self, state) Restore the internal state of the masked array, for pickling purposes. state is typically the output of the __getstate__ output, and is a 5-tuple:

Basic customization:

MaskedArray.__new__ (cls[, data, mask, dtype, copy, subok, ndmin, fill_value, keep_mask, hard_mask, flag, shrink, **options) Create a new masked array from scratch.
MaskedArray.__array__ () a.__array__(|dtype) -> reference if type unchanged, copy otherwise.
MaskedArray.__array_wrap__ (self, obj[, context]) Special hook for ufuncs. Wraps the numpy array and sets the mask according to context.

Container customization: (see Indexing)

MaskedArray.__len__ () <])
MaskedArray.__getitem__ (self, indx) x.__getitem__(y) <==> x[y]
MaskedArray.__setitem__ (self, indx, value) x.__setitem__(i, y) <==> x[i]=y
MaskedArray.__delitem__ () x.__delitem__(y) <==> del x[y]
MaskedArray.__getslice__ (self, i, j) x.__getslice__(i, j) <==> x[i:j]
MaskedArray.__setslice__ (self, i, j, value) x.__setslice__(i, j, value) <==> x[i:j]=value
MaskedArray.__contains__ () x.__contains__(y) <==> y in x

Specific methods

Handling the mask

The following methods can be used to access information about the mask or to manipulate the mask.

MaskedArray.__setmask__ (self, mask[, copy]) Set the mask.
MaskedArray.harden_mask (self) Force the mask to hard.
MaskedArray.soften_mask (self) Force the mask to soft.
MaskedArray.unshare_mask (self) Copy the mask and set the sharedmask flag to False.
MaskedArray.shrink_mask (self) Reduce a mask to nomask when possible.

Handling the fill_value

MaskedArray.get_fill_value (self) Return the filling value.
MaskedArray.set_fill_value (self[, value]) Set the filling value to value.

Counting the missing elements

MaskedArray.count (self[, axis]) Count the non-masked elements of the array along the given axis.