Skip to content

Commit 230a899

Browse files
author
y-p
committed
Merge pull request #5817 from jtratner/make-urls-globals
CLN: Make io/data urls easier to monkey-patch
2 parents ad1b215 + c45db0e commit 230a899

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

pandas/io/data.py

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ def _in_chunks(seq, size):
107107
'time': 't1', 'short_ratio': 's7'}
108108

109109

110+
_YAHOO_QUOTE_URL = 'http://finance.yahoo.com/d/quotes.csv?'
111+
112+
110113
def get_quote_yahoo(symbols):
111114
"""
112115
Get current yahoo quote
@@ -124,8 +127,7 @@ def get_quote_yahoo(symbols):
124127

125128
data = defaultdict(list)
126129

127-
url_str = 'http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s' % (sym_list,
128-
request)
130+
url_str = _YAHOO_QUOTE_URL + 's=%s&f=%s' % (sym_list, request)
129131

130132
with urlopen(url_str) as url:
131133
lines = url.readlines()
@@ -175,6 +177,9 @@ def _retry_read_url(url, retry_count, pause, name):
175177
"return a 200 for url %r" % (retry_count, name, url))
176178

177179

180+
_HISTORICAL_YAHOO_URL = 'http://ichart.finance.yahoo.com/table.csv?'
181+
182+
178183
def _get_hist_yahoo(sym, start, end, retry_count, pause):
179184
"""
180185
Get historical data for the given name from yahoo.
@@ -183,8 +188,7 @@ def _get_hist_yahoo(sym, start, end, retry_count, pause):
183188
Returns a DataFrame.
184189
"""
185190
start, end = _sanitize_dates(start, end)
186-
yahoo_url = 'http://ichart.finance.yahoo.com/table.csv?'
187-
url = (yahoo_url + 's=%s' % sym +
191+
url = (_HISTORICAL_YAHOO_URL + 's=%s' % sym +
188192
'&a=%s' % (start.month - 1) +
189193
'&b=%s' % start.day +
190194
'&c=%s' % start.year +
@@ -196,6 +200,9 @@ def _get_hist_yahoo(sym, start, end, retry_count, pause):
196200
return _retry_read_url(url, retry_count, pause, 'Yahoo!')
197201

198202

203+
_HISTORICAL_GOOGLE_URL = 'http://www.google.com/finance/historical?'
204+
205+
199206
def _get_hist_google(sym, start, end, retry_count, pause):
200207
"""
201208
Get historical data for the given name from google.
@@ -204,13 +211,13 @@ def _get_hist_google(sym, start, end, retry_count, pause):
204211
Returns a DataFrame.
205212
"""
206213
start, end = _sanitize_dates(start, end)
207-
google_URL = 'http://www.google.com/finance/historical?'
208214

209215
# www.google.com/finance/historical?q=GOOG&startdate=Jun+9%2C+2011&enddate=Jun+8%2C+2013&output=csv
210-
url = google_URL + urlencode({"q": sym,
211-
"startdate": start.strftime('%b %d, ' '%Y'),
212-
"enddate": end.strftime('%b %d, %Y'),
213-
"output": "csv"})
216+
url = "%s%s" % (_HISTORICAL_GOOGLE_URL,
217+
urlencode({"q": sym,
218+
"startdate": start.strftime('%b %d, ' '%Y'),
219+
"enddate": end.strftime('%b %d, %Y'),
220+
"output": "csv"}))
214221
return _retry_read_url(url, retry_count, pause, 'Google')
215222

216223

@@ -251,6 +258,9 @@ def _calc_return_index(price_df):
251258
return df
252259

253260

261+
_YAHOO_COMPONENTS_URL = 'http://download.finance.yahoo.com/d/quotes.csv?'
262+
263+
254264
def get_components_yahoo(idx_sym):
255265
"""
256266
Returns DataFrame containing list of component information for
@@ -275,8 +285,7 @@ def get_components_yahoo(idx_sym):
275285
stats = 'snx'
276286
# URL of form:
277287
# http://download.finance.yahoo.com/d/quotes.csv?s=@%5EIXIC&f=snxl1d1t1c1ohgv
278-
url = ('http://download.finance.yahoo.com/d/quotes.csv?s={0}&f={1}'
279-
'&e=.csv&h={2}')
288+
url = _YAHOO_COMPONENTS_URL + 's={0}&f={1}&e=.csv&h={2}'
280289

281290
idx_mod = idx_sym.replace('^', '@%5E')
282291
url_str = url.format(idx_mod, stats, 1)
@@ -430,6 +439,9 @@ def get_data_google(symbols=None, start=None, end=None, retry_count=3,
430439
adjust_price, ret_index, chunksize, 'google', name)
431440

432441

442+
_FRED_URL = "http://research.stlouisfed.org/fred2/series/"
443+
444+
433445
def get_data_fred(name, start=dt.datetime(2010, 1, 1),
434446
end=dt.datetime.today()):
435447
"""
@@ -443,14 +455,12 @@ def get_data_fred(name, start=dt.datetime(2010, 1, 1),
443455
"""
444456
start, end = _sanitize_dates(start, end)
445457

446-
fred_URL = "http://research.stlouisfed.org/fred2/series/"
447-
448458
if not is_list_like(name):
449459
names = [name]
450460
else:
451461
names = name
452462

453-
urls = [fred_URL + '%s' % n + '/downloaddata/%s' % n + '.csv' for
463+
urls = [_FRED_URL + '%s' % n + '/downloaddata/%s' % n + '.csv' for
454464
n in names]
455465

456466
def fetch_data(url, name):
@@ -470,11 +480,12 @@ def fetch_data(url, name):
470480
return df
471481

472482

483+
_FAMAFRENCH_URL = 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp'
484+
485+
473486
def get_data_famafrench(name):
474487
# path of zip files
475-
zip_file_url = ('http://mba.tuck.dartmouth.edu/pages/faculty/'
476-
'ken.french/ftp')
477-
zip_file_path = '{0}/{1}.zip'.format(zip_file_url, name)
488+
zip_file_path = '{0}/{1}.zip'.format(_FAMAFRENCH_URL, name)
478489

479490
with urlopen(zip_file_path) as url:
480491
raw = url.read()
@@ -618,10 +629,12 @@ def get_options_data(self, month=None, year=None, expiry=None):
618629
return [f(month, year, expiry) for f in (self.get_put_data,
619630
self.get_call_data)]
620631

632+
_OPTIONS_BASE_URL = 'http://finance.yahoo.com/q/op?s={sym}'
633+
621634
def _get_option_data(self, month, year, expiry, table_loc, name):
622635
year, month = self._try_parse_dates(year, month, expiry)
623636

624-
url = 'http://finance.yahoo.com/q/op?s={sym}'.format(sym=self.symbol)
637+
url = self._OPTIONS_BASE_URL.format(sym=self.symbol)
625638

626639
if month and year: # try to get specified month from yahoo finance
627640
m1, m2 = _two_char_month(month), month

0 commit comments

Comments
 (0)