How can I check for NaN values?

后端 未结 17 1864
盖世英雄少女心
盖世英雄少女心 2020-11-22 05:02

float(\'nan\') results in Nan (not a number). But how do I check for it? Should be very easy, but I cannot find it.

相关标签:
17条回答
  • 2020-11-22 05:41

    numpy.isnan(number) tells you if it's NaN or not.

    0 讨论(0)
  • 2020-11-22 05:45

    Another method if you're stuck on <2.6, you don't have numpy, and you don't have IEEE 754 support:

    def isNaN(x):
        return str(x) == str(1e400*0)
    
    0 讨论(0)
  • 2020-11-22 05:45

    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:

    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]

    Short-circuit evaluation means that isnan will not be called on values that are not of type 'float', as False and (…) quickly evaluates to False without having to evaluate the right-hand side.

    0 讨论(0)
  • 2020-11-22 05:46

    It seems that checking if it's equal to itself

    x!=x
    

    is the fastest.

    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)
    
    
    0 讨论(0)
  • 2020-11-22 05:47

    here is an answer working with:

    • NaN implementations respecting IEEE 754 standard
      • ie: python's NaN: float('nan'), numpy.nan...
    • any other objects: string or whatever (does not raise exceptions if encountered)

    A NaN implemented following the standard, is the only value for which the inequality comparison with itself should return True:

    def is_nan(x):
        return (x != x)
    

    And some examples:

    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)}")
    

    Output:

    nan      : True
    nan      : True
    55       : False
    'string' : False
    <function <lambda> at 0x000000000927BF28> : False
    
    0 讨论(0)
  • 2020-11-22 05:48

    With python < 2.6 I ended up with

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

    This works for me with python 2.5.1 on a Solaris 5.9 box and with python 2.6.5 on Ubuntu 10

    0 讨论(0)
提交回复
热议问题