Python pandas .isnull()不适用于对象dtype中的NaT

问题描述 投票:7回答:3

我有这个系列:

ser=pd.Series([11,22,33,np.nan,np.datetime64('nat')],name='my_series')

该系列看起来像这样:

0     11
1     22
2     33
3    NaN
4    NaN
Name: my_series, dtype: object

但我只得到一个True为NULL值:

ser.isnull()

0    False
1    False
2    False
3     True
4    False
Name: my_series, dtype: bool

这是一个错误或如何正确计算熊猫系列中的NULL值?这没有帮助:

ser=ser.replace('NaN',np.nan)

谢谢!

python datetime pandas isnull
3个回答
0
投票

要解决这个问题,你也可以这样做

series.apply(lambda x: str(x) == "nat")

然后,如果需要,您仍然可以使用np.datetime


0
投票

令人惊讶的是,在执行了您给出的代码后,我得到了以下输出:enter image description here

Alternate way is: count those values which you are able to count.

计算系列对象的长度,减法将给出空值的计数。如下:**

enter image description here

len(ser)-(ser[ser.isnull()==False]).count()

0
投票

今天早上我遇到了一个类似的问题但是在str系列中!这对我和你的样本数据也很有用:

pd.isna(ser)

© www.soinside.com 2019 - 2024. All rights reserved.