dpnp.astype¶
- dpnp.astype(x, dtype, /, *, order='K', casting='unsafe', copy=True, device=None)[source]¶
Copy the array with data type casting.
- Parameters:
- x{dpnp.ndarray, usm_ndarray}
Array data type casting.
- dtype{None, str, dtype object}
Target data type.
- order{None, "C", "F", "A", "K"}, optional
Row-major (C-style) or column-major (Fortran-style) order. When order is
"A", it uses"F"if a is column-major and uses"C"otherwise. And when order is"K", it keeps strides as closely as possible.Default:
"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.
Default:
"unsafe".- copybool, optional
Specifies 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.Default:
True.- device{None, string, SyclDevice, SyclQueue, Device}, optional
An array API concept of device where the output array is created. device can be
None, a oneAPI filter selector string, an instance ofdpctl.SyclDevicecorresponding to a non-partitioned SYCL device, an instance ofdpctl.SyclQueue, or adpnp.tensor.Deviceobject returned bydpnp.ndarray.device. If the value isNone, returned array is created on the same device as x.Default:
None.
- Returns:
- outdpnp.ndarray
An array having the specified data type.
See also
dpnp.ndarray.astypeEquivalent method.
Examples
>>> import dpnp as np >>> x = np.array([1, 2, 3]); x array([1, 2, 3]) >>> np.astype(x, np.float32) array([1., 2., 3.], dtype=float32)
Non-copy case:
>>> x = np.array([1, 2, 3]) >>> result = np.astype(x, x.dtype, copy=False) >>> result is x True