Skip to content

Commit 1cb3bb7

Browse files
Merge pull request #1 from pydata/interval
ENH add interval kwarg to get_data_yahoo
2 parents 33a7dfa + 9b641e2 commit 1cb3bb7

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

pandas_datareader/data.py

+17-12
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def _retry_read_url(url, retry_count, pause, name):
188188
_HISTORICAL_YAHOO_URL = 'http://ichart.finance.yahoo.com/table.csv?'
189189

190190

191-
def _get_hist_yahoo(sym, start, end, retry_count, pause):
191+
def _get_hist_yahoo(sym, start, end, interval, retry_count, pause):
192192
"""
193193
Get historical data for the given name from yahoo.
194194
Date format is datetime
@@ -203,15 +203,15 @@ def _get_hist_yahoo(sym, start, end, retry_count, pause):
203203
'&d=%s' % (end.month - 1) +
204204
'&e=%s' % end.day +
205205
'&f=%s' % end.year +
206-
'&g=d' +
206+
'&g=%s' % interval +
207207
'&ignore=.csv')
208208
return _retry_read_url(url, retry_count, pause, 'Yahoo!')
209209

210210

211211
_HISTORICAL_GOOGLE_URL = 'http://www.google.com/finance/historical?'
212212

213213

214-
def _get_hist_google(sym, start, end, retry_count, pause):
214+
def _get_hist_google(sym, start, end, interval, retry_count, pause):
215215
"""
216216
Get historical data for the given name from google.
217217
Date format is datetime
@@ -322,14 +322,14 @@ def get_components_yahoo(idx_sym):
322322
return idx_df
323323

324324

325-
def _dl_mult_symbols(symbols, start, end, chunksize, retry_count, pause,
325+
def _dl_mult_symbols(symbols, start, end, interval, chunksize, retry_count, pause,
326326
method):
327327
stocks = {}
328328
failed = []
329329
for sym_group in _in_chunks(symbols, chunksize):
330330
for sym in sym_group:
331331
try:
332-
stocks[sym] = method(sym, start, end, retry_count, pause)
332+
stocks[sym] = method(sym, start, end, interval, retry_count, pause)
333333
except IOError:
334334
warnings.warn('Failed to read symbol: {0!r}, replacing with '
335335
'NaN.'.format(sym), SymbolWarning)
@@ -351,20 +351,20 @@ def _dl_mult_symbols(symbols, start, end, chunksize, retry_count, pause,
351351
_source_functions = {'google': _get_hist_google, 'yahoo': _get_hist_yahoo}
352352

353353

354-
def _get_data_from(symbols, start, end, retry_count, pause, adjust_price,
354+
def _get_data_from(symbols, start, end, interval, retry_count, pause, adjust_price,
355355
ret_index, chunksize, source):
356356

357357
src_fn = _source_functions[source]
358358

359359
# If a single symbol, (e.g., 'GOOG')
360360
if isinstance(symbols, (compat.string_types, int)):
361-
hist_data = src_fn(symbols, start, end, retry_count, pause)
361+
hist_data = src_fn(symbols, start, end, interval, retry_count, pause)
362362
# Or multiple symbols, (e.g., ['GOOG', 'AAPL', 'MSFT'])
363363
elif isinstance(symbols, DataFrame):
364-
hist_data = _dl_mult_symbols(symbols.index, start, end, chunksize,
364+
hist_data = _dl_mult_symbols(symbols.index, start, end, interval, chunksize,
365365
retry_count, pause, src_fn)
366366
else:
367-
hist_data = _dl_mult_symbols(symbols, start, end, chunksize,
367+
hist_data = _dl_mult_symbols(symbols, start, end, interval, chunksize,
368368
retry_count, pause, src_fn)
369369
if source.lower() == 'yahoo':
370370
if ret_index:
@@ -377,7 +377,7 @@ def _get_data_from(symbols, start, end, retry_count, pause, adjust_price,
377377

378378
def get_data_yahoo(symbols=None, start=None, end=None, retry_count=3,
379379
pause=0.001, adjust_price=False, ret_index=False,
380-
chunksize=25):
380+
chunksize=25, interval='d'):
381381
"""
382382
Returns DataFrame/Panel of historical stock prices from symbols, over date
383383
range, start to end. To avoid being penalized by Yahoo! Finance servers,
@@ -406,12 +406,17 @@ def get_data_yahoo(symbols=None, start=None, end=None, retry_count=3,
406406
If True, includes a simple return index 'Ret_Index' in hist_data.
407407
chunksize : int, default 25
408408
Number of symbols to download consecutively before intiating pause.
409+
interval : string, default 'd'
410+
Time interval code, valid values are 'd' for daily, 'w' for weekly,
411+
'm' for monthly and 'v' for dividend.
409412
410413
Returns
411414
-------
412415
hist_data : DataFrame (str) or Panel (array-like object, DataFrame)
413416
"""
414-
return _get_data_from(symbols, start, end, retry_count, pause,
417+
if interval not in ['d', 'w', 'm', 'v']:
418+
raise ValueError("Invalid interval: valid values are 'd', 'w', 'm' and 'v'")
419+
return _get_data_from(symbols, start, end, interval, retry_count, pause,
415420
adjust_price, ret_index, chunksize, 'yahoo')
416421

417422

@@ -445,7 +450,7 @@ def get_data_google(symbols=None, start=None, end=None, retry_count=3,
445450
-------
446451
hist_data : DataFrame (str) or Panel (array-like object, DataFrame)
447452
"""
448-
return _get_data_from(symbols, start, end, retry_count, pause,
453+
return _get_data_from(symbols, start, end, None, retry_count, pause,
449454
adjust_price, ret_index, chunksize, 'google')
450455

451456

pandas_datareader/tests/test_data.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def test_get_multi2(self):
117117
assert_n_failed_equals_n_null_columns(w, result)
118118

119119
def test_dtypes(self):
120-
#GH399, #GH8980
120+
#GH3995, #GH8980
121121
data = web.get_data_google('F', start='JAN-01-10', end='JAN-27-13')
122122
assert np.issubdtype(data.Open.dtype, np.number)
123123
assert np.issubdtype(data.Close.dtype, np.number)
@@ -208,6 +208,26 @@ def test_get_data_single_symbol(self):
208208
# just test that we succeed
209209
web.get_data_yahoo('GOOG')
210210

211+
def test_get_data_interval(self):
212+
# daily interval data
213+
pan = web.get_data_yahoo('XOM', '2013-01-01', '2013-12-31', interval='d')
214+
self.assertEqual(len(pan), 252)
215+
216+
# weekly interval data
217+
pan = web.get_data_yahoo('XOM', '2013-01-01', '2013-12-31', interval='w')
218+
self.assertEqual(len(pan), 53)
219+
220+
# montly interval data
221+
pan = web.get_data_yahoo('XOM', '2013-01-01', '2013-12-31', interval='m')
222+
self.assertEqual(len(pan), 12)
223+
224+
# dividend data
225+
pan = web.get_data_yahoo('XOM', '2013-01-01', '2013-12-31', interval='v')
226+
self.assertEqual(len(pan), 4)
227+
228+
# test fail on invalid interval
229+
self.assertRaises(ValueError, web.get_data_yahoo, 'XOM', interval='NOT VALID')
230+
211231
def test_get_data_multiple_symbols(self):
212232
# just test that we succeed
213233
sl = ['AAPL', 'AMZN', 'GOOG']

0 commit comments

Comments
 (0)