[Numpy-discussion] annoying numpy string to float conversion behaviour
matthieu.brucher at gmail.com
Wed Jun 20 04:45:14 EDT 2007
This was discussed some time ago (I started it because I had exactly the
same problem), numpy is not responsible for this, Python is. Python uses the
C standard library and in C by MS, NaN and Inf can be displayed, but not
read from a string, so this is the behaviour displayed here.
Wait for Python 3k...
2007/6/20, Torgil Svensson <torgil.svensson at gmail.com>:
> Is there a reason for numpy.float not to convert it's own string
> representation correctly?
> Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
> (Intel)] on win32>>> import numpy
> >>> numpy.__version__
> >>> numpy.float("1.0")
> >>> numpy.nan
> >>> numpy.float("-1.#IND")
> Traceback (most recent call last):
> File "<pyshell#20>", line 1, in <module>
> ValueError: invalid literal for float(): -1.#IND
> Also, nan and -nan are represented differently for different float to
> string conversion methods. I guess the added zeros are a bug
> >>> str(nan)
> >>> "%f" % nan
> >>> str(-nan)
> >>> "%f" % -nan
> This is a problem when floats are stored in text-files that are later
> read to be numerically processed. For now I use the following to
> convert the number.
> def string_to_number(x):
> if x in special_numbers:
> return special_numbers[x]
> return float(x) if ("." in x) or ("e" in x) else int(x)
> Is there a simpler way that I missed?
> Best Regards,
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion