首页 > 解决方案 > 如何检查 Pandas/NumPy 任意对象是否包含 NaT/NaN/Null

问题描述

我想检查 Pandas 对象是否/包含任何 Null/NaN/NaT 值,但如果该对象是一个列表或一个奇异值,我事先没有任何信息。

我试过了

x = [1,2,3,pd.NaT]
if pd.notnull(x):
    ...

但是如果对象x是一个列表,它会返回这个值错误(由于它返回一个布尔值数组):

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

如果我这样做:

x = pd.NaT
if pd.notnull(x).any():
   ...

如果我收到一个奇异值,它会返回此错误:

AttributeError: 'bool' object has no attribute 'any'

能够处理可能包含 NaN 和 NaN 本身的两个列表的最干净的方法是什么?

标签: pythonpandasnull

解决方案


Wrap x inside of list and pass to pd.notna and chain any. It works because pd.notna returns numpy ndarray. Therefore, the any is actually ndarray.any. The When calling numpy ndarray.any with out axis parameter, it will check on all dimensions. Therefore, it works on both list x or single value x

x = [1,2,3,pd.NaT]

In [369]: pd.notna([x])
Out[369]: array([[ True,  True,  True, False]]) #it is 2d-array

In [370]: type(pd.notna([x]))
Out[370]: numpy.ndarray

In [373]: pd.notna([x]).any()  #`ndarray.any` checks on all dimensions of this 2d-array
Out[373]: True

In [374]: pd.notna([x]).all()  #`ndarray.all` checks on all dimensions of this 2d-array
Out[374]: False

On x is single pd.NaT

x = pd.NaT

In [377]: pd.notna([x])
Out[377]: array([False])  #it is 1d-array

In [378]: pd.notna([x]).any()
Out[378]: False

In [379]: pd.notna([x]).all()
Out[379]: False

推荐阅读