Masked array operations

Constants

ma.MaskType

Creation

From existing data

ma.masked_array Arrays with possibly masked values. Masked values of True exclude the corresponding element from any computation.
ma.array (data[, dtype, copy, order, ...]) Arrays with possibly masked values. Masked values of True exclude the corresponding element from any computation.
ma.copy () copy a.copy(order=’C’)
ma.frombuffer (buffer[, dtype, count, offset]) Interpret a buffer as a 1-dimensional array.
ma.fromfunction (function, shape, **kwargs) Construct an array by executing a function over each coordinate.
ma.MaskedArray.copy ([order]) Return a copy of the array.

Ones and zeros

ma.empty (shape[, dtype, order]) Return a new array of given shape and type, without initialising entries.
ma.empty_like (a) Create a new array with the same shape and type as another.
ma.masked_all (shape[, dtype]) Return an empty masked array of the given shape and dtype, where all the data are masked.
ma.masked_all_like (arr) Return an empty masked array of the same shape and dtype as the array a, where all the data are masked.
ma.ones (shape[, dtype, order]) Return a new array of given shape and type, filled with ones.
ma.zeros (shape[, dtype, order]) Return a new array of given shape and type, filled with zeros.

Inspecting the array

ma.all (self[, axis, out]) Check if all of the elements of a are true.
ma.any (self[, axis, out]) Check if any of the elements of a are true.
ma.count (a[, axis]) Count the non-masked elements of the array along the given axis.
ma.count_masked (arr[, axis]) Count the number of masked elements along the given axis.
ma.getmask (a) Return the mask of a, if any, or nomask.
ma.getmaskarray (arr) Return the mask of arr, if any, or a boolean array of the shape of a, full of False.
ma.getdata (a[, subok]) Return the _data part of a if a is a MaskedArray, or a itself.
ma.nonzero (self) Return the indices of the elements of a that are not zero nor masked, as a tuple of arrays.
ma.shape (obj) Return the shape of an array.
ma.size (obj[, axis]) Return the number of elements along a given axis.
ma.MaskedArray.data
ma.MaskedArray.mask Mask
ma.MaskedArray.recordmask
ma.MaskedArray.all (self[, axis, out]) Check if all of the elements of a are true.
ma.MaskedArray.any (self[, axis, out]) Check if any of the elements of a are true.
ma.MaskedArray.count (self[, axis]) Count the non-masked elements of the array along the given axis.
ma.MaskedArray.nonzero (self) Return the indices of the elements of a that are not zero nor masked, as a tuple of arrays.
ma.shape (obj) Return the shape of an array.
ma.size (obj[, axis]) Return the number of elements along a given axis.

Manipulating a MaskedArray

Changing the shape

ma.ravel (self) Returns a 1D version of self, as a view.
ma.reshape (a, new_shape[, order]) Change the shape of the array a to new_shape.
ma.resize (x, new_shape) Return a new array with the specified shape.
ma.MaskedArray.flatten ([order]) Collapse an array into one dimension.
ma.MaskedArray.ravel (self) Returns a 1D version of self, as a view.
ma.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.
ma.MaskedArray.resize (self, newshape[, refcheck, order]) Change shape and size of array in-place.

Modifying axes

ma.swapaxes () swapaxes a.swapaxes(axis1, axis2)
ma.transpose (a[, axes]) Return a view of the array with dimensions permuted according to axes, as a masked array.
ma.MaskedArray.swapaxes (axis1, axis2) Return a view of the array with axis1 and axis2 interchanged.
ma.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.

Changing the number of dimensions

ma.atleast_1d (*arys) Convert inputs to arrays with at least one dimension.
ma.atleast_2d (*arys) View inputs as arrays with at least two dimensions.
ma.atleast_3d (*arys) View inputs as arrays with at least three dimensions.
ma.expand_dims (x, axis) Expand the shape of the array by including a new axis before the given one.
ma.squeeze (a) Remove single-dimensional entries from the shape of an array.
ma.MaskedArray.squeeze () Remove single-dimensional entries from the shape of a.
ma.column_stack (tup)
Stack 1-D arrays as columns into a 2-D array
ma.concatenate (arrays[, axis]) Concatenate the arrays along the given axis.
ma.dstack (tup)
Stack arrays in sequence depth wise (along third axis)
ma.hstack (tup)
Stack arrays in sequence horizontally (column wise)
ma.hsplit (ary, indices_or_sections)
Split array into multiple sub-arrays horizontally.
ma.mr_ Translate slice objects to concatenation along the first axis.
ma.row_stack (tup)
Stack arrays vertically.
ma.vstack (tup)
Stack arrays vertically.

