Python 如何检查NaN值?

Python 如何检查NaN值?,python,math,Python,Math,float('nan')产生nan(不是数字)。但我如何检查它呢?应该很容易,但我找不到它。测试NaN的通常方法是查看它是否等于自身: def isNaN(num): return num != num 如果x是NaN(不是数字),则返回True,否则返回False 或者将数字与自身进行比较。楠总是这样NaN,否则(例如,如果它是一个数字)比较应该会成功。numpy。isnan(数字)告诉您它是否是NaN。如果您坚持使用python>>值=浮点(nan) >>>类型(值) >>>

float('nan')
产生nan(不是数字)。但我如何检查它呢?应该很容易,但我找不到它。

测试NaN的通常方法是查看它是否等于自身:

def isNaN(num):
    return num != num

如果x是NaN(不是数字),则返回
True
,否则返回
False


或者将数字与自身进行比较。楠总是这样NaN,否则(例如,如果它是一个数字)比较应该会成功。

numpy。isnan(数字)
告诉您它是否是
NaN

如果您坚持使用python<2.6,则使用另一种方法

def isNaN(x):
    return str(float(x)).lower() == 'nan'

这对我来说适用于Solaris 5.9上的python 2.5.1和Ubuntu 10上的python 2.6.5

math.isnan()
运行此代码时出现问题:

a = "hello"
math.isnan(a)
这引发了一个例外。 我的解决方案是再做一次检查:

def is_nan(x):
    return isinstance(x, float) and math.isnan(x)

实际上,我刚刚遇到了这个问题,但对我来说,它是在检查nan、-inf或inf。我只是使用了

if float('-inf') < float(num) < float('inf'):
if float('-inf')

这对于数字是正确的,对于nan和inf都是错误的,并且对于字符串或其他类型(这可能是一件好事)会引发异常。此外,这不需要导入任何库,如math或numpy(numpy非常大,它的大小是任何已编译应用程序的两倍)。

我从一个web服务接收数据,该服务以字符串形式发送
NaN
。但我的数据中也可能有其他类型的字符串,因此一个简单的
float(value)
可能会引发异常。我使用了以下已接受答案的变体:

def isnan(value):
  try:
      import math
      return math.isnan(float(value))
  except:
      return False
要求:

isnan('hello') == False
isnan('NaN') == True
isnan(100) == False
isnan(float('nan')) = True

判断变量是NaN还是None的所有方法:

无类型

In [1]: from numpy import math

In [2]: a = None
In [3]: not a
Out[3]: True

In [4]: len(a or ()) == 0
Out[4]: True

In [5]: a == None
Out[5]: True

In [6]: a is None
Out[6]: True

In [7]: a != a
Out[7]: False

In [9]: math.isnan(a)
Traceback (most recent call last):
  File "<ipython-input-9-6d4d8c26d370>", line 1, in <module>
    math.isnan(a)
TypeError: a float is required

In [10]: len(a) == 0
Traceback (most recent call last):
  File "<ipython-input-10-65b72372873e>", line 1, in <module>
    len(a) == 0
TypeError: object of type 'NoneType' has no len()
In [11]: b = float('nan')
In [12]: b
Out[12]: nan

In [13]: not b
Out[13]: False

In [14]: b != b
Out[14]: True

In [15]: math.isnan(b)
Out[15]: True

以下是一个解决方案:

  • 遵守IEEE 754标准的NaN实现
    • ie:python的NaN:
      float('NaN')
      numpy.NaN
  • 任何其他对象:字符串或其他任何对象(如果遇到,不会引发异常)
按照标准执行的NaN是唯一一个与自身进行不平等性比较时应返回True的值:

def is_nan(x):
    return (x != x)
还有一些例子:

import numpy as np
values = [float('nan'), np.nan, 55, "string", lambda x : x]
for value in values:
    print(f"{repr(value):<8} : {is_nan(value)}")
