In working with some of our data, I had to perform a pretty basic conditional combinations of columns. After filling null values, attempted to add to columns in the assignment of a new variable. One of the columns ended up being object, which is not at all unprecedented. What I found, however, was that seemingly valid values would not convert to float (e.g. 4,789.67). After much searching, it seems that every solution I have seen points to the existence of an irregular character (which does not describe my case). Consequently, I tried to experiment in IPython to recreate the error, and I was successful. I do not understand, however, why I got this error:
TEST
z='4,534.07' #initial assignment
print z
print type(z) #checked type
print repr(z) #tried to reveal hidden characters
print repr(z.replace("'","")) #tried to remove excess quotes
print z[1:-1] #tried again to remove excess quotes
print float(z) #failed conversion attempt
OUTPUT
4,534.07
<type 'str'>
'4,534.07'
'4,534.07'
,534.0
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-70-8a3c46ebe6ab> in <module>()
6 print z[1:-1]
7 print z
----> 8 print float(z)
ValueError: invalid literal for float(): 4,534.07
The solutions I have seen for the basic conversion question invariably suggest the following for conversion of 'x' to float -->> float(x). I would be very grateful for anyone who can explain what I have missed. (I have not had this happen before.)
I have been using the Enthought platform:
Release notes Canopy 1.0.0.1160
Canopy 1.0.0
First release. See Documention Browser, Canopy Users Guide for release notes describing what's new and any known issues and workarounds
Thanks
the existence of an irregular character. Do you think the comma is a regular character for a number?locale-based answer needs to uselocale.atofrather thanlocale.atoi)… but yeah, I think it's close enough to be a dup.