3

Context

I'm learning python for Data Science and I'm using Foursquare API to explore venues near a coordinate. It returns a JSON file, so I created a function to return a Pandas DataFrame from Foursquare results using 'foursquare' package (github.com/mLewisLogic/foursquare) and then extract append the data to a DataFrame.

The function works in my Jupyter Notebook (you can check the function here https://github.com/dacog/foursquare_api_tools/blob/master/foursquare_api_tools/foursquare_api_tools.py), and I though about making it easier for others and tried to create a package which could be installed using pip directly from github. I successfully created a package and published it to github to test it, but when I'm trying to use the function it returns

NameError: name 'pd' is not defined

Steps to try the package

!pip install git+https://github.com/dacog/foursquare_api_tools.git#egg=foursquare_api_tools

# @hidden_cell
CLIENT_ID = 'Secret' # your Foursquare ID
CLIENT_SECRET = 'Secret' # your Foursquare Secret
VERSION = '20180605' # Foursquare API version

from foursquare_api_tools import foursquare_api_tools as ft

ft.venues_explore(client,lat='40.7233',lng='-74.0030',limit=100)

and I get

NameError                                 Traceback (most recent call last)
<ipython-input-47-0a062ed9d667> in <module>()
      3 import pandas as pd
      4 
----> 5 ft.venues_explore(client,lat='40.7233',lng='-74.0030',limit=100)

/opt/conda/envs/DSX-Python35/lib/python3.5/site-packages/foursquare_api_tools/foursquare_api_tools.py in venues_explore(client, lat, lng, limit)
      3     This returns a pandas dataframe with name, city ,country, lat, long, postal code, address and main category as columns'''
      4         # creata a dataframe
----> 5         df_a = pd.DataFrame(columns=['Name', 'City', 'Latitude','Longitude','Category','Postal Code', 'Address'])
      6         ll=lat+','+lng
      7         #get venues using client https://github.com/mLewisLogic/foursquare

NameError: name 'pd' is not defined

I tried import pandas as pd in the main notebook, inside the function, in __init__.py always with the same result.

You can check the code at https://github.com/dacog/foursquare_api_tools

It's the first time I'm creating a package and pretty new to python, so any help will be greatly appreciated.

UPDATES Pandas is working fine in the environment when I'm doing the tests. enter image description here

The installed Python versions are:

!which python --> /home/jupyterlab/conda/bin/python

!whereis python
/usr/bin/python /usr/bin/python2.7 /usr/lib/python2.7 /etc/python /etc/python2.7
/usr/local/lib/python2.7 /usr/share/python
/home/jupyterlab/conda/bin/python /home/jupyterlab/conda/bin/python3.6
/home/jupyterlab/conda/bin/python3.6-config /home/jupyterlab/conda/bin/python3.6m /home/jupyterlab/conda/bin/python3.6m-config /usr/share/man/man1/python.1.gz
smci
  • 32,567
  • 20
  • 113
  • 146
daco
  • 600
  • 1
  • 4
  • 14
  • 2
    Have you pip installed pandas? It seems the module you installed didn't stall its dependency. – mattsap Feb 15 '19 at 18:14
  • Hi. Pandas is already installed. I also tried in several environments just in case (Local Anaconda, IBM DataPlatform, labs.cognitiveclass.ai). I always get the same error. Pandas work ok outside the package. If I use the function directly in the notebook (without importing it as a package) it works. – daco Feb 15 '19 at 18:17
  • 1
    I'm wondering if you have multiple python distributions and when you installed pandas, it's using a different distribution. – mattsap Feb 15 '19 at 18:20
  • As @mattsap says, you probably have multiple Python distributions on your path (and/or your path is not what you think it is), and are picking up a different python distribution. Type `which python` and `whereis python` and **post us the results here**, then check if pandas is installed in that distribution. – smci Feb 15 '19 at 18:22
  • @smci i tried what you say. Here is the output !which python --> /home/jupyterlab/conda/bin/python !whereis python python: /usr/bin/python /usr/bin/python2.7 /usr/lib/python2.7 /etc/python /etc/python2.7 /usr/local/lib/python2.7 /usr/share/python /home/jupyterlab/conda/bin/python /home/jupyterlab/conda/bin/python3.6 /home/jupyterlab/conda/bin/python3.6-config /home/jupyterlab/conda/bin/python3.6m /home/jupyterlab/conda/bin/python3.6m-config /usr/share/man/man1/python.1.gz – daco Feb 15 '19 at 18:24
  • @smci I updated the comment. I pressed enter without shift and sent the answer. My mistake. Now it has the results. – daco Feb 15 '19 at 18:27
  • Ok so your path is wrong, you're picking up the MacOS internal Python install `/usr/bin/python` (2.7) ahead of your Anaconda distribution `/home/jupyterlab/conda/bin/python` (3.6). Edit your path to put Anaconda path ahead of the internal one, close all your shells, logout, login again, and you will pick up the right version of Python. Also, please post your path in the question above (since it's the cause of the problem), not in the comments. – smci Feb 15 '19 at 18:29
  • Possible duplicate of [Running Jupyter with multiple Python and IPython paths](https://stackoverflow.com/questions/39007571/running-jupyter-with-multiple-python-and-ipython-paths) – smci Feb 15 '19 at 18:34
  • @smci thank you for your answers! I tried what @ suvayu suggested and it worked! You can ckech it here https://gist.github.com/e26e21df3b93860e75fc374be89a1a53 – daco Feb 15 '19 at 18:43
  • That's not a good solution. How do you know you aren't installing pandas into the wrong distribution, i.e. the MacOS internal Python install `/usr/bin/python` (2.7) that's earlier on your path? – smci Feb 15 '19 at 18:46

2 Answers2

5

You are missing a import pandas as pd statement in foursquare_api_tools.py. Just add that line at the top of that file, and you should be good to go.

The clue is in the error: NameError, on line 5 where you call pd.DataFrame, because there is no import statement, Python does not know what the "name" pd means.

suvayu
  • 4,271
  • 2
  • 29
  • 35
  • Thanks! It worked :) you can check the notebook here https://gist.github.com/e26e21df3b93860e75fc374be89a1a53 – daco Feb 15 '19 at 18:44
0

In addition to "import pandas as pd" as seaborn to your libraries, use this:

Import pandas as pd                                                                      
Import seaborn as sns

Sns.set()

This should work in Jupyter notebook

4b0
  • 21,981
  • 30
  • 95
  • 142
Gregre
  • 1
  • 2