numpy.matrix.astype#

method

matrix.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 theorder 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), otherwisethe returned array will be forced to be a base-class array.

copybool, optional

By default, astype always returns a newly allocated array. If thisis set to false, and thedtype,order, andsubokrequirements are satisfied, the input array is returned insteadof a copy.

Returns:
arr_tndarray

Unlesscopy is False and the other conditions for returning the inputarray are satisfied (see description forcopy input parameter),arr_tis a new array of the same shape as the input array, with dtype, ordergiven bydtype,order.

Raises:
ComplexWarning

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

Examples

>>>importnumpyasnp>>>x=np.array([1,2,2.5])>>>xarray([1. ,  2. ,  2.5])
>>>x.astype(int)array([1, 2, 2])
On this page