从Yahoo!加载数据 pandas 金融 [英] Loading data from Yahoo! Finance with pandas

查看:68
本文介绍了从Yahoo!加载数据 pandas 金融的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Wes McKinney的《 Python for Data Analysis》一书,在第139页的Correlation and Covariance下,尝试运行他的代码从Yahoo!获取数据时遇到错误.金融.

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.

这是我正在跑步的东西:

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()})

这是我遇到的错误:

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

有什么问题的想法吗?

推荐答案

正如Karl指出的那样,股票代码已更改,这意味着Yahoo返回了找不到页面".

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

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

这篇关于从Yahoo!加载数据 pandas 金融的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