Reputation: 16661
I have a .txt data where columns 6 and 7 are GPS position in the form:
50;18.5701400N,4;07.7693770E
When I read it by read_csv I try to convert it to cartesian coordinates by using converters. I wrote the function for converter
convertFunc = lambda x : float((x[0:5]+x[6:12]).replace(';','.'))
convert = {6:convertFunc,7:convertFunc}
when I use it on single value it works how I would like:
convertFunc(myData.Lat[1])
Out [159]: 55.187110250000003
when I try to use it in read_csv it does not work
myData = DataFrame(read_csv('~/data.txt', sep=',' names=['A', 'B', 'C', 'D', 'E', 'Lat', 'Long'],converters=convert))
I have an error:
...
convertFunc = lambda x : float((x[0:5] + x[6:12]).replace(';', '.'))
ValueError: invalid literal for float(): DGPS ongitu
I don't know where do it wrong or what I misunderstand in converters? Or maybe anyone knows good way (package) to work with GPS data in that form?
(I think it can be some problem with lambda
When I want to apply my function to the column I have an error: TypeError: only length-1 arrays can be converted to Python scalars
)
Upvotes: 2
Views: 8898
Reputation: 69116
Your converter is not ok.
In [67]: convertFunc = lambda x : float((x[0:5]+x[6:12]).replace(';','.'))
In [68]: convertFunc('4;07.7693770E')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
...
ValueError: invalid literal for float(): 4.07.693770
On top of a dodgy converter, i think you apply the converter to the wrong column (look at the exception you get).
Upvotes: 0
Reputation: 179392
That converter is a bit hacky; might I recommend something more robust like this?
def convert_dmds(s):
deg, min = s[:-1].split(';')
sign = 1 if s[-1] in 'NE' else -1
return sign * (float(deg) + float(min) / 60.0)
def convert_gps(s):
lat, lon = s.split(',')
return (convert_dmds(lat), convert_dmds(lon))
Also, the error indicates that you are trying to convert something that is clearly not a GPS string -- a header row, perhaps?
Upvotes: 7