为什么isnull()可以工作,但是==None不能工作?

2024-04-25 04:54:04 发布

您现在位置:Python中文网/ 问答频道 /正文


Tags: python
1条回答
网友
1楼 · 发布于 2024-04-25 04:54:04

如上注释所述,pandas中缺少的数据由NaN表示,其中NaN是一个数值,即浮点类型。但是None是PythonNoneType,因此NaN将不等价于None。你知道吗

In [27]: np.nan == None
Out[27]: False

在此Github thread中,他们进一步讨论,指出:

This was done quite a while ago to make the behavior of nulls consistent, in that they don't compare equal. This puts None and np.nan on an equal (though not-consistent with python, BUT consistent with numpy) footing.

这意味着当你做df[df['label'] == None],你要elementwise检查np.nan == np.nan,我们知道是假的。你知道吗

In [63]: np.nan == np.nan
Out[63]: False

此外,在应用Boolean indexing时,不应执行df[df['label'] == None],对NoneType使用==不是最佳实践,正如PEP8所述:

Comparisons to singletons like None should always be done with is or is not, never the equality operators.

例如,您可以执行tst.value.apply(lambda x: x is None),这会产生与.isnull()相同的结果,说明pandas如何将它们视为NaNs。注意这是下面的tst数据帧示例,其中tst.value.dtypes是一个object,我已经显式指定了NoneType元素。你知道吗

pandas文档中有一个很好的example说明了这一点及其效果。你知道吗

例如,如果您有两个列,一个是float类型,另一个是object,您可以看到pandas如何以一种很好的方式处理None类型,请注意float它使用的是NaN。你知道吗

In [32]: tst = pd.DataFrame({"label" : [1, 2, None, 3, None], "value" : ["A", "B", None, "C", None]})

Out[39]:
   label value
0    1.0     A
1    2.0     B
2    NaN  None
3    3.0     C
4    NaN  None

In [51]: type(tst.value[2])
Out[51]: NoneType

In [52]: type(tst.label[2])
Out[52]: numpy.float64

这篇文章很好地解释了NaNNone之间的区别,肯定会看看这个。你知道吗

相关问题 更多 >