简体   繁体   English

Yahoo Finance API / URL无法正常工作:Pandas DataReader的Python修复程序

[英]Yahoo Finance API / URL not working: Python fix for Pandas DataReader

Yahoo Finance URL has not been accessible using the Pandas DataReader's "yahoo" method since 16 May 2017. I have yet to test this fix-yahoo-finance: https://pypi.python.org/pypi/fix-yahoo-finance that was posted just yesterday, with the statement: "Yahoo! finance has decommissioned their historical data API". 自2017年5月16日以来,使用Pandas DataReader的“雅虎”方法无法访问Yahoo Finance URL。我还没有测试此修复程序 - 雅虎财务: https//pypi.python.org/pypi/fix-yahoo-finance昨天发布的声明是:“Yahoo! finance已经退役他们的历史数据API”。

EDIT August 2, 2017: I have since followed the steps in https://pypi.python.org/pypi/fix-yahoo-finance to: $ pip3 install fix_yahoo_finance --upgrade --no-cache-dir, upgraded pandas_datareader to work with "fix-yahoo-finance 0.0.6", and amended codes: 编辑2017年8月2日:我已经按照https://pypi.python.org/pypi/fix-yahoo-finance中的步骤执行:$ pip3安装fix_yahoo_finance --upgrade --no-cache-dir,将pandas_datareader升级为使用“fix-yahoo-finance 0.0.6”和修改后的代码:

from pandas_datareader import data as pdr
import fix_yahoo_finance

data = pdr.get_data_yahoo('AAPL', start='2017-04-23', end='2017-05-24')

Note that the order of the last 2 data columns are 'Adj Close' and 'Volume' ie. 请注意,最后2个数据列的顺序是“Adj Close”和“Volume”,即。 not the previous format. 不是以前的格式。 For my purpose, they are simply reset to the original format: 为了我的目的,他们只需重置为原始格式:

cols = ['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Close']
data.reindex(columns=cols)

I would recommend using Quandl. 我建议使用Quandl。 I'm not sure if Yahoo is going to be reliable after their acquisition. 我不确定雅虎收购后是否会变得可靠。 In Quandl if you have multiple symbols you have to loop. 在Quandl中,如果你有多个符号,你必须循环。 Read the docs and do something like this: 阅读文档并执行以下操作:

    import quandl as qdl
    start_date = '2016-01-01'
    end_date = '2017-05-22'
    for symbol in symbols:

        quandldata = qdl.get_table("WIKI/PRICES",qopts={"columns":["date", "adj_close"]},
        ticker=symbol, date = {'gte': start_date,'lte' : end_date})
        # specify that the quandldata df has index col = 'date'
        quandldata = quandldata.set_index(["date"], drop=True)
        # rename col adj close to the respective symbol to prevent clash w/ same name for all cols
        quandldata = quandldata.rename(columns={'adj_close': symbol})
        df = df.join(quandldata) 
import pandas_datareader.data as pdweb
from pandas_datareader import data as pdr
import fix_yahoo_finance # must pip install first 
data = pdr.get_data_yahoo('SPY','2017-05-20','2017-05-23')
data = pdr.get_data_yahoo(['SPY','QQQ'],'2017-05-01','2017-05-23', as_panel=False,group_by = 'ticker')

I have followed the steps in https://pypi.python.org/pypi/fix-yahoo-finance to: $ pip3 install fix_yahoo_finance --upgrade --no-cache-dir and also upgraded pandas_datareader to be sure. 我已经按照https://pypi.python.org/pypi/fix-yahoo-finance中的步骤执行:$ pip3 install fix_yahoo_finance --upgrade --no-cache-dir并且还升级了pandas_datareader以确保。

The "fix-yahoo-finance 0.0.6" worked well, for example BHP.AX: “fix-yahoo-finance 0.0.6”运作良好,例如BHP.AX:

from pandas_datareader import data as pdr
import fix_yahoo_finance

data = pdr.get_data_yahoo('BHP.AX', start='2017-04-23', end='2017-05-24')

Note that the order of the last 2 data columns are 'Adj Close' and 'Volume' ie. 请注意,最后2个数据列的顺序是“Adj Close”和“Volume”,即。 not the previous format. 不是以前的格式。 For my purpose, they are reset to the original format: 为了我的目的,他们被重置为原始格式:

cols = ['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Close']
data.reindex(columns=cols)

I like user3443068 answer as it's simple. 我喜欢user3443068的答案,因为它很简单。

I also would suggest looking at using Google as your source as the yahoo instance is probably going to go through many deprecated versions given where the company's going. 我也建议使用谷歌作为你的来源,因为雅虎实例可能会经历许多已弃用的版本,考虑到公司的发展方向。

def get_ret(tickers_ls, start_dt, end_dt):
        #create dataframe
        df_ret=pd.DataFrame() 

        #get prices for all tickers 
        for tk in tickers:
            p = wb.DataReader(tk, "google", start_date, end_date).Close
            df_ret_tmp = p.to_frame()['Close'].reset_index()
            df_ret_tmp['Ticker']=tk
        ## append
            df_ret=df_ret.append(df_ret_tmp) 

        #pivot and get into single dataframe
        pivoted = df_ret.pivot(index='Date', columns='Ticker')
        pivoted.columns = pivoted.columns.droplevel(0)

        return pivoted

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM