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

John Machin sjmachin at
Sat Jun 14 06:07:54 CEST 2008

On Jun 14, 1:45 pm, "John [H2O]" <washa... at> wrote:
> I have a script:
> from numpy import float
> OutD=[]
> v=['3','43','23.4','NaN','43']
> OutD.append([float(i) for i in v[1]])
> On linux:
> Python 2.5.1 (r251:54863, Mar  7 2008, 04:10:12)
> [GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
> [john at andLinux analysis]$ python
> [[3.0, 43.0, 23.399999999999999, nan, 43.0]]
> On XP:
> Python 2.5 (r25:51908, Mar  9 2007, 17:40:28) [MSC v.1310 32 bit (Intel)]
> Microsoft Windows XP [Version 5.1.2600]
> (C) Copyright 1985-2001 Microsoft Corp.
> C:\analysis>C:\Python25\python.exe
> Traceback (most recent call last):
>   File "", line 4, in <module>
>     OutD.append([float(i) for i in v])
> ValueError: invalid literal for float(): NaN
> WTF?

Avoid impolite astonishment; RTFloatingM instead:
float( [x])

Convert a string or a number to floating point. If the argument is a
string, it must contain a possibly signed decimal or floating point
number, possibly embedded in whitespace. Otherwise, the argument may
be a plain or long integer or a floating point number, and a floating
point number with the same value (within Python's floating point
precision) is returned. If no argument is given, returns 0.0.

Note: When passing in a string, values for NaN and Infinity may be
returned, depending on the underlying C library. The specific set of
strings accepted which cause these values to be returned depends
entirely on the C library and is known to vary.

You may like to suggest a minor extension to the docs: after "is known
to vary" add "and may even be empty".


More information about the Python-list mailing list