pandas 从 Yahoo! 加载数据 熊猫理财

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23642194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:02:58  来源:igfitidea点击:

Loading data from Yahoo! Finance with pandas

pythonpandas

提问by panterasBox

I am working my way through Wes McKinney's book Python For Data Analysis and on page 139 under Correlation and Covariance, I am getting an error when I try to run his code to obtain data from Yahoo! Finance.

我正在阅读 Wes McKinney 的书 Python For Data Analysis 和第 139 页的“相关性和协方差”下的内容,当我尝试运行他的代码以从 Yahoo! 获取数据时遇到错误!金融。

Here is what I am running:

这是我正在运行的内容:

#CORRELATION AND COVARIANCE
import pandas.io.data as web

all_data = {}
for ticker in ['AAPL', 'IBM', 'MSFT', 'GOOG']:
    all_data[ticker] = web.get_data_yahoo(ticker, '1/1/2003', '1/1/2013')

price = DataFrame({tic: data['Adj Close']
                   for tic, data in all_data.iteritems()})
volume = DataFrame({tic: data['Volume']
                    for tic, data in all_data.iteritems()})

Here is the error I am getting:

这是我得到的错误:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "C:\Users\eMachine\WinPython-64bit-2.7.5.3\python-2.7.5.amd64\lib\site-packages\pandas\io\data.py", line 390, in get_data_yahoo
    adjust_price, ret_index, chunksize, 'yahoo', name)
  File "C:\Users\eMachine\WinPython-64bit-2.7.5.3\python-2.7.5.amd64\lib\site-packages\pandas\io\data.py", line 336, in _get_data_from
    hist_data = src_fn(symbols, start, end, retry_count, pause)
  File "C:\Users\eMachine\WinPython-64bit-2.7.5.3\python-2.7.5.amd64\lib\site-packages\pandas\io\data.py", line 190, in _get_hist_yahoo
    return _retry_read_url(url, retry_count, pause, 'Yahoo!')
  File "C:\Users\eMachine\WinPython-64bit-2.7.5.3\python-2.7.5.amd64\lib\site-packages\pandas\io\data.py", line 169, in _retry_read_url
    "return a 200 for url %r" % (retry_count, name, url))
IOError: after 3 tries, Yahoo! did not return a 200 for url 'http://ichart.yahoo.com/table.csv?s=GOOG&a=0&b=1&c=2000&d=0&e=1&f=2010&g=d&ignore=.csv'
>>> ... >>> >>> ... >>> 

Any idea on what the problem is?

知道问题是什么吗?

采纳答案by acutesoftware

As Karl pointed out, the ticker had changed meaning Yahoo returns a 'page not found'.

正如卡尔指出的那样,股票代码发生了变化,这意味着雅虎返回“找不到页面”。

When polling data from the web, it is a good idea to wrap the call in a try except

从 Web 轮询数据时,最好将调用包装在 try 中,除了

all_data = {}
for ticker in ['AAPL', 'IBM', 'MSFT', 'GOOG']:
    try:
        all_data[ticker] = web.get_data_yahoo(ticker, '1/1/2003', '1/1/2013')
        price = DataFrame({tic: data['Adj Close']
                    for tic, data in all_data.iteritems()})
        volume = DataFrame({tic: data['Volume']
                    for tic, data in all_data.iteritems()})
    except:
        print "Cant find ", ticker

回答by andrewpederson

Had the same problem and changing 'GOOG' to 'GOOGL' seems to work, once you've followed these instructions to switch from pandas.io.data to pandas_datareader.data.

遇到了同样的问题并且将“GOOG”更改为“GOOGL”似乎可行,一旦您按照这些说明从pandas.io.data 切换到pandas_datareader.data。

http://pandas-datareader.readthedocs.org/en/latest/remote_data.html#yahoo-finance

http://pandas-datareader.readthedocs.org/en/latest/remote_data.html#yahoo-finance

回答by Steve Eckhart

As of 6/1/17, I pieced the following together from this page and a couple of others:

截至 2017 年 6 月 1 日,我从这个页面和其他几个页面拼凑了以下内容:

from pandas_datareader import data as web
# import pandas.io.data as web
import fix_yahoo_finance
import datetime

start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2017, 6, 1)

all_data={}
for ticker in ['AAPL', 'IBM', 'MSFT', 'GOOGL']:
    all_data[ticker] = web.get_data_yahoo(ticker, start, end)

price = DataFrame({tic: data['Adj Close'] 
                   for tic, data in all_data.iteritems()})
volume = DataFrame({tic: data['Volume']
                     for tic, data in all_data.iteritems()})

回答by sam

Im using the code snippet below to load yahoo finance data.

我使用下面的代码片段来加载雅虎财务数据。

import pandas_datareader as pdr
from datetime import datetime
from pandas import DataFrame as df

def get_data(selection, sdate, edate):
    data = pdr.get_data_yahoo(symbols=selection, start=sdate, end=edate)
    data =  df(data['Adj Close'])
    return data

start_date = datetime(2017, 1, 1)
end_date = datetime(2019,4,28)

selected = [ 'TD.TO', 'AC.TO', 'BNS.TO', 'ENB.TO', 'MFC.TO','RY.TO','BCE.TO']

print(get_data(selected, start_date, end_date).head(1))

https://repl.it/repls/DevotedBetterAlgorithms

https://repl.it/repls/DevotedBetterAlgorithms