bundles / numpy 2.4.3 / numpy / _core / _multiarray_umath / can_cast
built-in
numpy._core._multiarray_umath:can_cast
Signature
built-in
can_cast ( from_ , to , casting = safe ) Summary
Returns True if cast between data types can occur according to the casting rule.
Parameters
from_: dtype, dtype specifier, NumPy scalar, or arrayData type, NumPy scalar, or array to cast from.
to: dtype or dtype specifierData type to cast to.
casting: {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optionalControls what kind of data casting may occur.
'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.
Returns
out: boolTrue if cast can occur according to the casting rule.
Notes
Examples
Basic examplesimport numpy as np np.can_cast(np.int32, np.int64) np.can_cast(np.float64, complex) np.can_cast(complex, float)
np.can_cast('i8', 'f8') np.can_cast('i8', 'f4') np.can_cast('i4', 'S4')
See also
Aliases
-
numpy._core._multiarray_umath.can_cast