繁体   English   中英

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

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

自2017年5月16日以来,使用Pandas DataReader的“雅虎”方法无法访问Yahoo Finance URL。我还没有测试此修复程序 - 雅虎财务: https//pypi.python.org/pypi/fix-yahoo-finance昨天发布的声明是:“Yahoo! finance已经退役他们的历史数据API”。

编辑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')

请注意,最后2个数据列的顺序是“Adj Close”和“Volume”,即。 不是以前的格式。 为了我的目的,他们只需重置为原始格式:

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

我建议使用Quandl。 我不确定雅虎收购后是否会变得可靠。 在Quandl中,如果你有多个符号,你必须循环。 阅读文档并执行以下操作:

    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')

我已经按照https://pypi.python.org/pypi/fix-yahoo-finance中的步骤执行:$ pip3 install fix_yahoo_finance --upgrade --no-cache-dir并且还升级了pandas_datareader以确保。

“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')

请注意,最后2个数据列的顺序是“Adj Close”和“Volume”,即。 不是以前的格式。 为了我的目的,他们被重置为原始格式:

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

我喜欢user3443068的答案,因为它很简单。

我也建议使用谷歌作为你的来源,因为雅虎实例可能会经历许多已弃用的版本,考虑到公司的发展方向。

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