使用Python,Pandas和Yahoo Finance API提取股票价格数据时出错

时间:2018-10-31 15:31:55

标签: python-3.x pandas dataframe yahoo-api pandas-datareader

我正在尝试运行一个python脚本,该脚本从Wikipedia页面所定义的标准普尔500指数中提取雅虎财务的价格数据。但是由于某些原因,EVRG代码会引发此错误:

  

回溯(最近通话最近):文件   “ /usr/local/lib/python3.7/site-packages/pandas/core/indexes/base.py”,   第3078行,位于get_loc中       返回self._engine.get_loc(key)文件“ pandas / _libs / index.pyx”,第140行,在pandas._libs.index.IndexEngine.get_loc文件中   第162行中的“ pandas / _libs / index.pyx”   pandas._libs.index.IndexEngine.get_loc文件   “ pandas / _libs / hashtable_class_helper.pxi”,第1492行,在   pandas._libs.hashtable.PyObjectHashTable.get_item文件   “ pandas / _libs / hashtable_class_helper.pxi”,第1500行,在   pandas._libs.hashtable.PyObjectHashTable.get_item KeyError:'Date'

     

在处理上述异常期间,发生了另一个异常:

     

回溯(最近一次通话最近):文件“ ./python-for-finance-6.py”,   第50行,在       get_data_from_yahoo()get_data_from_yahoo中的文件“ ./python-for-finance-6.py”,第45行       df = web.DataReader(ticker.replace(“。”,“-”),“ yahoo”,开始,结束)文件   “ /usr/local/lib/python3.7/site-packages/pandas_datareader/data.py”,   DataReader中的第310行       session = session).read()文件“ /usr/local/lib/python3.7/site-packages/pandas_datareader/base.py”,   第210行,处于读取状态       params = self._get_params(self.symbols))文件“ /usr/local/lib/python3.7/site-packages/pandas_datareader/yahoo/daily.py”,   _read_one_data中的第142行       to_datetime(prices ['Date'],unit ='s')。dt.date)文件“ /usr/local/lib/python3.7/site-packages/pandas/core/frame.py”,行   2688,在获取项中       返回self._getitem_column(key)文件“ /usr/local/lib/python3.7/site-packages/pandas/core/frame.py”,行   2695,位于_getitem_column中       返回self._get_item_cache(key)文件“ /usr/local/lib/python3.7/site-packages/pandas/core/generic.py”,行   2489,位于_get_item_cache中       值= self._data.get(item)文件“ /usr/local/lib/python3.7/site-packages/pandas/core/internals.py”,   4115行,进入       loc = self.items.get_loc(item)文件“ /usr/local/lib/python3.7/site-packages/pandas/core/indexes/base.py”,   第3080行,位于get_loc中       返回self._engine.get_loc(self._maybe_cast_indexer(key))文件“ pandas / _libs / index.pyx”,第140行,在   pandas._libs.index.IndexEngine.get_loc文件   第162行中的“ pandas / _libs / index.pyx”   pandas._libs.index.IndexEngine.get_loc文件   “ pandas / _libs / hashtable_class_helper.pxi”,第1492行,在   pandas._libs.hashtable.PyObjectHashTable.get_item文件   “ pandas / _libs / hashtable_class_helper.pxi”,第1500行,在   pandas._libs.hashtable.PyObjectHashTable.get_item KeyError:'Date'

有人可以帮忙吗?

我的代码是这样的:

import bs4 as bs
import datetime as dt
import os 
import pandas as pd
import pandas_datareader.data as web
import pickle
import requests

# This is a script that will get the tickers from the wikipedia list, 
print them and save it as a pickle file
def save_sp500_tickers():
    resp = requests.get('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
    soup = bs.BeautifulSoup(resp.text, 'lxml')
    table = soup.find('table', {'class': 'wikitable sortable'})
    tickers= []
    for row in table.findAll('tr')[1:]:
        ticker=row.findAll('td')[0].text
        mapping = str.maketrans(".","-")
        ticker = ticker.translate(mapping)
        if ticker.find("_"):
            ticker = ticker.replace("_","-")
        tickers.append(ticker)
    with open("sp500tickers.pickle", "wb") as f:
        pickle.dump(tickers, f)
    print(tickers)

    return tickers

save_sp500_tickers()

def get_data_from_yahoo(reload_sp500 = False):
    if reload_sp500:
        tickers = save_sp500_tickers()
    else: 
        with open("sp500tickers.pickle", "rb") as f:
            tickers = pickle.load(f)
    if not os.path.exists('stock_dfs'):
        os.makedirs('stock_dfs')

    start = dt.datetime(2000,1,1)
    end = dt.datetime(2017,12,31)

    for ticker in tickers:
        print(ticker)
        if not os.path.exists('stock_dfs/{}.csv'.format(ticker)):
            df = web.DataReader(ticker.replace(".","-"), 'yahoo', start, end)
            df.to_csv('stock_dfs/{}.csv'.format(ticker))
        else:
            print('Already have {}'.format(ticker))

get_data_from_yahoo()

0 个答案:

没有答案