pandas.isnull()
(也是pd.isna()
,在较新的版本中)检查数字数组和字符串/对象数组中的缺失值。从文档中,它检查:
数字数组中的NaN,对象数组中的None / NaN
快速示例:
import pandas as pd
import numpy as np
s = pd.Series(['apple', np.nan, 'banana'])
pd.isnull(s)
Out[9]:
0 False
1 True
2 False
dtype: bool
numpy.nan
用来表示缺失值的想法是pandas
引入的,这就是为什么pandas
有工具来处理它的原因。
日期时间也是如此(如果使用pd.NaT
,则无需指定dtype)
In [24]: s = Series([Timestamp('20130101'),np.nan,Timestamp('20130102 9:30')],dtype='M8[ns]')
In [25]: s
Out[25]:
0 2013-01-01 00:00:00
1 NaT
2 2013-01-02 09:30:00
dtype: datetime64[ns]``
In [26]: pd.isnull(s)
Out[26]:
0 False
1 True
2 False
dtype: bool