Joining arrays

ma.column_stack (tup)
Stack 1-D arrays as columns into a 2-D array
ma.concatenate (arrays[, axis]) Concatenate the arrays along the given axis.
ma.dstack (tup)
Stack arrays in sequence depth wise (along third axis)
ma.hstack (tup)
Stack arrays in sequence horizontally (column wise)
ma.vstack (tup)
Stack arrays vertically.

Operations on masks

Creating a mask

ma.make_mask (m[, copy, shrink, flag, ...]) Return m as a mask, creating a copy if necessary or requested.
ma.make_mask_none (newshape[, dtype]) Return a mask of shape s, filled with False.
ma.mask_or (m1, m2[, copy, shrink]) Return the combination of two masks m1 and m2.
ma.make_mask_descr (ndtype) Constructs a dtype description list from a given dtype. Each field is set to a bool.

Accessing a mask

ma.getmask (a) Return the mask of a, if any, or nomask.
ma.getmaskarray (arr) Return the mask of arr, if any, or a boolean array of the shape of a, full of False.
ma.masked_array.mask Mask

Finding masked data

ma.flatnotmasked_contiguous (a) Find contiguous unmasked data in a flattened masked array.
ma.flatnotmasked_edges (a) Find the indices of the first and last not masked values in a 1D masked array. If all values are masked, returns None.
ma.notmasked_contiguous (a[, axis]) Find contiguous unmasked data in a masked array along the given axis.
ma.notmasked_edges (a[, axis]) Find the indices of the first and last not masked values along the given axis in a masked array.

Modifying a mask

ma.mask_cols (a[, axis]) Mask whole columns of a 2D array that contain masked values.
ma.mask_or (m1, m2[, copy, shrink]) Return the combination of two masks m1 and m2.
ma.mask_rowcols (a[, axis]) Mask whole rows and/or columns of a 2D array that contain masked values. The masking behavior is selected with the axis parameter.
ma.mask_rows (a[, axis]) Mask whole rows of a 2D array that contain masked values.
ma.harden_mask () harden_mask(self) Force the mask to hard.
ma.soften_mask () soften_mask(self) Force the mask to soft.
ma.MaskedArray.harden_mask (self) Force the mask to hard.
ma.MaskedArray.soften_mask (self) Force the mask to soft.
ma.MaskedArray.shrink_mask (self) Reduce a mask to nomask when possible.
ma.MaskedArray.unshare_mask (self) Copy the mask and set the sharedmask flag to False.

Conversion operations

> to a masked array

ma.asarray (a[, dtype, order]) Convert the input a to a masked array of the given datatype.
ma.asanyarray (a[, dtype]) Convert the input a to a masked array of the given datatype. If a is a subclass of MaskedArray, its class is conserved.
ma.fix_invalid (a[, mask, copy, fill_value]) Return (a copy of) a where invalid data (nan/inf) are masked and replaced by fill_value.
ma.masked_equal (x, value[, copy]) Shortcut to masked_where, with condition (x == value).
ma.masked_greater (x, value[, copy]) Return the array x masked where (x > value). Any value of mask already masked is kept masked.
ma.masked_greater_equal (x, value[, copy]) Shortcut to masked_where, with condition (x >= value).
ma.masked_inside (x, v1, v2[, copy]) Shortcut to masked_where, where condition is True for x inside the interval [v1,v2] (v1 <= x <= v2). The boundaries v1 and v2 can be given in either order.
ma.masked_invalid (a[, copy]) Mask the array for invalid values (NaNs or infs). Any preexisting mask is conserved.
ma.masked_less (x, value[, copy]) Shortcut to masked_where, with condition (x < value).
ma.masked_less_equal (x, value[, copy]) Shortcut to masked_where, with condition (x <= value).
ma.masked_not_equal (x, value[, copy]) Shortcut to masked_where, with condition (x != value).
ma.masked_object (x, value[, copy, shrink]) Mask the array x where the data are exactly equal to value.
ma.masked_outside (x, v1, v2[, copy]) Shortcut to masked_where, where condition is True for x outside the interval [v1,v2] (x < v1)|(x > v2). The boundaries v1 and v2 can be given in either order.
ma.masked_values (x, value[, rtol, atol, copy, ...]) Mask the array x where the data are approximately equal in value, i.e. (abs(x - value) <= atol+rtol*abs(value))
ma.masked_where (condition, a[, copy]) Return a as an array masked where condition is True. Masked values of a or condition are kept.

