Skip to content

Sliding Window w/ Step Size & Get Back Windows #22976

@drcrook1

Description

@drcrook1

Code Sample, a copy-pastable example if possible

Below is likely a very poor implementation of this (it is super super slow; approximately 65 seconds for my data set with 75 windows to do and my implementation is also very memory intensive). Basically, I want to define a window size and a step size and get back a list of dataframes in order of appearance.

I need to use this because I will effectively be doing this twice. First I need to get my major windows and then in each window, I need to generate more, smaller windows as well as more data inside there.

Additionally, I need to be able to align my windows to the right, left or center and have an option to remove windows which do not have the correct sizes in them.

My dataframe is multi-indexed as well (the primary index is what I've been using to slice and that works great).

the df.rolling() seems very close but the primary draw back is I can not define a step size and I also have not been able to figure out how to get the actual windows out of it prefferably as a dataframe.

def get_slice(slicer, df, copy=True):
    vals = df.index.levels[0][:slicer]
    if(copy):
        return df.loc[pd.IndexSlice[vals,:], :].copy()
    else:
        return df.loc[pd.IndexSlice[vals,:], :]

def get_window_walk_list(df, n, slide_size, copy=True):
    """
    Takes a dataframe, a window size and a slide size.
    Returns a list of dataframes which have that size following that slice.
    """
    print("Entered window walk list")
    df.sort_index(ascending=True, inplace=True)
    list_df = [get_slice(i + n, df, copy) for i in range(0, len(df.index.levels[0]) - n, slide_size)]
    if(list_df[-1].shape[0] < df.shape[0]):
        print("appending latest")
        list_df.append(df[:].copy())
    list_df = list_df[::-1]
    return list_df

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions