Hush
Hush

Reputation: 1

Python - pandas, chain indexing error

I have PyCharm and Python 3.5 running. I am try to get (source, destination) from the csv source, and store responses to time, distance columns.

I am trying to replicate this code: http://www.analyticsvidhya.com/blog/2015/03/hacking-google-maps-create-distance-features-model-applications/

However if I run this code as is, it doesn't work.

Code stops running because of chain indexing I think... I can upload csv if desired.

Thanks for your help

INPUT: cities.csv

error lines:

cities['distance'][i] = finddist(source, destination)
cities['time'][i] = findtime(source, destination)

Code:

from googlemaps import Client
from datetime import datetime


def finddist(source, destination):
    gmaps = Client(key='mykey')
    now = datetime.now()
    directions_result = gmaps.directions(source, destination, mode="driving", departure_time=now)
    for map1 in directions_result:
        overall_stats = map1['legs']
        for dimensions in overall_stats:
            distance = dimensions['distance']
            return [distance['text']]

def findtime(source, destination):
    gmaps = Client(key='mykey')
    now = datetime.now()
    directions_result = gmaps.directions(source, destination, mode="driving", departure_time=now)
    for map1 in directions_result:
        overall_stats = map1['legs']
        for dimensions in overall_stats:
            duration = dimensions['duration']
            return [duration['text']]

import numpy as np
import pandas as pd
import pylab as pl
import os
os.chdir("/users/merterten/documents")
cities = pd.read_csv("cities.csv")

cities["distance"] = 0
cities["time"] = 0
for i in range(0, 8):
    source = cities['Source'][i]
    destination = cities['Destination'][i]
    cities['distance'][i] = finddist(source, destination)
    cities['time'][i] = findtime(source, destination)

Error:

/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 /Users/merterten/PycharmProjects/untitled/maps/__init__.py
/Users/merterten/PycharmProjects/untitled/maps/__init__.py:37: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  cities['distance'][i] = finddist(source, destination)
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/indexing.py:132: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)
/Users/merterten/PycharmProjects/untitled/maps/__init__.py:38: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  cities['time'][i] = findtime(source, destination)

Process finished with exit code 0

Upvotes: 0

Views: 193

Answers (1)

hilberts_drinking_problem
hilberts_drinking_problem

Reputation: 11602

This is not strictly speaking an error - process finished with exit code 0 means that the program terminated successfully.

At the same time, you are not writing any files or issuing any print statements. Presumably you want to record your results somehow, e.g. put cities.to_csv('results.csv') in the end.

The rest of the log you are seeing are pandas warnings, not errors.

Upvotes: 1

Related Questions