numpy.ma
In addition to the MaskedArray class, the numpy.ma module defines several constants.
MaskedArray
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)
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. It is represented internally as np.False_.
np.False_
masked_print_options
String used in lieu of missing data when a masked array is printed. By default, this string is '--'.
'--'
A subclass of ndarray designed to manipulate numerical arrays with missing data.
ndarray
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).
data
numpy.ndarray
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.
mask
True
A fill_value, a value that may be used to replace the invalid entries in order to return a standard numpy.ndarray.
fill_value
See also
Array Attributes
MaskedArray.
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 = np.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.
baseclass
Current mask.
recordmask
Get or set 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:
False
>>> x = np.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])
The filling value of the masked array is a scalar. When setting, None will set to a default based on the data type.
Examples
>>> for dt in [np.int32, np.int64, np.float64, np.complex128]: ... np.ma.array([0, 1], dtype=dt).get_fill_value() ... 999999 999999 1e+20 (1e+20+0j)
>>> x = np.ma.array([0, 1.], fill_value=-np.inf) >>> x.fill_value -inf >>> x.fill_value = np.pi >>> x.fill_value 3.1415926535897931 # may vary
Reset to default:
>>> x.fill_value = None >>> x.fill_value 1e+20
Class of the underlying data (read-only).
sharedmask
Share status of the mask (read-only).
hardmask
Hardness of the mask
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
An object to simplify the interaction of the array with the ctypes module.
MaskedArray.dtype
Data-type of the array’s elements.
MaskedArray.flags
Information about the memory layout of the array.
MaskedArray.itemsize
Length of one array element in bytes.
MaskedArray.nbytes
Total bytes consumed by the elements of 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 when traversing an array.
MaskedArray.imag
The imaginary part of the masked array.
MaskedArray.real
The real part of the masked array.
MaskedArray.flat
Return a flat iterator, or set a flattened version of self to value.
MaskedArray.__array_priority__
Array methods
MaskedArray.__float__()
MaskedArray.__float__
Convert to float.
MaskedArray.__int__()
MaskedArray.__int__
Convert to int.
MaskedArray.view([dtype, type, fill_value])
MaskedArray.view
Return a view of the MaskedArray data.
MaskedArray.astype(dtype[, order, casting, …])
MaskedArray.astype
Copy of the array, cast to a specified type.
MaskedArray.byteswap([inplace])
MaskedArray.byteswap
Swap the bytes of the array elements
MaskedArray.compressed()
MaskedArray.compressed
Return all the non-masked data as a 1-D array.
MaskedArray.filled([fill_value])
MaskedArray.filled
Return a copy of self, with masked values filled with a given value.
MaskedArray.tofile(fid[, sep, format])
MaskedArray.tofile
Save a masked array to a file in binary format.
MaskedArray.toflex()
MaskedArray.toflex
Transforms a masked array into a flexible-type array.
MaskedArray.tolist([fill_value])
MaskedArray.tolist
Return the data portion of the masked array as a hierarchical Python list.
MaskedArray.torecords()
MaskedArray.torecords
MaskedArray.tostring([fill_value, order])
MaskedArray.tostring
A compatibility alias for tobytes, with exactly the same behavior.
MaskedArray.tobytes([fill_value, order])
MaskedArray.tobytes
Return the array data as a string containing the raw bytes in the array.
For reshape, resize, and transpose, the single tuple argument may be replaced with n integers which will be interpreted as an n-tuple.
n
MaskedArray.flatten([order])
MaskedArray.flatten
Return a copy of the array collapsed into one dimension.
MaskedArray.ravel([order])
MaskedArray.ravel
Returns a 1D version of self, as a view.
MaskedArray.reshape(*s, **kwargs)
MaskedArray.reshape
Give a new shape to the array without changing its data.
MaskedArray.resize(newshape[, refcheck, order])
MaskedArray.resize
MaskedArray.squeeze([axis])
MaskedArray.squeeze
Remove axes of length one from a.
MaskedArray.swapaxes(axis1, axis2)
MaskedArray.swapaxes
Return a view of the array with axis1 and axis2 interchanged.
MaskedArray.transpose(*axes)
MaskedArray.transpose
Returns a view of the array with axes transposed.
MaskedArray.T
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.
axis
MaskedArray.argmax([axis, fill_value, out])
MaskedArray.argmax
Returns array of indices of the maximum values along the given axis.
MaskedArray.argmin([axis, fill_value, out])
MaskedArray.argmin
Return array of indices to the minimum values along the given axis.
MaskedArray.argsort([axis, kind, order, …])
MaskedArray.argsort
Return an ndarray of indices that sort the array along the specified axis.
MaskedArray.choose(choices[, out, mode])
MaskedArray.choose
Use an index array to construct a new array from a set of choices.
MaskedArray.compress(condition[, axis, out])
MaskedArray.compress
Return a where condition is True.
MaskedArray.diagonal([offset, axis1, axis2])
MaskedArray.diagonal
Return specified diagonals.
MaskedArray.fill(value)
MaskedArray.fill
Fill the array with a scalar value.
MaskedArray.item(*args)
MaskedArray.item
Copy an element of an array to a standard Python scalar and return it.
MaskedArray.nonzero()
MaskedArray.nonzero
Return the indices of unmasked elements that are not zero.
MaskedArray.put(indices, values[, mode])
MaskedArray.put
Set storage-indexed locations to corresponding values.
MaskedArray.repeat(repeats[, axis])
MaskedArray.repeat
Repeat elements of an array.
MaskedArray.searchsorted(v[, side, sorter])
MaskedArray.searchsorted
Find indices where elements of v should be inserted in a to maintain order.
MaskedArray.sort([axis, kind, order, …])
MaskedArray.sort
Sort the array, in-place
MaskedArray.take(indices[, axis, out, mode])
MaskedArray.take
MaskedArray.copy([order])
MaskedArray.copy
Return a copy of the array.
MaskedArray.dump(file)
MaskedArray.dump
Dump a pickle of the array to the specified file.
MaskedArray.dumps()
MaskedArray.dumps
Returns the pickle of the array as a string.
MaskedArray.all([axis, out, keepdims])
MaskedArray.all
Returns True if all elements evaluate to True.
MaskedArray.anom([axis, dtype])
MaskedArray.anom
Compute the anomalies (deviations from the arithmetic mean) along the given axis.
MaskedArray.any([axis, out, keepdims])
MaskedArray.any
Returns True if any of the elements of a evaluate to True.
MaskedArray.clip([min, max, out])
MaskedArray.clip
Return an array whose values are limited to [min, max].
[min, max]
MaskedArray.conj()
MaskedArray.conj
Complex-conjugate all elements.
MaskedArray.conjugate()
MaskedArray.conjugate
Return the complex conjugate, element-wise.
MaskedArray.cumprod([axis, dtype, out])
MaskedArray.cumprod
Return the cumulative product of the array elements over the given axis.
MaskedArray.cumsum([axis, dtype, out])
MaskedArray.cumsum
Return the cumulative sum of the array elements over the given axis.
MaskedArray.max([axis, out, fill_value, …])
MaskedArray.max
Return the maximum along a given axis.
MaskedArray.mean([axis, dtype, out, keepdims])
MaskedArray.mean
Returns the average of the array elements along given axis.
MaskedArray.min([axis, out, fill_value, …])
MaskedArray.min
Return the minimum along a given axis.
MaskedArray.prod([axis, dtype, out, keepdims])
MaskedArray.prod
Return the product of the array elements over the given axis.
MaskedArray.product([axis, dtype, out, keepdims])
MaskedArray.product
MaskedArray.ptp([axis, out, fill_value, …])
MaskedArray.ptp
Return (maximum - minimum) along the given dimension (i.e.
MaskedArray.round([decimals, out])
MaskedArray.round
Return each element rounded to the given number of decimals.
MaskedArray.std([axis, dtype, out, ddof, …])
MaskedArray.std
Returns the standard deviation of the array elements along given axis.
MaskedArray.sum([axis, dtype, out, keepdims])
MaskedArray.sum
Return the sum of the array elements over the given axis.
MaskedArray.trace([offset, axis1, axis2, …])
MaskedArray.trace
Return the sum along diagonals of the array.
MaskedArray.var([axis, dtype, out, ddof, …])
MaskedArray.var
Compute the variance along the specified axis.
MaskedArray.__lt__(value, /)
MaskedArray.__lt__
Return self<value.
MaskedArray.__le__(value, /)
MaskedArray.__le__
Return self<=value.
MaskedArray.__gt__(value, /)
MaskedArray.__gt__
Return self>value.
MaskedArray.__ge__(value, /)
MaskedArray.__ge__
Return self>=value.
MaskedArray.__eq__(other)
MaskedArray.__eq__
Check whether other equals self elementwise.
MaskedArray.__ne__(other)
MaskedArray.__ne__
Check whether other does not equal self elementwise.
bool()
MaskedArray.__bool__(/)
MaskedArray.__bool__
self != 0
MaskedArray.__abs__(self)
MaskedArray.__abs__
MaskedArray.__add__(other)
MaskedArray.__add__
Add self to other, and return a new masked array.
MaskedArray.__radd__(other)
MaskedArray.__radd__
Add other to self, and return a new masked array.
MaskedArray.__sub__(other)
MaskedArray.__sub__
Subtract other from self, and return a new masked array.
MaskedArray.__rsub__(other)
MaskedArray.__rsub__
Subtract self from other, and return a new masked array.
MaskedArray.__mul__(other)
MaskedArray.__mul__
Multiply self by other, and return a new masked array.
MaskedArray.__rmul__(other)
MaskedArray.__rmul__
Multiply other by self, and return a new masked array.
MaskedArray.__div__(other)
MaskedArray.__div__
Divide other into self, and return a new masked array.
MaskedArray.__truediv__(other)
MaskedArray.__truediv__
MaskedArray.__rtruediv__(other)
MaskedArray.__rtruediv__
Divide self into other, and return a new masked array.
MaskedArray.__floordiv__(other)
MaskedArray.__floordiv__
MaskedArray.__rfloordiv__(other)
MaskedArray.__rfloordiv__
MaskedArray.__mod__(value, /)
MaskedArray.__mod__
Return self%value.
MaskedArray.__rmod__(value, /)
MaskedArray.__rmod__
Return value%self.
MaskedArray.__divmod__(value, /)
MaskedArray.__divmod__
Return divmod(self, value).
MaskedArray.__rdivmod__(value, /)
MaskedArray.__rdivmod__
Return divmod(value, self).
MaskedArray.__pow__(other)
MaskedArray.__pow__
Raise self to the power other, masking the potential NaNs/Infs
MaskedArray.__rpow__(other)
MaskedArray.__rpow__
Raise other to the power self, masking the potential NaNs/Infs
MaskedArray.__lshift__(value, /)
MaskedArray.__lshift__
Return self<<value.
MaskedArray.__rlshift__(value, /)
MaskedArray.__rlshift__
Return value<<self.
MaskedArray.__rshift__(value, /)
MaskedArray.__rshift__
Return self>>value.
MaskedArray.__rrshift__(value, /)
MaskedArray.__rrshift__
Return value>>self.
MaskedArray.__and__(value, /)
MaskedArray.__and__
Return self&value.
MaskedArray.__rand__(value, /)
MaskedArray.__rand__
Return value&self.
MaskedArray.__or__(value, /)
MaskedArray.__or__
Return self|value.
MaskedArray.__ror__(value, /)
MaskedArray.__ror__
Return value|self.
MaskedArray.__xor__(value, /)
MaskedArray.__xor__
Return self^value.
MaskedArray.__rxor__(value, /)
MaskedArray.__rxor__
Return value^self.
MaskedArray.__iadd__(other)
MaskedArray.__iadd__
Add other to self in-place.
MaskedArray.__isub__(other)
MaskedArray.__isub__
Subtract other from self in-place.
MaskedArray.__imul__(other)
MaskedArray.__imul__
Multiply self by other in-place.
MaskedArray.__idiv__(other)
MaskedArray.__idiv__
Divide self by other in-place.
MaskedArray.__itruediv__(other)
MaskedArray.__itruediv__
True divide self by other in-place.
MaskedArray.__ifloordiv__(other)
MaskedArray.__ifloordiv__
Floor divide self by other in-place.
MaskedArray.__imod__(value, /)
MaskedArray.__imod__
Return self%=value.
MaskedArray.__ipow__(other)
MaskedArray.__ipow__
Raise self to the power other, in place.
MaskedArray.__ilshift__(value, /)
MaskedArray.__ilshift__
Return self<<=value.
MaskedArray.__irshift__(value, /)
MaskedArray.__irshift__
Return self>>=value.
MaskedArray.__iand__(value, /)
MaskedArray.__iand__
Return self&=value.
MaskedArray.__ior__(value, /)
MaskedArray.__ior__
Return self|=value.
MaskedArray.__ixor__(value, /)
MaskedArray.__ixor__
Return self^=value.
MaskedArray.__repr__()
MaskedArray.__repr__
Literal string representation.
MaskedArray.__str__()
MaskedArray.__str__
Return str(self).
MaskedArray.ids()
MaskedArray.ids
Return the addresses of the data and mask areas.
MaskedArray.iscontiguous()
MaskedArray.iscontiguous
Return a boolean indicating whether the data is contiguous.
For standard library functions:
MaskedArray.__copy__()
MaskedArray.__copy__
Used if copy.copy is called on an array.
copy.copy
MaskedArray.__deepcopy__(memo, /)
MaskedArray.__deepcopy__
Used if copy.deepcopy is called on an array.
copy.deepcopy
MaskedArray.__getstate__()
MaskedArray.__getstate__
Return the internal state of the masked array, for pickling purposes.
MaskedArray.__reduce__()
MaskedArray.__reduce__
Return a 3-tuple for pickling a MaskedArray.
MaskedArray.__setstate__(state)
MaskedArray.__setstate__
Restore the internal state of the masked array, for pickling purposes.
Basic customization:
MaskedArray.__new__(cls[, data, mask, …])
MaskedArray.__new__
Create a new masked array from scratch.
MaskedArray.__array__([dtype], /)
MaskedArray.__array__
Returns either a new reference to self if dtype is not given or a new array of provided data type if dtype is different from the current dtype of the array.
MaskedArray.__array_wrap__(obj[, context])
MaskedArray.__array_wrap__
Special hook for ufuncs.
Container customization: (see Indexing)
MaskedArray.__len__(/)
MaskedArray.__len__
Return len(self).
MaskedArray.__getitem__(indx)
MaskedArray.__getitem__
x.__getitem__(y) <==> x[y]
MaskedArray.__setitem__(indx, value)
MaskedArray.__setitem__
x.__setitem__(i, y) <==> x[i]=y
MaskedArray.__delitem__(key, /)
MaskedArray.__delitem__
Delete self[key].
MaskedArray.__contains__(key, /)
MaskedArray.__contains__
Return key in self.
The following methods can be used to access information about the mask or to manipulate the mask.
MaskedArray.__setmask__(mask[, copy])
MaskedArray.__setmask__
Set the mask.
MaskedArray.harden_mask()
MaskedArray.harden_mask
Force the mask to hard.
MaskedArray.soften_mask()
MaskedArray.soften_mask
Force the mask to soft.
MaskedArray.unshare_mask()
MaskedArray.unshare_mask
Copy the mask and set the sharedmask flag to False.
MaskedArray.shrink_mask()
MaskedArray.shrink_mask
Reduce a mask to nomask when possible.
MaskedArray.get_fill_value()
MaskedArray.get_fill_value
The filling value of the masked array is a scalar.
MaskedArray.set_fill_value([value])
MaskedArray.set_fill_value
MaskedArray.count([axis, keepdims])
MaskedArray.count
Count the non-masked elements of the array along the given axis.