简体   繁体   中英

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?

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 ie 128).
so after passing the above-mentioned parameters, the function returns a new dataframe with the window size of 256 with 50% overlapping.

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