Loading data from Yahoo! Finance with pandas

2024/10/10 0:27:36

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 weball_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_yahooadjust_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_fromhist_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_yahooreturn _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?

Answer

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
https://en.xdnf.cn/q/69955.html

Related Q&A

Run Multiple Instances of ChromeDriver

Using selenium and python I have several tests that need to run in parallel. To avoid using the same browser I added the parameter of using a specific profile directory and user data (see below). The p…

numpy 2d array max/argmax

I have a numpy matrix:>>> A = np.matrix(1 2 3; 5 1 6; 9 4 2) >>> A matrix([[1, 2, 3],[5, 1, 6],[9, 4, 2]])Id like to get the index of the maximum value in each row along with the valu…

How do I add a python script to the startup registry?

Im trying to make my python script run upon startup but I get the error message windowserror access denied, but I should be able to make programs start upon boot because teamviewer ( a third-party prog…

Python: How can I filter a n-nested dict of dicts by leaf value?

Ive got a dict that looks something like this:d = {Food: {Fruit : {Apples : {Golden Del. : [Yellow],Granny Smith : [Green],Fuji : [Red], },Cherries : [Red],Bananas : [Yellow],Grapes …

contour plot - clabel spacing

I have trouble with matplotlib / pyplot / basemap. I plot contour lines (air pressure) on a map. I use clabel to show the value of the contour lines. But the problem is: the padding between the value a…

Class-based view has no attribute .as_view() error

Im following this tutorial, trying to make an API for my Products table.Heres my .views/API/apitest.py view:from my_app.views.API.serializers import ProductSerializer from my_app.models import Product …

Ordering query result by numeric strings in django (postgres backend)

I have a table with a name (varchar) field that only holds numeric string and I want to order my queries by this field. But name fields are being ordered by alphabetically but I want them to be ordered…

Resolve argparse alias back to the original command

Im using a subparser/subcommand that has an alias. Im using the dest option for the subparser to store the name of the subcommand so I can get it later.Currently if the subcommands name is reallyLongN…

Django: How do I make fields non-editable by default in an inline model formset?

I have an inline model formset, and Id like to make fields non-editable if those fields already have values when the page is loaded. If the user clicks an "Edit" button on that row, it would…

How to store result of an operation (like TOPK) per epoch in keras

I have written a custom layer in keras. in part of this custom layer lets say I have a matrix like this: c = tf.cast(tf.nn.top_k(tf.nn.top_k(n, tf.shape(n)[1])[1][:, ::-1], tf.shape(n)[1])[1][:, ::-1],…