微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

pandas_datareader.get_data_yahoo 上的 KeyError

如何解决pandas_datareader.get_data_yahoo 上的 KeyError

我正在尝试使用 pandas_datareader 的 .get_data_yahoo 函数下载数百只股票的数据。为了加速这个过程,我想在 python 的 concurrent.futures 中使用多线程。您可以在下面的代码窗口中看到我的代码的精简版本,试图下载包含在德国 DAX 中的股票。

from pandas_datareader import data as pdr
from pytickersymbols import PyTickerSymbols
import concurrent.futures
import yfinance as yf
import datetime
import os
from time import sleep
yf.pdr_override()


def download_stockdata(ticker):
    print(f"Downloading {ticker} \n")
    df = pdr.get_data_yahoo(ticker,datetime.datetime.Now() - datetime.timedelta(days=365),datetime.date.today())
    print(f"{ticker} downloaded \n")
    return df

if __name__ == '__main__':

    tickers = []
    index_to_scan = "DAX"
    for element in list(PyTickerSymbols().get_stocks_by_index(index_to_scan)):
        if element["symbols"]:
            tickers.append(element.get("symbols")[0].get("yahoo"))

    print(f"Symbols in {index_to_scan}: {tickers} \n")                        

    print("Starting multi thread download")
    
    futures = []
        
    with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
        for ticker in tickers:
            future = executor.submit(download_stockdata,ticker)
            futures.append(future)
        
        futures,_ = concurrent.futures.wait(futures)
        for future in futures:
            print(future.result())

运行此代码时出现以下错误

KeyError                                  Traceback (most recent call last)
<ipython-input-1-2e4c65895072> in <module>
     36         futures,_ = concurrent.futures.wait(futures)
     37         for future in futures:
---> 38             print(future.result())

~\anaconda3\envs\Trading\lib\concurrent\futures\_base.py in result(self,timeout)
    430                 raise CancelledError()
    431             elif self._state == FINISHED:
--> 432                 return self.__get_result()
    433 
    434             self._condition.wait(timeout)

~\anaconda3\envs\Trading\lib\concurrent\futures\_base.py in __get_result(self)
    386     def __get_result(self):
    387         if self._exception:
--> 388             raise self._exception
    389         else:
    390             return self._result

~\anaconda3\envs\Trading\lib\concurrent\futures\thread.py in run(self)
     55 
     56         try:
---> 57             result = self.fn(*self.args,**self.kwargs)
     58         except BaseException as exc:
     59             self.future.set_exception(exc)

<ipython-input-1-2e4c65895072> in download_stockdata(ticker)
     11 def download_stockdata(ticker):
     12     print(f"Downloading {ticker} \n")
---> 13     df = pdr.get_data_yahoo(ticker,datetime.date.today())
     14     print(f"{ticker} downloaded \n")
     15     return df

~\anaconda3\envs\Trading\lib\site-packages\yfinance\multi.py in download(tickers,start,end,actions,threads,group_by,auto_adjust,back_adjust,progress,period,interval,prepost,proxy,rounding,**kwargs)
    117 
    118     if len(tickers) == 1:
--> 119         return shared._DFS[tickers[0]]
    120 
    121     try:

KeyError: 'BMW.F'

我尝试了不同的多线程方法,例如 threading.Thread()、multiprocessing.pool 中的 ThreadPool 和 concurrent.futures。所有方法都会导致 KeyError 的键不同,但每次运行都会有所不同。从这一点开始,我不再有如何处理错误的想法。有人可以帮我解决 KeyError 问题吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。