Skip to content

Commit be533f6

Browse files
nehaleckyy-p
authored and
y-p
committed
CLN: tweaks in io.data (yahoo)
1 parent ed89690 commit be533f6

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

pandas/io/data.py

+23-10
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def get_quote_yahoo(symbols):
133133

134134

135135
def _get_hist_yahoo(sym=None, start=None, end=None, retry_count=3,
136-
pause=0):
136+
pause=0, **kwargs):
137137
"""
138138
Get historical data for the given name from yahoo.
139139
Date format is datetime
@@ -195,11 +195,22 @@ def _adjust_prices(hist_data, price_list=['Open', 'High', 'Low', 'Close']):
195195

196196
def _calc_return_index(price_df):
197197
"""
198-
Return a returns index from a input price df or series.
198+
Return a returns index from a input price df or series. Initial value
199+
(typically NaN) is set to 1.
199200
"""
200-
201-
ret_index = price_df.pct_change().add(1).cumprod()
202-
ret_index.ix[0] = 1
201+
df = price_df.pct_change().add(1).cumprod()
202+
mask = ~df.ix[1].isnull() & df.ix[0].isnull()
203+
df.ix[0][mask] = 1
204+
205+
#Check for first stock listings after starting date of index in ret_index
206+
#If True, find first_valid_index and set previous entry to 1.
207+
if(~mask).any:
208+
for sym in mask.index[~mask]:
209+
tstamp = df[sym].first_valid_index()
210+
t_idx = df.index.get_loc(tstamp) - 1
211+
df[sym].ix[t_idx] = 1
212+
213+
ret_index = df
203214
return ret_index
204215

205216

@@ -258,16 +269,17 @@ def get_components_yahoo(idx_sym):
258269

259270

260271
def get_data_yahoo(symbols=None, start=None, end=None, retry_count=3, pause=0,
261-
adjust_price=False, ret_index=False, chunksize=25, **kwargs):
272+
adjust_price=False, ret_index=False, chunksize=25,
273+
**kwargs):
262274
"""
263275
Returns DataFrame/Panel of historical stock prices from symbols, over date
264276
range, start to end. To avoid being penalized by Yahoo! Finance servers,
265277
pauses between downloading 'chunks' of symbols can be specified.
266278
267279
Parameters
268280
----------
269-
symbols : string, list-like object (list, tupel, Series), or DataFrame
270-
Single stock symbol (ticker), list-like object of symbols or
281+
symbols : string, array-like object (list, tuple, Series), or DataFrame
282+
Single stock symbol (ticker), array-like object of symbols or
271283
DataFrame with index containing stock symbols.
272284
start : string, (defaults to '1/1/2010')
273285
Starting date, timestamp. Parses many different kind of date
@@ -290,7 +302,7 @@ def get_data_yahoo(symbols=None, start=None, end=None, retry_count=3, pause=0,
290302
291303
Returns
292304
-------
293-
hist_data : DataFrame (str) or Panel (list-like object, DataFrame)
305+
hist_data : DataFrame (str) or Panel (array-like object, DataFrame)
294306
"""
295307

296308
def dl_mult_symbols(symbols):
@@ -444,7 +456,8 @@ class Options(object):
444456
445457
# Fetch call and put data with expiry from now to 8 months out
446458
>>> forward_calls, forward_puts = aapl.get_forward_data(8,
447-
... call=True, put=True)
459+
... call=True, put=True)
460+
448461
"""
449462

450463
def __init__(self, symbol):

0 commit comments

Comments
 (0)