This solution provides a single liner using a list. Starting from the left side of the time series and iterating forward (a reverse iteration can also be performed), the iteration returns a subset of the index equal to the loop window and goes into step size equal to the frequency. Note that the last period is probably a stub whose length is shorter than the reverse window.
This method uses days, not monthly or weekly shifts.
freq = 30 # Days lookback = 60 # Days idx = pd.date_range('2010-01-01', '2015-01-01') [idx[(freq * n):(lookback + freq * n)] for n in range(int(len(idx) / freq))] Out[86]: [<class 'pandas.tseries.index.DatetimeIndex'> [2010-01-01, ..., 2010-03-01] Length: 60, Freq: D, Timezone: None, <class 'pandas.tseries.index.DatetimeIndex'> [2010-01-31, ..., 2010-03-31] Length: 60, Freq: D, Timezone: None, ... Length: 60, Freq: D, Timezone: None, <class 'pandas.tseries.index.DatetimeIndex'> [2014-11-06, ..., 2015-01-01] Length: 57, Freq: D, Timezone: None]
source share