I have an OHLC and datetime dataframe. I would like to have a running volume where maximum volume for the last n rows provided the data is the same. np.rolling(window=n).max() would not work here because I would like to have max of only that day for which I am currently on.
Date Volume Running Max volume
8th Jan 2021 28495 28495
8th Jan 2021 26936 28495
8th Jan 2021 9504 28495
8th Jan 2021 32056 32056
9th Jan 2021 12032 12032
9th Jan 2021 27334 27334
9th Jan 2021 42278 42278
9th Jan 2021 5696 42278
9th Jan 2021 28997 42278
10th Jan 2021 41490 41490
10th Jan 2021 32624 41490
10th Jan 2021 19975 41490
10th Jan 2021 24048 41490
10th Jan 2021 48014 48014
>Solution :
Try:
n = 5 # window
df['Running Max volume'] = df.groupby('Date')['Volume'].rolling(n, 1) \
.max().droplevel(0).astype(int)
print(df)
# Output
Date Volume Running Max volume
0 2021-01-08 28495 28495
1 2021-01-08 26936 28495
2 2021-01-08 9504 28495
3 2021-01-08 32056 32056
4 2021-01-09 12032 12032
5 2021-01-09 27334 27334
6 2021-01-09 42278 42278
7 2021-01-09 5696 42278
8 2021-01-09 28997 42278
9 2021-01-10 41490 41490
10 2021-01-10 32624 41490
11 2021-01-10 19975 41490
12 2021-01-10 24048 41490
13 2021-01-10 48014 48014