[Solved] NameError: name ‘pd’ is not defined when calling a function in custom package


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


and I get

NameError                                 Traceback (most recent call last)
<ipython-input-47-0a062ed9d667> in <module>()
      3 import pandas as pd
----> 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.

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
Enquirer: daco


Solution #1:

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.

Respondent: suvayu

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.