将numpy导入为np
值=[float('nan'),np.nan,55,“string”,lambda x:x]
对于值中的值:
打印(f{repr(值):
对于浮点数类型的nan

>>将熊猫作为pd导入
>>>值=浮点(nan)
>>>类型(值)
>>> 
>>>pd.isnull(值)
真的
>>>
>>>值='nan'
>>>类型(值)
>>> 
>>>pd.isnull(值)
假的

对于panda take pd.isnull中的字符串:

if not pd.isnull(atext):
  for word in nltk.word_tokenize(atext):
NLTK的特征提取功能

def act_features(atext):
features = {}
if not pd.isnull(atext):
  for word in nltk.word_tokenize(atext):
    if word not in default_stopwords:
      features['cont({})'.format(word.lower())]=True
return features
如何从混合数据类型列表中删除NaN(浮点)项 如果iterable中有混合类型,则有一个不使用numpy的解决方案:

from math import isnan

Z = ['a','b', float('NaN'), 'd', float('1.1024')]

[x for x in Z if not (
                      type(x) == float # let's drop all float values…
                      and isnan(x) # … but only if they are nan
                      )]
[a',b',d',1.1024] 短路评估意味着不会对非“浮点”类型的值调用
isnan
,因为
False和(…)
快速评估为
False
,而无需评估右侧。

这里有三种方法可以测试变量是否为“NaN”。 输出

It's pd.isna  : True
It's np.isnan  : True
It's math.isnan  : True

在Python3.6中,检查字符串值x math.isnan(x)和np.isnan(x)会引发错误。 因此,如果我事先不知道它是一个数字,我就无法检查给定的值是否为NaN。 下面的内容似乎可以解决这个问题

if str(x)=='nan' and type(x)!='str':
    print ('NaN')
else:
    print ('non NaN')

检查它是否等于自身

x!=x
是最快的。

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)


关于python中NaN的一些历史,请参阅PEP 754。警告之词:引用下面的Bear评论“对于python用户,我确信,在运算符重载的情况下,有很多方法可能会混淆此函数。请使用math.isnan()上面提到的754规范中说,NaN==NaN应该总是错误的,尽管它并不总是这样实现的。这不可能是数学和/或numpy在引擎盖下检查这个的方式吗?即使这是可行的,并且在一定程度上是有道理的,我是一个有原则的人,我在此声明这是被禁止的巫术。请使用改为math.isnan。@djsadinoff混淆还有其他缺点吗?math.isnan()不能检查字符串值,所以这个解决方案看起来更健壮。这不是太容易移植,因为Windows有时会调用它
-1。35;IND
在python版本2.7中也可以工作。
numpy.all(numpy.isnan(数据列表))
如果需要确定列表中的所有元素是否都是NaNy,那么它也很有用。不需要NumPy:
all(map(math.isnan,[float(“nan”)]*5))
当这个答案在6年前编写时,Python 2.5仍在普遍使用——math.isnan不是标准库的一部分。现在,我真的希望在很多地方不是这样!请注意,np.isnan()不处理decimal.decimal类型(与许多numpy函数一样)。math.isnan()没有处理。它可能被否决,因为isnan()接受浮点,而不是字符串。函数没有任何问题,问题只是在他尝试使用它时出现。(对于那个特定的用例,他的解决方案是有效的,但它不是这个问题的答案。)以这种方式检查类型时要小心。这将不起作用,例如对于numpy.float32 NaN。最好使用try/except构造:
def is_NaN(x):try:return math.isnan(x)除非:return False
NaN并不意味着值不是有效的数字。IEEE浮点表示法的一部分是指定特定结果未定义。例如,0/0。因此询问“hello”is nan是没有意义的。这更好,因为nan可以在任何字符串、int或float列表中着陆,所以有用的检查我必须实现这一点来处理pandas中的字符串列。或者
尝试:int(value)
@chwi那么你的建议说明了
value
还是不是
nan
呢?嗯,“不是数字”,任何不能浇铸到 ['a', 'b', 'd', 1.1024]
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)}}")
It's pd.isna  : True
It's np.isnan  : True
It's math.isnan  : True
if str(x)=='nan' and type(x)!='str':
    print ('NaN')
else:
    print ('non NaN')
x!=x
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)