How to find max in dataframe with in a certain length in python?
<p>Here's my DataFrame:</p>

<pre><code>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
</code></pre>

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

<p>So I would like it to look like this.</p>

<pre><code>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
</code></pre>

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

<p>you can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.rolling.html" rel="nofollow">.rolling().max()</a> method:</p>

<pre><code>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
</code></pre>

<p>Explanation:</p>

<pre><code>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
</code></pre>

python python-2.7 pandas dataframe