简体   繁体   中英

Rolling window Pandas

I need to create a dataset out of my time series which contains samples made out of rolling, overlapping windows. That is, to split my dataframe with a certain window size and a certain step.

How to do this using Pandas? I see that there is a rolling window, but it is used to perform some aggregations over the values in the window (eg calculating rolling average). I'm only interested in isolating these overlapping windows. How to do it?

So the output would be dataframe like this:

1, a
2, b
3, c
4, d
5, e
6, f
7, g

And for window size 3 and step 2 the output would be:

1, a
2, b
3, c

3, c
4, d
5, e

5, e
6, f
7, g

Just to be clear, I know how to write a function for this, just wanted to check if there's something already available in Pandas.

I don't think there is a any pandas function that would help you. A simple implementation is:

A = pd.DataFrame(index=range(1,10), 
                 data=['a','b','c','d','e','f','g','h','i'], 
                 columns=['letters'])

step = 2 
size = 3
n_examples = len(A)
dataframes = []
k=0

while(k * step + size < n_examples):
    dataframes += [A.loc[k * step:k * step + size]]
    k+=1

print(dataframes)

With output:

[  letters
 1       a
 2       b
 3       c,   letters
 2       b
 3       c
 4       d
 5       e,   letters
 4       d
 5       e
 6       f
 7       g]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM