gim*_*mel 1112
检查浮点数x是否为NaN(不是数字).NaN是IEEE 754标准的一部分.操作,例如但不限于inf*0,inf/inf或涉及NaN的任何操作,例如nan*1,返回NaN.
2.6版中的新功能.
>>> import math
>>> x = float('nan')
>>> math.isnan(x)
True
Run Code Online (Sandbox Code Playgroud)
Chr*_*ung 316
测试NaN的常用方法是查看它是否与自身相同:
def isNaN(num):
return num != num
Run Code Online (Sandbox Code Playgroud)
mav*_*vnn 132
numpy.isnan(number)NaN在Python 2.5中告诉你它是否存在.
Grz*_*orz 31
似乎检查它是否等于自身
x!=x
Run Code Online (Sandbox Code Playgroud)
是最快的。
import pandas as pd
import numpy as np
import math
x = float('nan')
%timeit x!=x
44.8 ns ± 0.152 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
%timeit math.isnan(x)
94.2 ns ± 0.955 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
%timeit pd.isna(x)
281 ns ± 5.48 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit np.isnan(x)
1.38 µs ± 15.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Run Code Online (Sandbox Code Playgroud)
Dav*_*ist 28
我实际上只是碰到了这个,但对我来说,它正在检查nan,-inf或inf.我刚刚用过
if float('-inf') < float(num) < float('inf'):
Run Code Online (Sandbox Code Playgroud)
这对于数字来说是正确的,对于nan和inf都是假的,并且会对字符串或其他类型(这可能是一件好事)引发异常.此外,这不需要导入任何库,如数学或numpy(numpy是如此的大,它是任何编译的应用程序的大小翻倍).
x0s*_*x0s 28
这是一个回答:
float('nan')np.nan这里是:
import numpy as np
def is_nan(x):
return (x is np.nan or x != x)
Run Code Online (Sandbox Code Playgroud)
还有一些例子:
values = [float('nan'), np.nan, 55, "string", lambda x : x]
for value in values:
print "{:<8} : {}".format(repr(value), is_nan(value))
Run Code Online (Sandbox Code Playgroud)
输出:
nan : True
nan : True
55 : False
'string' : False
<function <lambda> at 0x000000000927BF28> : False
Run Code Online (Sandbox Code Playgroud)
M. *_*put 24
import pandas as pd
import numpy as np
import math
#For single variable all three libraries return single boolean
x1 = float("nan")
print(f"It's pd.isna : {pd.isna(x1)}")
print(f"It's np.isnan : {np.isnan(x1)}")
print(f"It's math.isnan : {math.isnan(x1)}")
Run Code Online (Sandbox Code Playgroud)
输出量
import pandas as pd
import numpy as np
import math
#For single variable all three libraries return single boolean
x1 = float("nan")
print(f"It's pd.isna : {pd.isna(x1)}")
print(f"It's np.isnan : {np.isnan(x1)}")
print(f"It's math.isnan : {math.isnan(x1)}")
Run Code Online (Sandbox Code Playgroud)
Tom*_*lak 22
或者将数字与自身进行比较.NaN总是!= NaN,否则(例如,如果它是一个数字)比较应该成功.
Jos*_*Lee 17
另一种方法,如果你坚持<2.6,你没有numpy,并且你没有IEEE 754支持:
def isNaN(x):
return str(x) == str(1e400*0)
Run Code Online (Sandbox Code Playgroud)
好吧,我输入了这篇文章,因为我的功能存在一些问题:
math.isnan()
Run Code Online (Sandbox Code Playgroud)
运行此代码时出现问题:
a = "hello"
math.isnan(a)
Run Code Online (Sandbox Code Playgroud)
它引发了例外.我的解决方案是再次检查:
def is_nan(x):
return isinstance(x, float) and math.isnan(x)
Run Code Online (Sandbox Code Playgroud)
比较pd.isna,math.isnan以及np.isnan它们处理不同类型对象的灵活性。
下表显示了是否可以使用给定方法检查对象的类型:
+------------+-----+---------+------+--------+------+
| Method | NaN | numeric | None | string | list |
+------------+-----+---------+------+--------+------+
| pd.isna | yes | yes | yes | yes | yes |
| math.isnan | yes | yes | no | no | no |
| np.isnan | yes | yes | no | no | yes | <-- # will error on mixed type list
+------------+-----+---------+------+--------+------+
Run Code Online (Sandbox Code Playgroud)
pd.isna检查不同类型缺失值的最灵活的方法。
没有一个答案涵盖了 的灵活性pd.isna。虽然math.isnan和np.isnan将返回True值NaN,但您无法检查不同类型的对象,例如None或 字符串。这两种方法都会返回错误,因此检查混合类型的列表会很麻烦。这个 whilepd.isna是灵活的,将为不同类型的类型返回正确的布尔值:
+------------+-----+---------+------+--------+------+
| Method | NaN | numeric | None | string | list |
+------------+-----+---------+------+--------+------+
| pd.isna | yes | yes | yes | yes | yes |
| math.isnan | yes | yes | no | no | no |
| np.isnan | yes | yes | no | no | yes | <-- # will error on mixed type list
+------------+-----+---------+------+--------+------+
Run Code Online (Sandbox Code Playgroud)
随着python <2.6,我最终得到了
def isNaN(x):
return str(float(x)).lower() == 'nan'
Run Code Online (Sandbox Code Playgroud)
这适用于Solaris 5.9机器上的python 2.5.1和Ubuntu 10上的python 2.6.5
我正在从NaN作为字符串发送的网络服务接收数据'Nan'。但是我的数据中也可能有其他类型的字符串,所以一个简单的float(value)可能会引发异常。我使用了以下已接受答案的变体:
def isnan(value):
try:
import math
return math.isnan(float(value))
except:
return False
Run Code Online (Sandbox Code Playgroud)
要求:
isnan('hello') == False
isnan('NaN') == True
isnan(100) == False
isnan(float('nan')) = True
Run Code Online (Sandbox Code Playgroud)