numpy.exceptions.DTypePromotionError#
- exception exceptions.DTypePromotionError[源代码]#
无法将多个 DType 转换为通用 DType.
此异常派生自
TypeError,并且在无法将 dtype 转换为单个通用 dtype 时引发.这可能是因为它们属于不同的类别/类,或者是同一类的不兼容实例(请参阅示例).注释
许多函数将使用提升来找到正确的结果和实现.对于这些函数,该错误通常会与一个更具体的错误链接在一起,表明没有找到适用于输入 dtype 的实现.
通常,当 arr1 == arr2 可以安全地返回所有
False时,应认为两个数组的 dtype 之间的提升是“无效”的,因为这些 dtype 在根本上是不同的.示例
日期时间和复数是不兼容的类,无法提升:
>>> import numpy as np >>> np.result_type(np.dtype("M8[s]"), np.complex128) Traceback (most recent call last): ... DTypePromotionError: The DType <class 'numpy.dtype[datetime64]'> could not be promoted by <class 'numpy.dtype[complex128]'>. This means that no common DType exists for the given inputs. For example they cannot be stored in a single array unless the dtype is `object`. The full list of DTypes is: (<class 'numpy.dtype[datetime64]'>, <class 'numpy.dtype[complex128]'>)
例如,对于结构化 dtype,结构可能不匹配,并且当给出两个字段数量不匹配的结构化 dtype 时,也会给出相同的
DTypePromotionError:>>> dtype1 = np.dtype([("field1", np.float64), ("field2", np.int64)]) >>> dtype2 = np.dtype([("field1", np.float64)]) >>> np.promote_types(dtype1, dtype2) Traceback (most recent call last): ... DTypePromotionError: field names `('field1', 'field2')` and `('field1',)` mismatch.