gpt4 book ai didi

python - 从 Yahoo! 加载数据用 Pandas 理财

转载 作者:太空狗 更新时间:2023-10-29 21:37:18 25 4
gpt4 key购买 nike

我正在阅读 Wes McKinney 的书 Python For Data Analysis 和 Correlation and Covariance 下的第 139 页,当我尝试运行他的代码以从 Yahoo!金融。

这是我正在运行的:

#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 返回“找不到页面”。

从网络轮询数据时,最好将调用包装在 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

关于python - 从 Yahoo! 加载数据用 Pandas 理财,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23642194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com