Search code examples
pythonwindowoverlappooling

Calculate minimum of overlapping window


I am having trouble with thinking of a way to solve a problem. I have a dataframe like the one below:

enter image description here

These are values for overlapping windows. My challenge is to calculate the minimum value when pooling(?) the windows. It should look like:

enter image description here

I am a bit lost here on what direction to take, does anyone have any suggestions?


Solution

  • Assuming that you are looking to define windows that do not overlap with the value of the minimum if the new window is the intersection of two old windows, we need to solve this issue by doing two things.

    • first we need to define the windows. i assume that if we get all the starting points and the end points we can do this as the windows don't have to be of equal size, just not overlapping each other.

      my_list = np.sort(list(set(np.concatenate([df['Start'].values,df['End'].values]))))

      then we build a new dataframe from this list:

      new_df = pd.DataFrame({'Start':my_list[:-1],'End':my_list[1:]})

    • then we find the value of the min:

      new_df['value'] = new_df.apply(lambda x: min(df[(df['Start'] <= x[0]) & (df['End'] >= x[1])]['Value']), axis = 1)

    OUTPUT

              Start End value
           0    0   5   0.1
           1    5   10  0.1
           2    10  15  0.2
           3    15  20  0.2
           4    20  25  0.4
           5    25  30  0.3
           6    30  35  0.3
    

    if you need a more detail explanation of what each line/function is doing, please feel free to ask for more details, i'll update the answer.