Copy of the array, cast to a specified type.
Parameters:Typecode or data-type to which the array is cast.
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â.
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.
If True, then sub-classes will be passed-through (default), otherwise the returned array will be forced to be a base-class array.
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.
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.
When casting from complex to float or int. To avoid this, one should use a.real.astype(t)
.
Notes
Changed in version 1.17.0: Casting between a simple data type and a structured one is possible only for âunsafeâ casting. Casting to multiple fields is allowed, but casting from multiple fields is not.
Changed in version 1.9.0: Casting from numeric to string types in âsafeâ casting mode requires that the string dtype length is long enough to store the max integer/float value converted.
Examples
>>> x = np.array([1, 2, 2.5]) >>> x array([1. , 2. , 2.5])
>>> x.astype(int) array([1, 2, 2])
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4