astype#
- ivy.astype(x, dtype, /, *, copy=True, out=None)[source]#
Copy an array to a specified data type irrespective of type- promotion rules.
Casting floating-point
NaN
andinfinity
values to integral data types is not specified and is implementation-dependent.When casting a boolean input array to a numeric data type, a value of
True
must cast to a numeric value equal to1
, and a value ofFalse
must cast to a numeric value equal to0
.When casting a numeric input array to
bool
, a value of0
must cast toFalse
, and a non-zero value must cast toTrue
.- Parameters:
x (
Union
[Array
,NativeArray
]) – array to cast.dtype (
Union
[Dtype
,NativeDtype
]) – desired data type.copy (
bool
, default:True
) – specifies whether to copy an array when the specifieddtype
matches the data type of the input arrayx
. IfTrue
, a newly allocated array must always be returned. IfFalse
and the specifieddtype
matches the data type of the input array, the input array must be returned; otherwise, a newly allocated must be returned. Default:True
.out (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
- Returns:
ret – an array having the specified data type. The returned array must have the same shape as
x
.
Examples
With
ivy.Array
input:>>> x = ivy.array([1, 2]) >>> y = ivy.zeros_like(x) >>> y = ivy.astype(x, ivy.float64) >>> print(y) ivy.array([1., 2.])
>>> x = ivy.array([3.141, 2.718, 1.618]) >>> y = ivy.zeros_like(x) >>> ivy.astype(x, ivy.int32, out=y) >>> print(y) ivy.array([3., 2., 1.])
>>> x = ivy.array([[-1, -2], [0, 2]]) >>> ivy.astype(x, ivy.float64, out=x) >>> print(x) ivy.array([[-1., -2.], [0., 2.]])
With
ivy.NativeArray
input:>>> x = ivy.native_array([3.141, 2.718, 1.618]) >>> y = ivy.astype(x, ivy.int32) >>> print(y) ivy.array([3, 2, 1])
With
ivy.Container
input:>>> x = ivy.Container(a=ivy.array([0,2,1]),b=ivy.array([1,0,0])) >>> print(ivy.astype(x, ivy.bool)) { a: ivy.array([False, True, True]), b: ivy.array([True, False, False]) }
With
ivy.Array
instance method:>>> x = ivy.array([[-1, -2], [0, 2]]) >>> print(x.astype(ivy.float64)) ivy.array([[-1., -2.], [0., 2.]])
With
ivy.Container
instance method:>>> x = ivy.Container(a=ivy.array([False,True,True]), ... b=ivy.array([3.14, 2.718, 1.618])) >>> print(x.astype(ivy.int32)) { a: ivy.array([0, 1, 1]), b: ivy.array([3, 2, 1]) }
- Array.astype(self, dtype, /, *, copy=True, out=None)[source]#
Copy an array to a specified data type irrespective of type- promotion rules.
Casting floating-point
NaN
andinfinity
values to integral data types is not specified and is implementation-dependent.When casting a boolean input array to a numeric data type, a value of
True
must cast to a numeric value equal to1
, and a value ofFalse
must cast to a numeric value equal to0
.When casting a numeric input array to
bool
, a value of0
must cast toFalse
, and a non-zero value must cast toTrue
.- Parameters:
self (
Array
) – array to cast.dtype (
Dtype
) – desired data type.copy (
bool
, default:True
) – specifies whether to copy an array when the specifieddtype
matches the data type of the input arrayx
. IfTrue
, a newly allocated array must always be returned. IfFalse
and the specifieddtype
matches the data type of the input array, the input array must be returned; otherwise, a newly allocated must be returned. Default:True
.out (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array
- Returns:
ret – an array having the specified data type. The returned array must have the same shape as
x
.
Examples
Using
ivy.Array
instance method:>>> x = ivy.array([[-1, -2], [0, 2]]) >>> print(x.astype(ivy.float64)) ivy.array([[-1., -2.], [0., 2.]])
- Container.astype(self, dtype, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, *, copy=True, out=None)[source]#
Copy an array to a specified data type irrespective of type- promotion rules.
Casting floating-point
NaN
andinfinity
values to integral data types is not specified and is implementation-dependent.When casting a boolean input array to a numeric data type, a value of
True
must cast to a numeric value equal to1
, and a value ofFalse
must cast to a numeric value equal to0
.When casting a numeric input array to
bool
, a value of0
must cast toFalse
, and a non-zero value must cast toTrue
.- Parameters:
self (
Container
) – array to cast.dtype (
Union
[Dtype
,Container
]) – desired data type.copy (
Union
[bool
,Container
], default:True
) – specifies whether to copy an array when the specifieddtype
matches the data type of the input arrayx
. IfTrue
, a newly allocated array must always be returned. IfFalse
and the specifieddtype
matches the data type of the input array, the input array must be returned; otherwise, a newly allocated must be returned. Default:True
.out (
Optional
[Container
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Container
- Returns:
ret – an array having the specified data type. The returned array must have the same shape as
x
.
Examples
Using
ivy.Container
instance method:>>> x = ivy.Container(a=ivy.array([False,True,True]), ... b=ivy.array([3.14, 2.718, 1.618])) >>> print(x.astype(ivy.int32)) { a: ivy.array([0, 1, 1]), b: ivy.array([3, 2, 1]) }