【发布时间】:2020-12-08 19:53:33
【问题描述】:
这是一个简单的 numpy 数组:
import numpy as np
x = np.array([15, 22, 3])
我想把第二个值转成nan:
x[1] = np.nan
它返回以下错误:
ValueError: cannot convert float NaN to integer
print(type(x[1])) # this returns "<class 'numpy.int32'>"
通过将整数数组转换为浮点数数组,我设法用 NaN 替换了所需的值。
但仍然。 当我实际尝试转换 int 时,为什么 numpy 告诉我无法转换 float NaN?
谢谢
【问题讨论】:
-
np.nan是一个浮点值。没有intnan,因此numpy无法将np.nan转换为int