numpy.ma.array#

ma.array(data,dtype=None,copy=False,order=None,mask=np.False_,fill_value=None,keep_mask=True,hard_mask=False,shrink=True,subok=True,ndmin=0)[source]#

An array class with possibly masked values.

Masked values of True exclude the corresponding element from anycomputation.

Construction:

x=MaskedArray(data,mask=nomask,dtype=None,copy=False,subok=True,ndmin=0,fill_value=None,keep_mask=True,hard_mask=None,shrink=True,order=None)
Parameters:
dataarray_like

Input data.

masksequence, optional

Mask. Must be convertible to an array of booleans with the sameshape asdata. True indicates a masked (i.e. invalid) data.

dtypedtype, optional

Data type of the output.Ifdtype is None, the type of the data argument (data.dtype)is used. Ifdtype is not None and different fromdata.dtype,a copy is performed.

copybool, optional

Whether to copy the input data (True), or to use a reference instead.Default is False.

subokbool, optional

Whether to return a subclass ofMaskedArray if possible (True) or aplainMaskedArray. Default is True.

ndminint, optional

Minimum number of dimensions. Default is 0.

fill_valuescalar, optional

Value used to fill in the masked values when necessary.If None, a default based on the data-type is used.

keep_maskbool, optional

Whether to combinemask with the mask of the input data, if any(True), or to use onlymask for the output (False). Default is True.

hard_maskbool, optional

Whether to use a hard mask or not. With a hard mask, masked valuescannot be unmasked. Default is False.

shrinkbool, optional

Whether to force compression of an empty mask. Default is True.

order{‘C’, ‘F’, ‘A’}, optional

Specify the order of the array. If order is ‘C’, then the arraywill be in C-contiguous order (last-index varies the fastest).If order is ‘F’, then the returned array will be inFortran-contiguous order (first-index varies the fastest).If order is ‘A’ (default), then the returned array may bein any order (either C-, Fortran-contiguous, or even discontiguous),unless a copy is required, in which case it will be C-contiguous.

Examples

>>>importnumpyasnp

Themask can be initialized with an array of boolean valueswith the same shape asdata.

>>>data=np.arange(6).reshape((2,3))>>>np.ma.MaskedArray(data,mask=[[False,True,False],...[False,False,True]])masked_array(  data=[[0, --, 2],        [3, 4, --]],  mask=[[False,  True, False],        [False, False,  True]],  fill_value=999999)

Alternatively, themask can be initialized to homogeneous booleanarray with the same shape asdata by passing in a scalarboolean value:

>>>np.ma.MaskedArray(data,mask=False)masked_array(  data=[[0, 1, 2],        [3, 4, 5]],  mask=[[False, False, False],        [False, False, False]],  fill_value=999999)
>>>np.ma.MaskedArray(data,mask=True)masked_array(  data=[[--, --, --],        [--, --, --]],  mask=[[ True,  True,  True],        [ True,  True,  True]],  fill_value=999999,  dtype=int64)

Note

The recommended practice for initializingmask with a scalarboolean value is to useTrue/False rather thannp.True_/np.False_. The reason isnomaskis represented internally asnp.False_.

>>>np.False_isnp.ma.nomaskTrue
On this page