[python] Numpy isnan()在一系列浮点数上失败(来自pandas dataframe apply)



Answers

在@unubtu的答案之上,你可以将pandas numpy对象数组强制转换为native(float64)类型,就行了

import pandas as pd
pd.to_numeric(df['tester'], errors='coerce')

指定errors ='coerce'以强制无法解析为数值的字符串变为NaN。 列类型将是dtype: float64 ,然后isnan检查应该工作

Question

我有一个浮点数组(一些正常数字,一些nans)来自一个pandas数据帧的应用程序。

由于某种原因,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



Links