> to a ndarray

ma.compress_cols (a) Suppress whole columns of a 2D array that contain masked values.
ma.compress_rowcols (x[, axis]) Suppress the rows and/or columns of a 2D array that contain masked values.
ma.compress_rows (a) Suppress whole rows of a 2D array that contain masked values.
ma.compressed (x) Return a 1-D array of all the non-masked data.
ma.filled (a[, fill_value]) Return a as an array where masked data have been replaced by value.
ma.MaskedArray.compressed (self) Return a 1-D array of all the non-masked data.
ma.MaskedArray.filled (self[, fill_value]) Return a copy of self, where masked values are filled with fill_value.

> to another object

ma.MaskedArray.tofile (self, fid[, sep, format])
ma.MaskedArray.tolist (self[, fill_value]) Copy the data portion of the array to a hierarchical python list and returns that list.
ma.MaskedArray.torecords (self) Transforms a MaskedArray into a flexible-type array with two fields:
ma.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.

Pickling and unpickling

ma.dump (a, F) Pickle the MaskedArray a to the file F. F can either be the handle of an exiting file, or a string representing a file name.
ma.dumps (a) Return a string corresponding to the pickling of the MaskedArray.
ma.load (F) Wrapper around cPickle.load which accepts either a file-like object or a filename.
ma.loads (strg) Load a pickle from the current string.

Filling a masked array

ma.common_fill_value (a, b) Return the common filling value of a and b, if any. If a and b have different filling values, returns None.
ma.default_fill_value (obj) Calculate the default fill value for the argument object.
ma.maximum_fill_value (obj) Calculate the default fill value suitable for taking the maximum of obj.
ma.maximum_fill_value (obj) Calculate the default fill value suitable for taking the maximum of obj.
ma.set_fill_value (a, fill_value) Set the filling value of a, if a is a masked array. Otherwise, do nothing.
ma.MaskedArray.get_fill_value (self) Return the filling value.
ma.MaskedArray.set_fill_value (self[, value]) Set the filling value to value.
ma.MaskedArray.fill_value Filling value.

Masked arrays arithmetics

Arithmetics

ma.anom (self[, axis, dtype]) Return the anomalies (deviations from the average) along the given axis.
ma.anomalies (self[, axis, dtype]) Return the anomalies (deviations from the average) along the given axis.
ma.average (a[, axis, weights, returned]) Average the array over the given axis.
ma.conjugate (x[, out]) Return the complex conjugate, element-wise.
ma.corrcoef (x[, y, rowvar, bias, ...]) The correlation coefficients formed from the array x, where the rows are the observations, and the columns are variables.
ma.cov (x[, y, rowvar, bias, ...]) Estimates the covariance matrix.
ma.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.
ma.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.
ma.mean (self[, axis, dtype, out]) Returns the average of the array elements along given axis. Refer to numpy.mean for full documentation.
ma.median (a[, axis, out, overwrite_input]) Compute the median along the specified axis.
ma.power (a, b[, third]) Computes a**b elementwise.
ma.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.
ma.std (self[, axis, dtype, out, ...]) Compute the standard deviation along the specified axis.
ma.sum (self[, axis, dtype, out]) Return the sum of the array elements over the given axis. Masked elements are set to 0 internally.
ma.var (self[, axis, dtype, out, ...]) Compute the variance along the specified axis.
ma.MaskedArray.anom (self[, axis, dtype]) Return the anomalies (deviations from the average) along the given axis.
ma.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.
ma.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.
ma.MaskedArray.mean (self[, axis, dtype, out]) Returns the average of the array elements along given axis. Refer to numpy.mean for full documentation.
ma.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.
ma.MaskedArray.std (self[, axis, dtype, out, ...]) Compute the standard deviation along the specified axis.
ma.MaskedArray.sum (self[, axis, dtype, out]) Return the sum of the array elements over the given axis. Masked elements are set to 0 internally.
ma.MaskedArray.var (self[, axis, dtype, out, ...]) Compute the variance along the specified axis.

