numpy.exceptions.DTypePromotionError#
- exception exceptions.DTypePromotionError[源代码]#
多个 DTypes 无法转换为一个公共的 DType。
此异常派生自
TypeError,每当 dtypes 无法转换为一个单一的公共 dtypes 时就会引发。这可能是因为它们属于不同的类别/类,或者属于同一个类的兼容性不强的实例(参见示例)。备注
许多函数将使用类型提升来查找正确的结果和实现。对于这些函数,错误通常会与更具体的错误链式连接,表明对于输入的 dtypes 未找到实现。
通常,当 arr1 == arr2 可以安全地返回所有
False时,两个数组的 dtypes 之间的类型提升应被视为“无效”,因为 dtypes 根本不同。示例
Datetimes 和复数是不同的类,无法进行类型提升。
>>> 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]'>)
例如,对于结构化 dtypes,结构可能不匹配,并且当给出两个结构字段数量不匹配的结构化 dtypes 时,会引发相同的
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.