Constants of the numpy.ma module¶
In addition to the MaskedArray class, the numpy.ma module
defines several constants.
-
numpy.ma.masked¶ The
maskedconstant is a special case ofMaskedArray, 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.
nomaskis 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[source]¶ - A subclass of
ndarraydesigned to manipulate numerical arrays with missing data.An instance of
MaskedArraycan be thought as the combination of several elements:
- The
data, as a regularnumpy.ndarrayof any shape or datatype (the data). - A boolean
maskwith the same shape as the data, where aTruevalue indicates that the corresponding element of the data is invalid. The special valuenomaskis 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 standardnumpy.ndarray.
Attributes and properties of masked arrays¶
See also
-
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
baseclassattribute.
-
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
Trueindicates 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
Trueif all the fields are masked,Falseotherwise:>>> 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 0-D ndarray with the same
dtypeas 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.matrixlib.defmatrix.matrix'>
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 |
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 |
Imaginary part. |
MaskedArray.real |
Real part |
MaskedArray.flat |
Flat version of the array. |
MaskedArray.__array_priority__ |
MaskedArray methods¶
See also
Conversion¶
MaskedArray.__hex__() <==> hex(x) |
|
MaskedArray.__long__() <==> long(x) |
|
MaskedArray.__oct__() <==> oct(x) |
|
MaskedArray.byteswap(inplace) |
Swap the bytes of the array elements |
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.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.choose(choices[, out, mode]) |
Use an index array to construct a new array from a set of choices. |
MaskedArray.fill(value) |
Fill the array with a scalar value. |
MaskedArray.item(*args) |
Copy an element of an array to a standard Python scalar and return it. |
MaskedArray.searchsorted(v[, side, sorter]) |
Find indices where elements of v should be inserted in a to maintain order. |
Pickling and copy¶
MaskedArray.dump(file) |
Dump a pickle of the array to the specified file. |
MaskedArray.dumps() |
Returns the pickle of the array as a string. |
Calculations¶
MaskedArray.conj() |
Complex-conjugate all elements. |
MaskedArray.conjugate() |
Return the complex conjugate, element-wise. |
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 |
Truth value of an array (bool):¶
MaskedArray.__nonzero__ |
x.__nonzero__() <==> x != 0 |
Arithmetic:¶
MaskedArray.__abs__() <==> abs(x) |
|
MaskedArray.__rdiv__ |
x.__rdiv__(y) <==> y/x |
MaskedArray.__mod__ |
x.__mod__(y) <==> x%y |
MaskedArray.__rmod__ |
x.__rmod__(y) <==> y%x |
MaskedArray.__divmod__(y) <==> divmod(x, y) |
|
MaskedArray.__rdivmod__(y) <==> divmod(y, x) |
|
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.__imod__ |
x.__imod__(y) <==> x%=y |
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¶
Special methods¶
For standard library functions:
MaskedArray.__copy__([order]) |
Return a copy of the array. |
Basic customization:
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. |
Container customization: (see Indexing)
MaskedArray.__len__() <==> len(x) |
|
MaskedArray.__delitem__ |
x.__delitem__(y) <==> del x[y] |
MaskedArray.__contains__ |
x.__contains__(y) <==> y in x |