python - 如何检查 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 本身的两个列表的最干净的方法是什么?
解决方案
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
推荐阅读
- javascript - @ 用于处理 vue js 中的资产不起作用
- c# - 在哪里生成 SignInManager IdentityUser .cshtml 文件
- handlebars.js - BigCommerce 在主页上显示品牌(模板)
- shopping-cart - 如何在 3dcart 中访问追加销售商品的定价水平?
- python - 错误:在 Module.__init__() 调用之前无法分配模块,即使在使用 super(DecoderRNN, self).__init__() 之后也是如此
- ubuntu - OpenNMS 快速安装指南 UBUNTU 初始化 OpenNMS .... 失败
- python - 在 Python 和 Keras 中使用 HDF5 文件的数据生成器
- javascript - 使用 Tildas 的目标 ID
- c++ - C++ 如何转换 std::vector
到 const std::vector ? - node.js - 使用 brew 将节点版本 10.7.0 降级到 10.6.0