1

I'm trying to access mutiple columns in rolling time window

My dataset is like

 index  Time    B    C    
  0     00:00  90    70
  1     00:01  90    10
  2     00:02  20    10         
  3     00:03  90    20       

Suppose a 3 second window, my target is to get the time difference between current row and the nearest row with same B value,

also with current row C value larger than 20. So, it needs to access all columns to perform comparsion and filtering

An example of expected output is like

 index  Time    B    C     Time difference
  0     00:00  90    70
  1     00:01  90    10
  2     00:02  20    10        00:00   
  3     00:03  90    20        00:02

Window from index 1-3 with window size 3, row 3 and row 1 will be qualified 

I tried this question, which works for numeric window size, but not for time window.

How can I access all columns in a time window, or to achieve the same result ?

Thank you!

whtitefall
  • 631
  • 9
  • 16
  • Can you put your expected output as text in your question? Is the `Time difference` column your expected output? – moys Mar 04 '20 at 02:18
  • @moys yes, I edited my question again, sorry for confusion – whtitefall Mar 04 '20 at 02:21
  • See if this works for you. `df['Time_diff'] = df.groupby('B')['Time'].apply(lambda x:pd.to_datetime(x, format = '%M:%S').diff())` this will get the diff between 1st & 2nd row as well because B is same there also – moys Mar 04 '20 at 02:37
  • @moys the code is working..but I think it's not a time window, since it's grouped by the column B – whtitefall Mar 04 '20 at 03:00

0 Answers0