1

I am working on a stock data, I am trying to get the Maximum(the largest value) of the "Last" value in the last 15 minutes. Which is shown in the expected output in column named Max.

The code i tried takes so long to compute, i am sure that there is a something missing. Not sure about how to do it since i am new to pandas calculations for time-series. Could anyone please give your solution. Thanks

Code Tried:

for c in df["Last"].dropna():
    df[c]=df["Last"].fillna(0).rolling('15T').max()
new="Prev15max_min"+df["Last"].dropna()
df.loc[:df.index[0]+pd.DateOffset(minutes=15),new]=np.nan

The data i have is shown below

Timestamp        Last          
1/20/19 12:15    3071.56
1/20/19 12:17    3097.82
1/20/19 12:17    3097.82
1/20/19 12:18    3095.25
1/20/19 12:19    3087.42
1/20/19 12:20    3095.29
1/20/19 12:21    3095.25
1/20/19 12:22    3093.11
1/20/19 12:23    3103
1/20/19 12:24    3095
1/20/19 12:25    3100.6
1/20/19 12:26    3099.84
1/20/19 12:27    3098.77
1/20/19 12:29    3097.24
1/20/19 12:29    3090
1/20/19 12:30    3090
1/20/19 12:31    3094.2

The expected output

Timestamp        Last           Max   
1/20/19 12:15    3071.56
1/20/19 12:17    3097.82
1/20/19 12:17    3097.82
1/20/19 12:18    3095.25
1/20/19 12:19    3087.42
1/20/19 12:20    3095.29
1/20/19 12:21    3095.25
1/20/19 12:22    3093.11
1/20/19 12:23    3103
1/20/19 12:24    3095
1/20/19 12:25    3100.6
1/20/19 12:26    3099.84
1/20/19 12:27    3098.77
1/20/19 12:29    3097.24
1/20/19 12:29    3090          3103
1/20/19 12:30    3090          3103
1/20/19 12:31    3094.29       3103
KSp
  • 1,199
  • 1
  • 11
  • 29
  • Is any of these helpful? https://stackoverflow.com/questions/21058333/compute-rolling-maximum-drawdown-of-pandas-series https://stackoverflow.com/questions/52218596/rolling-maximum-with-numpy https://stackoverflow.com/questions/43288542/max-in-a-sliding-window-in-numpy-array – Pythonista anonymous Apr 08 '19 at 09:05

1 Answers1

0

Use pandas.to_datetime and rolling.max:

import pandas as pd

df['Timestamp'] = pd.to_datetime(df['Timestamp'])
df = df.set_index('Timestamp')
df['max'] = df['Last'].rolling('15min', min_periods=15).max()
print(df) 

Output:

                        Last     max
Timestamp                           
2019-01-20 12:15:00  3071.56     NaN
2019-01-20 12:17:00  3097.82     NaN
2019-01-20 12:17:00  3097.82     NaN
2019-01-20 12:18:00  3095.25     NaN
2019-01-20 12:19:00  3087.42     NaN
2019-01-20 12:20:00  3095.29     NaN
2019-01-20 12:21:00  3095.25     NaN
2019-01-20 12:22:00  3093.11     NaN
2019-01-20 12:23:00  3103.00     NaN
2019-01-20 12:24:00  3095.00     NaN
2019-01-20 12:25:00  3100.60     NaN
2019-01-20 12:26:00  3099.84     NaN
2019-01-20 12:27:00  3098.77     NaN
2019-01-20 12:29:00  3097.24     NaN
2019-01-20 12:29:00  3090.00  3103.0
2019-01-20 12:30:00  3090.00  3103.0
2019-01-20 12:31:00  3094.20  3103.0

If you prefer Timestamp to be a column not index, add:

df.reset_index(inplace=True)
Chris
  • 29,127
  • 3
  • 28
  • 51