Minimum/maximum

ma.argmax (a[, axis, fill_value]) Function version of the eponymous method.
ma.argmin (a[, 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.
ma.max (obj[, axis, out, fill_value]) Return the maximum along a given axis.
ma.min (obj[, axis, out, fill_value]) Return the minimum along a given axis.
ma.ptp (obj[, axis, out, fill_value]) Return (maximum - minimum) along the the given dimension (i.e. peak-to-peak value).
ma.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.
ma.MaskedArray.argmin (self[, axis, fill_value, ...]) Return array of indices to the minimum values along the given axis.
ma.MaskedArray.max (self[, axis, out, fill_value]) Return the maximum along a given axis.
ma.MaskedArray.min (self[, axis, out, fill_value]) Return the minimum along a given axis.
ma.MaskedArray.ptp (self[, axis, out, fill_value]) Return (maximum - minimum) along the the given dimension (i.e. peak-to-peak value).

Sorting

ma.argsort (a[, axis, kind, order, ...]) Return an ndarray of indices that sort the array along the specified axis. Masked values are filled beforehand to fill_value.
ma.sort (a[, axis, kind, order, ...]) Return a sorted copy of an array.
ma.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.
ma.MaskedArray.sort (self[, axis, kind, order, ...]) Return a sorted copy of an array.

Algebra

ma.diag (v[, k]) Extract a diagonal or construct a diagonal array.
ma.dot (a, b[, strict]) Return the dot product of two 2D masked arrays a and b.
ma.identity (n[, dtype]) Return the identity array.
ma.inner (a, b) Inner product of two arrays.
ma.innerproduct (a, b) Inner product of two arrays.
ma.outer (a, b) Returns the outer product of two vectors.
ma.outerproduct (a, b) Returns the outer product of two vectors.
ma.trace (self[, offset, axis1, axis2, ...])
Return the sum along diagonals of the array.
ma.transpose (a[, axes]) Return a view of the array with dimensions permuted according to axes, as a masked array.
ma.MaskedArray.trace ([offset, axis1, axis2, ...]) Return the sum along diagonals of the array.
ma.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.

Polynomial fit

ma.vander (x[, n]) Generate a Van der Monde matrix.
ma.polyfit (x, y, deg[, rcond, full]) Least squares polynomial fit.

Clipping and rounding

ma.around () Round an array to the given number of decimals.
ma.clip (a, a_min, a_max[, out]) Clip (limit) the values in an array.
ma.round (a[, decimals, out]) Return a copy of a, rounded to ‘decimals’ places.
ma.MaskedArray.clip (a_min, a_max[, out]) Return an array whose values are limited to [a_min, a_max].
ma.MaskedArray.round ([decimals, out]) Return an array rounded a to the given number of decimals.

Miscellanea

ma.allequal (a, b[, fill_value]) Return True if all entries of a and b are equal, using fill_value as a truth value where either or both are masked.
ma.allclose (a, b[, masked_equal, rtol, ...]) Returns True if two arrays are element-wise equal within a tolerance.
ma.apply_along_axis (func1d, axis, arr, ...) Apply function to 1-D slices along the given axis.
ma.arange ([start,] stop[, step, ...]) Return evenly spaced values within a given interval.
ma.choose (a, choices[, out, mode]) Use an index array to construct a new array from a set of choices.
ma.ediff1d (arr[, to_end, to_begin]) Computes the differences between consecutive elements of an array.
ma.indices (dimensions[, dtype]) Return an array representing the indices of a grid.
ma.where (condition | x, y) Returns a (subclass of) masked array, shaped like condition, where the elements are x when condition is True, and y otherwise. If neither x nor y are given, returns a tuple of indices where condition is True (a la condition.nonzero()).