熊猫不是进口的吗?'NameError: 全局名称 'pandas' 未定义'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31721996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is Pandas not importing? 'NameError: global name 'pandas' is not defined'
提问by Nick Duddy
I'm getting a few errors here but I think it's due to pandas not importing as it's greyed out. If that is the problem, how would I fix this?
我在这里遇到了一些错误,但我认为这是由于Pandas未导入,因为它变灰了。如果这是问题,我将如何解决这个问题?
C:\Anaconda\python.exe C:/Users/nickd/Documents/SKLEARN-STOCKS/stock-mach.py Traceback (most recent call last): File "C:/Users/nickd/Documents/SKLEARN-STOCKS/stock-mach.py", line 38, in Key_Stats() File "C:/Users/nickd/Documents/SKLEARN-STOCKS/stock-mach.py", line 12, in Key_Stats df = pandas.DataFrame(columns = ['Date','Unix','Ticker','DE Ratio']) NameError: global name 'pandas' is not defined
Process finished with exit code 1
C:\Anaconda\python.exe C:/Users/nickd/Documents/SKLEARN-STOCKS/stock-mach.py Traceback(最近一次调用最后一次):文件“C:/Users/nickd/Documents/SKLEARN-STOCKS/stock -mach.py”,第 38 行,在 Key_Stats() 文件“C:/Users/nickd/Documents/SKLEARN-STOCKS/stock-mach.py”,第 12 行,在 Key_Stats 中 df = pandas.DataFrame(columns = [' Date','Unix','Ticker','DE Ratio']) NameError: 全局名称 'pandas' 未定义
进程以退出代码 1 结束
import pandas as pd
import os
import time
from datetime import datetime
#location of the data files
path = 'C:\Users\nickd\Documents\SKLEARN-STOCKS'
#what specific field do you want to grab and in all files in that directory
def Key_Stats(gather="Total Debt/Equity (mrq) "):
statspath = path+'/_KeyStats'
stock_list = [x[0] for x in os.walk(statspath)]
df = pandas.DataFrame(columns = ['Date','Unix','Ticker','DE Ratio'])
for each_dir in stock_list[1:5]:
each_file = os.listdir(each_dir)
ticker = each_dir.split("//")[1]
if len(each_file) >0:
for file in each_file:
date_stamp = datetime.strptime(file, '%Y%m%d%H%M%S.html')
#unix_time = time.mktime(date_stamp.timetuple())
print(date_stamp,unix_time)
full_file_path = each_file+'/'+file
#print(full_file_path)
source = open(full_file_path, 'r').read()
#print(source)
try:
value = float(source.split(gather+':</td><td class="yfnc_tabledata1">')[1].split('</td>')[0])
df = df.append({'Date':date_stamp,'Unix':unix_time,'Ticker':ticker,'DE Ratio':value,}, ignore_index = True)
except Exception as e:
pass
#print(ticker+":",value)
save = gather.replace(' ','').replace(')','').replace('(','').replace('/',''+('.csv')
print(save)
df.to_csv(save)
#time.sleep(15)
Key_Stats()
回答by The6thSense
You have imported it as
您已将其导入为
import pandas as pd
and calling
并打电话
#pandas
df = pandas.DataFrame(columns = ['Date','Unix','Ticker','DE Ratio'])
You could either change
你可以改变
import pandas as pdtoimport pandasordf = pandas.DataFrame(columns = ['Date','Unix','Ticker','DE Ratio'])todf = pd.DataFrame(columns = ['Date','Unix','Ticker','DE Ratio']).
import pandas as pd到import pandas或df = pandas.DataFrame(columns = ['Date','Unix','Ticker','DE Ratio'])到df = pd.DataFrame(columns = ['Date','Unix','Ticker','DE Ratio'])。
edit:
编辑:
error is due to missing )
错误是由于丢失 )
save = gather.replace(' ','').replace(')','').replace('(','').replace('/',''+('.csv'))
回答by Daniel Roseman
You imported pandas as pd. Either refer to it as such throughout, or remove the as pdfrom the import.
您将Pandas导入为pd. 要么在整个过程中如此引用它,要么as pd从导入中删除。
(Plus, never ever everdo except Exception... pass. You'll swallow all sorts of errors and never know what they were. If you aren't going to handle them, don't catch them at all.)
(另外,永远不要做except Exception... pass。你会吞下各种错误,永远不知道它们是什么。如果你不打算处理它们,根本不要抓住它们。)

