bundles / numpy 2.4.3 / numpy / astype
_ArrayFunctionDispatcher
numpy:astype
source: /numpy/_core/numeric.py:2678
Signature
def astype(x, dtype, /, copy = True, device = None)Summary
Copies an array to a specified data type.
Extended Summary
This function is an Array API compatible alternative to numpy.ndarray.astype.
Parameters
x: ndarrayInput NumPy array to cast.
array_likesare explicitly not supported here.dtype: dtypeData type of the result.
copy: bool, optionalSpecifies whether to copy an array when the specified dtype matches the data type of the input array
x. IfTrue, a newly allocated array must always be returned. IfFalseand the specified dtype matches the data type of the input array, the input array must be returned; otherwise, a newly allocated array must be returned. Defaults toTrue.device: str, optionalThe device on which to place the returned array. Default: None. For Array-API interoperability only, so must be
"cpu"if passed.
Returns
out: ndarrayAn array having the specified data type.
Examples
import numpy as np arr = np.array([1, 2, 3]); arr np.astype(arr, np.float64)✓
array([1., 2., 3.])Non-copy case:
arr = np.array([1, 2, 3]) arr_noncpy = np.astype(arr, arr.dtype, copy=False) np.shares_memory(arr, arr_noncpy)✓
True
See also
- ndarray.astype
Aliases
numpy.astype