numpy: handling float('NaN') different in XP vs. Linux

Robert Kern robert.kern at gmail.com
Mon Jun 16 21:57:06 CEST 2008


John [H2O] wrote:
> Dan Bishop wrote:
>>
>> Python just uses the atof() function from the underlying C library.
>> Some of them handle NaN's, and some of them don't.
> 
> As a work around, how would I write this in list comprehension form:
> 
>          newlist=[]
>          for i in range(len(v[1])):
>             try:
>                newlist.append(float(v[1][i]))
>             except:
>                newlist.append(-999.99) # or just nan possibly?

from numpy import nan

def nanfloat(x):
     if x.lower() == 'nan':
         return nan
     else:
         return float(x)

newlist = [myfloat(x) for x in v[1]]

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list