最佳答案
我有一个浮点数组(一些普通数字,一些浮点数) ,它来自于一个熊猫数据框的应用程序。
由于某种原因,numpy.isnan 在这个数组上失败了,但是如下所示,每个元素是一个浮点数,numpy.isnan 在每个元素上正确运行,变量的类型肯定是一个 numpy 数组。
怎么回事!
set([type(x) for x in tester])
Out[59]: {float}
tester
Out[60]:
array([-0.7000000000000001, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan], dtype=object)
set([type(x) for x in tester])
Out[61]: {float}
np.isnan(tester)
Traceback (most recent call last):
File "<ipython-input-62-e3638605b43c>", line 1, in <module>
np.isnan(tester)
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
set([np.isnan(x) for x in tester])
Out[65]: {False, True}
type(tester)
Out[66]: numpy.ndarray