如何修复pyhton中的pandas datareader错误?

时间:2020-03-16 19:31:04

标签: python pandas datetime pandas-datareader

当我尝试运行该函数时,它给了我这个错误,而我找不到任何有关它的信息。 我尝试遵循在线教程,但无法使我的代码正常工作。 完整代码:https://pastebin.com/0MALELM4

代码

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(2015, 1, 1)
    end = dt.datetime.now()
    for ticker in tickers:
        if not os.path.exists('stock_dfs/{}.csv'.format(ticker)):
            df = web.DataReader(ticker, 'stooq', start, end)
            df.reset_index(inplace=True)
            df.set_index("Date", inplace=True)
            df = df.drop("Symbol", axis=1)
            df.to_csv('stock_dfs/{}.csv'.format(ticker))
        else:
            print('Already have {}'.format(ticker))

错误:

    Traceback (most recent call last):
  File "D:\pyhtonlearning\py4finance.py", line 102, in <module>
    get_data_from_yahoo()
  File "D:\pyhtonlearning\py4finance.py", line 45, in get_data_from_yahoo
    df.set_index("Date", inplace=True)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\pandas\core\frame.py", line 4303, in set_index
    raise KeyError(f"None of {missing} are in the columns")
KeyError: "None of ['Date'] are in the columns"
[Finished in 2.1s with exit code 1]
[shell_cmd: python -u "D:\pyhtonlearning\py4finance.py"]
[dir: D:\pyhtonlearning]
[path: C:\Program Files (x86)\Python38-32\Scripts\;C:\Program Files (x86)\Python38-32\;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Users\Balazs\AppData\Local\Microsoft\WindowsApps;]

0 个答案:

没有答案