How can I check for NaN values?

后端 未结 17 1862
盖世英雄少女心
盖世英雄少女心 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:56

    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

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

提交回复
热议问题