Ashish Prasad Sah
Ashish Prasad Sah

Reputation: 49

rolling window with 50% overlapping on window size in pandas

I have a data frame like this which is imported from a CSV. Data frame

I would like to create a sliding window of size 256 with overlapping of 50%. For example, window1 should contain data from index 0-255, window2 should contain data from index 128-383 and so on until all the data is split in their respective windows. I am trying rolling.windows from pandas to create windows without any success. I want to achieve something like this. Overlapping windows How can I do that using the optimized methods included in Pandas or Numpy?

Upvotes: 0

Views: 2104

Answers (1)

Ashish Prasad Sah
Ashish Prasad Sah

Reputation: 49

def windows(d, w, t):  
    r = np.arange(len(d))   
    s = r[::t]   
    z = list(zip(s, s + w))   
    f = '{0[0]}:{0[1]}'.format
    g = lambda t: d.iloc[t[0]:t[1]]   
    return pd.concat(map(g, z), keys=map(f, z))   

windows(d,256,128)
d: dataframe w: window size(256) t: overlapping factor (eg. 50% of window size i.e 128).
so after passing the above-mentioned parameters, the function returns a new dataframe with the window size of 256 with 50% overlapping.

Upvotes: 1

Related Questions