如何检查 NaN 值
float('nan')
表示 NaN(不是数字)。 但我该如何检查呢?
float('nan')
represents NaN (not a number). But how do I check for it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
float('nan')
表示 NaN(不是数字)。 但我该如何检查呢?
float('nan')
represents NaN (not a number). But how do I check for it?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(20)
使用
math.isnan
:Use
math.isnan
:测试 NaN 的常用方法是查看它是否等于其自身:
The usual way to test for a NaN is to see if it's equal to itself:
numpy.isnan(number)
告诉您它是否为NaN
。numpy.isnan(number)
tells you if it'sNaN
or not.您可以通过以下三种方法来测试变量是否为“NaN”。
输出:
Here are three ways where you can test a variable is "NaN" or not.
Output:
编者注:下面的计时是有缺陷的,例如,它们没有考虑名称查找时间。 请参阅评论。
似乎检查它是否等于自身 (
x != x
) 是最快的。Editor's note: The below timings are flawed, for example, they have not factored out name lookup time. See the comments.
It seems that checking if it's equal to itself (
x != x
) is the fastest.答案
float('nan')
、numpy.nan
...按照标准实现的 NaN 是与其自身进行不等式比较应返回 True 的唯一值:
以及一些示例:
输出:
here is an answer working with:
float('nan')
,numpy.nan
...A NaN implemented following the standard, is the only value for which the inequality comparison with itself should return True:
And some examples:
Output:
实际上我刚刚遇到了这个,但对我来说它是检查 nan、-inf 或 inf。 我刚刚使用了
This is true for numeric, false for nan 和 inf,并且会引发字符串或其他类型之类的异常(这可能是一件好事)。 此外,这不需要导入任何库,如 math 或 numpy(numpy 太大了,它使任何编译的应用程序的大小增加了一倍)。
I actually just ran into this, but for me it was checking for nan, -inf, or inf. I just used
This is true for numbers, false for nan and both inf, and will raise an exception for things like strings or other types (which is probably a good thing). Also this does not require importing any libraries like math or numpy (numpy is so damn big it doubles the size of any compiled application).
math.isnan()
或将数字与其本身进行比较。 NaN 总是 != NaN,否则(例如,如果它是一个数字)比较应该成功。
math.isnan()
or compare the number to itself. NaN is always != NaN, otherwise (e.g. if it is a number) the comparison should succeed.
好吧,我输入了这篇文章,因为我对该函数有一些问题:
运行此代码时出现问题:
它引发异常。
我的解决方案是进行另一次检查:
Well I entered this post, because i've had some issues with the function:
There are problem when you run this code:
It raises exception.
My solution for that is to make another check:
如果你陷入<2.6,你没有 numpy,并且没有 IEEE 754 支持,还有另一种方法:
Another method if you're stuck on <2.6, you don't have numpy, and you don't have IEEE 754 support:
与 python < 2.6 我最终得到了
This Works for me with python 2.5.1 on a Solaris 5.9 box and with python 2.6.5 on Ubuntu 10
With python < 2.6 I ended up with
This works for me with python 2.5.1 on a Solaris 5.9 box and with python 2.6.5 on Ubuntu 10
比较
pd.isna
、math.isnan
和np.isnan
以及它们处理不同类型对象的灵活性。下表显示了是否可以使用给定方法检查对象的类型:
pd.isna
检查不同类型缺失值的最灵活的方法。
所有答案都没有涵盖 pd.isna 的灵活性。 虽然
math.isnan
和np.isnan
将为NaN
值返回True
,但您无法检查不同类型的None
等对象或字符串。 这两种方法都会返回错误,因此检查混合类型的列表会很麻烦。 而pd.isna
很灵活,将为不同类型的类型返回正确的布尔值:Comparison
pd.isna
,math.isnan
andnp.isnan
and their flexibility dealing with different type of objects.The table below shows if the type of object can be checked with the given method:
pd.isna
The most flexible method to check for different types of missing values.
None of the answers cover the flexibility of
pd.isna
. Whilemath.isnan
andnp.isnan
will returnTrue
forNaN
values, you cannot check for different type of objects likeNone
or strings. Both methods will return an error, so checking a list with mixed types will be cumbersom. This whilepd.isna
is flexible and will return the correct boolean for different kind of types:我正在从 Web 服务接收数据,该服务将
NaN
作为字符串'Nan'
发送。 但我的数据中也可能存在其他类型的字符串,因此简单的float(value)
可能会引发异常。 我使用了已接受答案的以下变体:要求:
I am receiving the data from a web-service that sends
NaN
as a string'Nan'
. But there could be other sorts of string in my data as well, so a simplefloat(value)
could throw an exception. I used the following variant of the accepted answer:Requirement:
判断变量是 NaN 还是 None 的所有方法:
None 类型
NaN 类型
All the methods to tell if the variable is NaN or None:
None type
NaN type
在 Python 3.6 中,检查字符串值 x math.isnan(x) 和 np.isnan(x) 会引发错误。
因此,如果我事先不知道给定值是一个数字,我无法检查给定值是否为 NaN。
以下似乎可以解决这个问题
In Python 3.6 checking on a string value x math.isnan(x) and np.isnan(x) raises an error.
So I can't check if the given value is NaN or not if I don't know beforehand it's a number.
The following seems to solve this issue
如何从混合数据类型列表中删除 NaN(浮点)项
如果可迭代中有混合类型,这里有一个不使用 numpy 的解决方案:
短路评估意味着
isnan
不会在非“float”类型的值上调用,因为False 和 (…)
会快速计算为False
,而无需评估右侧。How to remove NaN (float) item(s) from a list of mixed data types
If you have mixed types in an iterable, here is a solution that does not use numpy:
Short-circuit evaluation means that
isnan
will not be called on values that are not of type 'float', asFalse and (…)
quickly evaluates toFalse
without having to evaluate the right-hand side.如果您想检查非 NaN 的值,则对用于标记 NaN 的任何内容取反; pandas 有自己的专用函数来标记非 NaN 值。
如果您想过滤非 NaN 的值,这尤其有用。 对于 ndarray/Series 对象,
==
是矢量化的,因此也可以使用它。If you want to check for values that are not NaN, then negate whatever is used to flag NaNs; pandas has its own dedicated function for flagging non-NaN values.
This is especially useful if you want to filter values that are not NaN. For ndarray/Series objects,
==
is vectorized, so it can be used as well.要过滤掉“num_specimen_seen”列中的空字符串 (
''
)、None 和NaN
值,我们可以使用pd.notna() 函数。
在迭代 DataFrame 时,此代码将跳过“num_specimen_seen”列中的
NaN
和空字符串。To filter out both empty strings (
''
), None andNaN
values in the 'num_specimen_seen' column, we can use thepd.notna()
function from pandas.This code will skip both
NaN
and empty strings in the 'num_specimen_seen' column when iterating over the DataFrame.对于 panda 中的字符串,采用 pd.isnull:
作为 NLTK 特征提取的函数
for strings in panda take pd.isnull:
the function as feature extraction for NLTK