1

Here's my DataFrame:

DATE_TIME
1997-02-25 12:50:00    3238.0
1997-02-25 12:55:00    3237.5
1997-02-25 13:00:00    3237.5
1997-02-25 13:05:00    3237.5
1997-02-25 13:10:00    3239.0
1997-02-25 13:15:00    3242.0

I'm trying to find a way to find the max for every 2 intervals back and still keep in the DataFrame format.

So I would like it to look like this.

DATE_TIME
1997-02-25 12:50:00    3238.0
1997-02-25 12:55:00    3238.0
1997-02-25 13:00:00    3238.0
1997-02-25 13:05:00    3237.5
1997-02-25 13:10:00    3239.0
1997-02-25 13:15:00    3242.0

I've tried a variety of different things. Any help would be greatly appreciated.

1 Answer 1

1

you can use .rolling().max() method:

In [141]: df
Out[141]:
            DATE_TIME     VAL
0 1997-02-25 12:50:00  3238.0
1 1997-02-25 12:55:00  3237.5
2 1997-02-25 13:00:00  3237.5
3 1997-02-25 13:05:00  3237.5
4 1997-02-25 13:10:00  3239.0
5 1997-02-25 13:15:00  3242.0

In [142]: df['roll_3_max'] = df['VAL'].rolling(3).max().bfill()

In [143]: df
Out[143]:
            DATE_TIME     VAL  roll_3_max
0 1997-02-25 12:50:00  3238.0      3238.0
1 1997-02-25 12:55:00  3237.5      3238.0
2 1997-02-25 13:00:00  3237.5      3238.0
3 1997-02-25 13:05:00  3237.5      3237.5
4 1997-02-25 13:10:00  3239.0      3239.0
5 1997-02-25 13:15:00  3242.0      3242.0

Explanation:

In [140]: df['VAL'].rolling(3).max()
Out[140]:
0       NaN
1       NaN
2    3238.0
3    3237.5
4    3239.0
5    3242.0
Name: VAL, dtype: float64
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.