numpy.ma.MaskedArray.astype#

method

ma.MaskedArray.astype(dtype, order='K', casting='unsafe', subok=True, copy=True)#

Copy of the array, cast to a specified type.

Parameters:
dtypestr or dtype

Typecode or data-type to which the array is cast.

order{β€˜C’, β€˜F’, β€˜A’, β€˜K’}, optional

Controls the memory layout order of the result. β€˜C’ means C order, β€˜F’ means Fortran order, β€˜A’ means β€˜F’ order if all the arrays are Fortran contiguous, β€˜C’ order otherwise, and β€˜K’ means as close to the order the array elements appear in memory as possible. Default is β€˜K’.

casting{β€˜no’, β€˜equiv’, β€˜safe’, β€˜same_kind’, β€˜unsafe’}, optional

Controls what kind of data casting may occur. Defaults to β€˜unsafe’ for backwards compatibility.

  • β€˜no’ means the data types should not be cast at all.

  • β€˜equiv’ means only byte-order changes are allowed.

  • β€˜safe’ means only casts which can preserve values are allowed.

  • β€˜same_kind’ means only safe casts or casts within a kind, like float64 to float32, are allowed.

  • β€˜unsafe’ means any data conversions may be done.

subokbool, optional

If True, then sub-classes will be passed-through (default), otherwise the returned array will be forced to be a base-class array.

copybool, optional

By default, astype always returns a newly allocated array. If this is set to false, and the dtype, order, and subok requirements are satisfied, the input array is returned instead of a copy.

Returns:
arr_tndarray

Unless copy is False and the other conditions for returning the input array are satisfied (see description for copy input parameter), arr_t is a new array of the same shape as the input array, with dtype, order given by dtype, order.

Raises:
ComplexWarning

When casting from complex to float or int. To avoid this, one should use a.real.astype(t).

Examples

>>> import numpy as np
>>> x = np.array([1, 2, 2.5])
>>> x
array([1. ,  2. ,  2.5])
>>> x.astype(int)
array([1, 2, 2])