问题描述
我有一组浮点数(一些正常数字,一些 nans),它们来自对 Pandas 数据框的应用.
I have an array of floats (some normal numbers, some nans) that is coming out of an apply on a pandas dataframe.
出于某种原因,numpy.isnan 在这个数组上失败了,但是如下所示,每个元素都是一个浮点数,numpy.isnan 在每个元素上正确运行,变量的类型肯定是一个 numpy 数组.
For some reason, numpy.isnan is failing on this array, however as shown below, each element is a float, numpy.isnan runs correctly on each element, the type of the variable is definitely a numpy array.
这是怎么回事?!
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
推荐答案
np.isnan
可以应用于原生 dtype 的 NumPy 数组(例如 np.float64):
np.isnan
can be applied to NumPy arrays of native dtype (such as np.float64):
In [99]: np.isnan(np.array([np.nan, 0], dtype=np.float64))
Out[99]: array([ True, False], dtype=bool)
但是在应用于对象数组时会引发 TypeError:
but raises TypeError when applied to object arrays:
In [96]: np.isnan(np.array([np.nan, 0], dtype=object))
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''
因为你有 Pandas,你可以使用 pd.isnull
相反——它可以接受对象或本机 dtypes 的 NumPy 数组:
Since you have Pandas, you could use pd.isnull
instead -- it can accept NumPy arrays of object or native dtypes:
In [97]: pd.isnull(np.array([np.nan, 0], dtype=float))
Out[97]: array([ True, False], dtype=bool)
In [98]: pd.isnull(np.array([np.nan, 0], dtype=object))
Out[98]: array([ True, False], dtype=bool)
请注意,None
在对象数组中也被视为空值.
Note that None
is also considered a null value in object arrays.
这篇关于Numpy isnan() 在浮点数组上失败(来自 Pandas 数据帧应用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!