NameError:在自定义包中调用函数时未定义名称“pd" [英] NameError: name 'pd' is not defined when calling a function in custom package

查看:114
本文介绍了NameError:在自定义包中调用函数时未定义名称“pd"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我正在学习用于数据科学的 Python,并且我正在使用 Foursquare API 来探索坐标附近的场地.它返回一个 JSON 文件,因此我创建了一个函数来使用foursquare"包 (github.com/mLewisLogic/foursquare) 从 Foursquare 结果返回 Pandas DataFrame,然后将数据提取附加到 DataFrame.

该函数在我的 Jupyter Notebook 中有效(您可以在此处查看该函数

已安装的 Python 版本为:

!which python -->/home/jupyterlab/conda/bin/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

解决方案

您在 foursquare_api_tools.py 中缺少 import pandas as pd 语句.只需在该文件的顶部添加该行,您就可以开始使用了.

线索在错误中:NameError,在你调用pd.DataFrame的第5行,因为没有import语句,Python不知道"name" pd 的意思.

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.

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

解决方案

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.

这篇关于NameError:在自定义包中调用函数时未定义名称“pd"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