{ } Raw JSON

bundles / numpy 2.4.4 / numpy / exceptions / DTypePromotionError

class

numpy.exceptions:DTypePromotionError

source: /numpy/exceptions.py :199

Summary

Multiple DTypes could not be converted to a common one.

Extended Summary

This exception derives from TypeError and is raised whenever dtypes cannot be converted to a single common one. This can be because they are of a different category/class or incompatible instances of the same one (see Examples).

Notes

Many functions will use promotion to find the correct result and implementation. For these functions the error will typically be chained with a more specific error indicating that no implementation was found for the input dtypes.

Typically promotion should be considered "invalid" between the dtypes of two arrays when arr1 == arr2 can safely return all False because the dtypes are fundamentally different.

Examples

Datetimes and complex numbers are incompatible classes and cannot be promoted:
import numpy as np
np.result_type(np.dtype("M8[s]"), np.complex128)  # doctest: +IGNORE_EXCEPTION_DETAIL
For example for structured dtypes, the structure can mismatch and the same ``DTypePromotionError`` is given when two structured dtypes with a mismatch in their number of fields is given:
dtype1 = np.dtype([("field1", np.float64), ("field2", np.int64)])
dtype2 = np.dtype([("field1", np.float64)])
np.promote_types(dtype1, dtype2)  # doctest: +IGNORE_EXCEPTION_DETAIL

Aliases

  • numpy.exceptions.DTypePromotionError