python - Rolling argmax in pandas -


i have pandas timeseries , apply argmax function rolling window. however, due casting float rolling_apply, if apply numpy.argmax(), obtain index of slice of ndarray. there way apply rolling argmax series/dataframe?

series.idxmax() or series.argmax() both return timestamp object pandas.rolling_apply(series, window=10,func=lambda x: pandas.series(x).idxmax()) return float64.

edit: here example:

import pandas pd import numpy np import pandas.io.data web import datetime  start = datetime.datetime(2001,1,1) end = datetime.datetime.today() close = web.datareader('aapl','yahoo',start,end).close close = close / close.shift(1) - 1  close.resample('w-mon').idxmax()    # timestamp object close.resample('w-mon').argmax()    # timestamp object  pd.rolling_apply(close.resample('w-mon'), window=52, func=lambda x: pd.series(x).argmax())     

a working way be

ix = pd.rolling_apply(close, window=52, func=np.argmax) ix = np.where(np.isnan(ix),0,ix) ix = ix.astype(int) new_index = close.index[52:].map(lambda x:     close.index[np.argwhere(close.index==x)-52:np.argwhere(close.index==x)]  [ix[np.argwhere(close.index==x)]]) pd.series(new_index,index=close.index[52:]).apply(lambda x: x.flatten()[0]) 

but maybe there "pandonic" way?

this not implemented atm, not difficult, see issue here

here work-around, doing apply 'manually', should pretty efficient actually.

in [59]: rc = close.resample('w-mon')  in [60]: def f(rc, i, l):                                        s = rc.iloc[(i*l):((i+1)*l)]     try:         return s.loc[[s.idxmax()]]     except:         return none    ....:       in [61]: pd.concat([ f(rc, i, 52) in range(len(rc)) ]) out[61]:  date 2001-06-25    0.034350 2002-02-04    0.017548 2003-05-05    0.031083 2004-10-18    0.044588 2005-05-23    0.022959                 ...    2011-08-29    0.018310 2012-03-19    0.017339 2013-09-23    0.017571 2014-04-28    0.023196 2015-02-16    0.015051 name: close, dtype: float64 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -