Feeding an Dataframe into an API, getting json not serializable error

Viewed 63

I am trying to get data around how long it takes to drive from place to another. I am using this API https://github.com/traveltime-dev/traveltime-python-sdk and have got it to work when I manually type in the locations and their coordinates as shown below:

locations = [
    {"id": "London center", "coords": {"lat": 51.508930, "lng": -0.131387}},
    {"id": "Hyde Park", "coords": {"lat": 51.508824, "lng": -0.167093}},
    {"id": "ZSL London Zoo", "coords": {"lat": 51.536067, "lng": -0.153596}}
]

arrival_many_to_one = {
"id": "arrive-at many-to-one search example",
"departure_location_ids": ["Hyde Park","ZSL London Zoo"],
"arrival_location_id": "London center",
"transportation": {"type": "public_transport"},
"arrival_time_period": "weekday_morning",
"travel_time": 1900,
"properties": ["travel_time","fares"]
}
arrival_one_to_many = {
"id": "arrive-at one-to-many search example",
"arrival_location_ids": ["Hyde Park","ZSL London Zoo"],
"departure_location_id": "London center",
"transportation": {"type": "public_transport"},
"arrival_time_period": "weekday_morning",
"travel_time": 1900,
"properties": ["travel_time","fares"]
} 

out = ttpy.time_filter_fast(
        locations=locations, arrival_many_to_one=arrival_many_to_one, arrival_one_to_many=arrival_one_to_many)

However, I have a large number of locations which is unreasonable to type out manually. I currently have them, stored in a Dataframe but when I feed that through, where I created "Out" is falls over as the json not serializable. df is the dataframe with my locations and coordinates are stored and I arranged it so that the origin/departure is always the first row and the destinations/arrivals and row two onwards. My original plan was to feed the dataframe into the API and use slicing features to force the first row as the my arrival and then rest of the rows as the departure locations

arrival_one_to_many = {
    "id" : 'KA22 8EH' ,
    "arrival_location_ids": df[2:],
    "departure_location_id":df[:1],
    "transportation": {"type": "driving"},
    "arrival_time_period": "weekday_morning",
    "travel_time": 3000,
    "properties": ["travel_time"]}

I have tried various things such as converting the dataframe to a list, so that that list of locations looks as it does when I run the first of the code above. But I get a mix of the errors json not serializable or it expected "arrival_location_ids"/ "departure_location_ids" to be a string.

Is there a way I can feed all these locations into the API without manually typing everything. Maybe some sort of loop? Note: Unfortunately, this does require an API which I can't share with you, but any ideas to push me in the right direction would be appreciated

0 Answers
Related