can_cast

can_cast(from_: dtype | array, to: dtype, /) bool

Determines if one data type can be cast to another data type according to type promotion rules (see Type Promotion Rules).

Parameters:
  • from (Union[dtype, array]) – input data type or array from which to cast.

  • to (dtype) – desired data type.

Returns:

out (bool) – True if the cast can occur according to type promotion rules (see Type Promotion Rules); otherwise, False.

Notes

  • When from_ is a data type, the function must determine whether the data type can be cast to another data type according to the complete type promotion rules (see Type Promotion Rules) described in this specification, irrespective of whether a conforming array library supports devices which do not have full data type support.

  • When from_ is an array, the function must determine whether the data type of the array can be cast to the desired data type according to the type promotion graph of the array device. As not all devices can support all data types, full support for type promotion rules (see Type Promotion Rules) may not be possible. Accordingly, the output of can_cast(array, dtype) may differ from can_cast(array.dtype, dtype).