我试图使用pandas下载所有斯德哥尔摩大型股票的历史股票数据。它工作正常,但对于一些股票,它没有。
import pandas_datareader.data as pdr
import datetime
import csv
with open('stockholm_largecap.csv', 'rb') as f:
reader = csv.reader(f)
stockholmLargeCap = list(reader)
start = datetime.datetime(1970, 1, 1)
end = datetime.datetime.today();
stockData = {}
for symbol in stockholmLargeCap:
f = pdr.DataReader(symbol, 'yahoo', start, end)
print f
stockholm_largecap.csv按字母顺序包含所有股票,但一旦我获得某些股票(例如BETS-B.ST):符号警告:未能阅读符号:' BETS-B.ST&#39 ;用NaN代替。并且脚本终止。是否有某种方法可以继续该计划,忽略错误以及可能导致某些股票无效的原因?
raise RemoteDataError(msg.format(self.__class__.__name__))
pandas_datareader._utils.RemoteDataError: No data fetched using 'YahooDailyReader'
答案 0 :(得分:1)
使用try
和except
import pandas_datareader.data as pdr
for symbol in ['SPY', 'holla']:
try:
f = pdr.DataReader(symbol, 'yahoo', "2001-01-01", "2010-01-01")
print f.head(5)
except:
print ('did not find: '+symbol)
Open High Low Close Volume Adj Close
Date
2001-01-02 132.0000 132.1562 127.5625 128.8125 8737500 95.2724
2001-01-03 128.3125 136.0000 127.6562 135.0000 19431600 99.8488
2001-01-04 134.9375 135.4687 133.0000 133.5468 9219000 98.7740
2001-01-05 133.4687 133.6250 129.1875 129.1875 12911400 95.5497
2001-01-08 129.8750 130.1875 127.6875 130.1875 6625300 96.2893
did not find: holla
答案 1 :(得分:0)
当我尝试从清单中获取库存时,我遇到了同样的问题,我使用了异常处理块,尽管出现了符号警告viz,该异常处理块仍继续执行代码。[SymbolWarning:无法读取符号:'AXZZW',用NaN代替。 warnings.warn(msg.format(sym),SymbolWarning)]
from pandas_datareader._utils import RemoteDataError
from pandas_datareader.data import Options
try:
df1 = web.DataReader(rows[i],'yahoo',"2001-01-01","2010-01-01")
print("Downloading",i,"/",len(rows),"............")
print(df1)
except KeyError:
print("Data not found at Ticker %s"%i)
continue
except RemoteDataError:
print("Data not found at Ticker %s"%i)
continue
print("Success!")
希望这对您也有用!