python - Sliding window iterator using rolling in pandas -


if it's single row, can iterator following

import pandas pd import numpy np  = np.zeros((100,40)) x = pd.dataframe(a)  index, row in x.iterrows():     print index     print row 

now want each iterator return subset x[0:9, :], x[5:14, :], x[10:19, :] etc. how achieve rolling (pandas.dataframe.rolling)?

i'll experiment following dataframe.

setup

import pandas pd import numpy np string import uppercase  def generic_portfolio_df(start, end, freq, num_port, num_sec, seed=314):     np.random.seed(seed)     portfolios = pd.index(['portfolio {}'.format(i) in uppercase[:num_port]],                           name='portfolio')     securities = ['s{:02d}'.format(i) in range(num_sec)]     dates = pd.date_range(start, end, freq=freq)     return pd.dataframe(np.random.rand(len(dates) * num_sec, num_port),                         index=pd.multiindex.from_product([dates, securities],                                                          names=['date', 'id']),                         columns=portfolios                        ).groupby(level=0).apply(lambda x: x / x.sum())       df = generic_portfolio_df('2014-12-31', '2015-05-30', 'bm', 3, 5)  df.head(10) 

enter image description here

i'll introduce function roll number of rows , concatenate single dataframe i'll add top level column index indicates location in roll.

solution step-1

def rolled(df, n):     k = range(df.columns.nlevels)     _k = [i - len(k) in k]     myroll = pd.concat([df.shift(i).stack(level=k) in range(n)],                        axis=1, keys=range(n)).unstack(level=_k)     return [(i, row.unstack(0)) i, row in myroll.iterrows()] 

though hidden in function, myroll this

enter image description here

now can use iterator.

solution step-2

for i, roll in rolled(df.head(5), 3):     print roll     print                      0   1   2 portfolio                     portfolio  0.326164 nan nan portfolio b  0.201597 nan nan portfolio c  0.085340 nan nan                      0         1   2 portfolio                           portfolio  0.278614  0.326164 nan portfolio b  0.314448  0.201597 nan portfolio c  0.266392  0.085340 nan                      0         1         2 portfolio                                 portfolio  0.258958  0.278614  0.326164 portfolio b  0.089224  0.314448  0.201597 portfolio c  0.293570  0.266392  0.085340                      0         1         2 portfolio                                 portfolio  0.092760  0.258958  0.278614 portfolio b  0.262511  0.089224  0.314448 portfolio c  0.084208  0.293570  0.266392                      0         1         2 portfolio                                 portfolio  0.043503  0.092760  0.258958 portfolio b  0.132221  0.262511  0.089224 portfolio c  0.270490  0.084208  0.293570 

Comments