pandas.isnull() 不适用于小数类型?

pandas.isnull() not working on decimal type?

我是不是遗漏了什么,或者我们对 pandas.isnull() 有意见吗?

>>> import pandas as pd
>>> import decimal
>>> d = decimal.Decimal('NaN')
>>> d
Decimal('NaN')
>>> pd.isnull(d)
False
>>> f = float('NaN')
>>> f
nan
>>> pd.isnull(f)
True
>>> pd.isnull(float(d))
True

问题是我有一个包含 decimal.Decimal 值的数据框,并且 df.dropna() 不会因此删除 NaN...

是的,这不受支持,您可以使用 NaN 不等于自身的 属性,它仍然适用于 Decimal 类型:

In [20]:
import pandas as pd
import decimal
d = decimal.Decimal('NaN')
df = pd.DataFrame({'a':[d]})
df

Out[20]:
     a
0  NaN

In [21]:    
df['a'].apply(lambda x: x != x)

Out[21]:
0    True
Name: a, dtype: bool

所以你可以这样做:

In [26]:
df = pd.DataFrame({'a':[d,1,2,3]})
df[df['a'].apply(lambda x: x == x)]

Out[26]:
   a
1  1
2  2
3  3