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. If True, a newly allocated array must always be returned. If False and 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 of dpctl.SyclDevice corresponding to a non-partitioned SYCL device, an instance of dpctl.SyclQueue, or a dpnp.tensor.Device object returned by dpnp.ndarray.device. If the value is None, 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.astype

Equivalent 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