pydata/pandas — Issue #826: Try to workaround Yahoo! finance 404 errors, better error messages
Author: wesm
Created: 2012-02-24 20:18:45+00:00
State: open
Labels: Bug, prio-high

randomly get this crap

```

import pandas
import pandas.io.data as pd

def get_close(ticker, start, end):

stock = pd.DataReader(ticker, 'yahoo', start, end)
adj_close = stock['Adj Close']

return adj_close[-1]

start = datetime.datetime(2010,1,1)
end = datetime.datetime(2012,1,24)
ticker = ['TLT', 'IEF', 'IYR', 'XLY', 'XLK', 'XLU', 'XLV', 'XLP', 'XLI', 'XLF', 'XLB', 'XLE', 'SPY', 'EFA', 'EEM', 'EWJ', 'IWM', 'QQQ', 'LQD', 'JNK']

for t in ticker:
result = get_close(t, start, end)
print t,result
## -- End pasted text --
TLT 116.17
IEF 104.15
IYR 59.52
XLY 41.25
XLK 26.87
XLU 34.47
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
/home/wesm/code/pandas/ in ()
17
18 for t in ticker:
---> 19 result = get_close(t, start, end)
20 print t,result

/home/wesm/code/pandas/ in get_close(ticker, start, end)
6 def get_close(ticker, start, end):
7
----> 8 stock = pd.DataReader(ticker, 'yahoo', start, end)
9 adj_close = stock['Adj Close']
10

/home/wesm/code/pandas/pandas/io/data.pyc in DataReader(name, data_source, start, end)
51
52 if(data_source == "yahoo"):
---> 53 return get_data_yahoo(name=name, start=start, end=end)
54 elif(data_source == "fred"):
55 return get_data_fred(name=name, start=start, end=end)

/home/wesm/code/pandas/pandas/io/data.pyc in get_data_yahoo(name, start, end)
133
134 lines = urllib.urlopen(url).read()
--> 135 return read_csv(StringIO(lines), index_col=0, parse_dates=True)[::-1]
136
137 def get_data_fred(name=None, start=dt.datetime(2010, 1, 1),

/home/wesm/code/pandas/pandas/io/parsers.pyc in read_csv(filepath_or_buffer, sep, header, index_col, names, skiprows, na_values, parse_dates, date_parser, nrows, iterator, chunksize, skip_footer, converters, verbose, delimiter, encoding)
125 return parser
126
--> 127 return parser.get_chunk()
128
129 @Appender(_read_table_doc)

/home/wesm/code/pandas/pandas/io/parsers.pyc in get_chunk(self, rows)
483
484 if len(self.columns) != len(zipped_content):
--> 485 raise Exception('wrong number of columns')
486
487 data = dict((k, v) for k, v in izip(self.columns, zipped_content))

Exception: wrong number of columns

In [8]: %debug
> /home/wesm/code/pandas/pandas/io/parsers.py(485)get_chunk()
484 if len(self.columns) != len(zipped_content):
--> 485 raise Exception('wrong number of columns')
486

ipdb> u
> /home/wesm/code/pandas/pandas/io/parsers.py(127)read_csv()
126
--> 127 return parser.get_chunk()
128

ipdb>
> /home/wesm/code/pandas/pandas/io/data.py(135)get_data_yahoo()
134 lines = urllib.urlopen(url).read()
--> 135 return read_csv(StringIO(lines), index_col=0, parse_dates=True)[::-1]
136

ipdb> lines
'\n

Yahoo! - 404 Not Found

\n

\n

Yahoo!\n - Help

\n

Sorry, the page you requested was not found.

\n

Please check the URL for proper spelling and capitalization. If\nyou\'re having trouble locating a destination on Yahoo!, try visiting the\nYahoo! home\npage or look through a list of Yahoo!\'s\nonline services. Also, you may find what you\'re looking for\nif you try searching below.

\n

\n
Search the Web
\n\n\nadvanced search | most popular\n

\n

Please try Yahoo!\nHelp Central if you need more assistance.

\n

Copyright © 2012 Yahoo! Inc.\nAll rights reserved. Privacy\nPolicy - Terms\nof Service

\n

\n'
ipdb>
```