numpy.can_cast#
- numpy.can_cast(from_, to, casting='safe')#
Returns True if cast between data types can occur according to the casting rule.
- Parameters:
- from_dtype, dtype specifier, NumPy scalar, or array
Data type, NumPy scalar, or array to cast from.
- todtype or dtype specifier
Data type to cast to.
- casting{βnoβ, βequivβ, βsafeβ, βsame_kindβ, βunsafeβ}, optional
Controls 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:
- outbool
True if cast can occur according to the casting rule.
See also
Notes
Changed in version 2.0: This function does not support Python scalars anymore and does not apply any value-based logic for 0-D arrays and NumPy scalars.
Examples
Basic examples
>>> import numpy as np >>> np.can_cast(np.int32, np.int64) True >>> np.can_cast(np.float64, complex) True >>> np.can_cast(complex, float) False
>>> np.can_cast('i8', 'f8') True >>> np.can_cast('i8', 'f4') False >>> np.can_cast('i4', 'S4') False