Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use np.rolling with this condition?

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading