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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32863674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:57:06  来源:igfitidea点击:

Python pandas .isnull() does not work on NaT in object dtype

pythondatetimepandasisnull

提问by ragesz

I have this series:

我有这个系列:

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

The series looks like this:

该系列看起来像这样:

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

But I get only one Truefor NULL values:

但我只得到一个TrueNULL 值:

ser.isnull()

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

Is it a bug or how can I count correctly the NULL values in a pandas series? This does not help:

这是错误还是如何正确计算Pandas系列中的 NULL 值?这没有帮助:

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

Thanks!

谢谢!

回答by nck

I ran into a similar issue this morning but in an str series! This worked for me and with your sample data as well:

我今天早上遇到了类似的问题,但在 str 系列中!这对我以及您的示例数据都有效:

pd.isna(ser)

pd.isna(ser)

回答by mirthbottle

To get around this, you can also do

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

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

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

Then you can still use np.datetime if you want

然后你仍然可以使用 np.datetime 如果你愿意

回答by Yogesh

Surprisingly I got following output after execution the code you given: enter image description here

令人惊讶的是,在执行您给出的代码后,我得到了以下输出: 在此处输入图片说明

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

替代方法是:计算您能够计算的那些值。

calculate length of series object and subtraction will give you the count for null values. As follows:**

计算系列对象的长度,减法将为您提供空值的计数。如下:**

enter image description here

在此处输入图片说明

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