pbugnion / gmaps

Google maps for Jupyter notebooks
https://jupyter-gmaps.readthedocs.io/en/stable/
Other
760 stars 147 forks source link

help request: importing from local csv #71

Closed pleabargain closed 8 years ago

pleabargain commented 8 years ago

gmap notebook.ipynb.zip Hello

I have attached the ipynb file that I used while working on this problem. I removed the API key.

goal: load simple local csv files directly.

I have tried many options(see below). What code would you recommend I use to get a local csv to load properly?

thank you Dennis

import numpy as np

import gmaps
gmaps.configure(api_key="AIyourAPIKeyGoesHere")

m = gmaps.Map()

data = np.loadtxt("/home/dgd/Desktop/text.csv")
heatmap_layer = gmaps.Heatmap(data=data)
m.add_layer(heatmap_layer)
m

error

ValueError: could not convert string to float: b'lat'

import numpy as np
import gmaps

gmaps.configure(api_key="AIyourAPIKeyGoesHere")

m = gmaps.Map()

data = np.loadtxt("/home/dgd/Desktop/text.csv")
heatmap_layer = gmaps.Heatmap(data=data)
m.add_layer(heatmap_layer)
m

error:

ValueError: could not convert string to float: b'lat'

import numpy as np
import gmaps

gmaps.configure(api_key="AIyourAPIKeyGoesHere")

m = gmaps.Map()

data = np.genfromtxt("/home/dgd/Desktop/text.csv", delimiter=',')
heatmap_layer = gmaps.Heatmap(data=data)
m.add_layer(heatmap_layer)
m

error: TraitError: The 'data' trait of a Heatmap instance must be a list, but a value of class 'numpy.ndarray' (i.e. array([[ nan, nan], [ 38., 0.], [ 45., 0.]])) was specified.


import csv
import gmaps

gmaps.configure(api_key="AIyourAPIKeyGoesHere")

m = gmaps.Map()

with open('/home/dgd/Desktop/text.csv', 'rb') as f:
    reader = csv.reader(f)
    data =your_list = map(tuple, reader)

heatmap_layer = gmaps.Heatmap(data=data)
m.add_layer(heatmap_layer)
m

error:

TraitError: The 'data' trait of a Heatmap instance must be a list, but a value of class 'map' (i.e. <map object at 0x7fa4ac122320>) was specified.

reyale commented 8 years ago

Relevant: https://github.com/pbugnion/gmaps/issues/68#issuecomment-230229399

Basically data needs to be like: [(lat,lon),(lat,lon)], so an iterable of tuples.

You could transform:

array([[ nan, nan],
[ 38., 0.],
[ 45., 0.]])) 

which I read as a np.array of np.arrays of size 2 (Nx2 matrix) by going:

data=[(d[0],d[1]) for d in data]

pleabargain commented 8 years ago

@reyale Thank you. I tried variations on your suggestion

mydata = np.genfromtxt("/home/dgd/Desktop/text.csv", delimiter=',')
data = mydata
data =[(d[0],d[1]) for d in data]

heatmap_layer = gmaps.Heatmap(data=data)
m.add_layer(heatmap_layer)
m

but got InvalidPointException: (nan, nan) is not a valid latitude, longitude pair

I'm grateful for any other tips you might have to resolve my goal with gmaps.

thank you Dennis

reyale commented 8 years ago

Dennis,

My guess is that you have a blank or invalid float value in your csv, and that np.genfromtxt fills these values with np.nan. Please verify all data is not nan.

You might try:

http://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html

missing_values : variable, optional The set of strings corresponding to missing data. filling_values : variable, optional The set of values to be used as default when the data are missing.

in np.genfromtxt to automatically fill these values with valid floats.

pbugnion commented 8 years ago

@pleabargain If you can attach the CSV to the issue (or any subset of the CSV that causes the error), we may be able to give more targeted answers.

If you do have nan values in your data, you will have to remove these as a pre-processing step beforehand.

PRs for improved error handling always welcome!

pbugnion commented 8 years ago

Closing as likely not a problem in gmaps directly. That said, we should improve the way we notify the user that their data is invalid.