Home > Software design >  How to get the expanding maximum of a pandas groupby object
How to get the expanding maximum of a pandas groupby object

Time:07-07

I'm trying to calculate the expanding maximum for each 15 minute interval in a dataframe. Here is some sample data:

import pandas as pd
import numpy as np

np.random.seed(0)
indexcol = pd.date_range('01-01-2015 00:00:00','01-02-2015 00:00:00',freq='1min',tz='Europe/Amsterdam')
df = pd.DataFrame({'value':np.random.random(len(indexcol))},index=indexcol)
df = (df - 0.5).cumsum()

Which looks like this:

                              value
2015-01-01 00:00:00 01:00   0.048814
2015-01-01 00:01:00 01:00   0.264003
2015-01-01 00:02:00 01:00   0.366766
2015-01-01 00:03:00 01:00   0.411649
2015-01-01 00:04:00 01:00   0.335304
                             ...
2015-01-06 23:56:00 01:00 -38.901560
2015-01-06 23:57:00 01:00 -39.387544
2015-01-06 23:58:00 01:00 -39.015269
2015-01-06 23:59:00 01:00 -39.151855
2015-01-07 00:00:00 01:00 -39.166035

Visualization of sample data

Now I want to calculate the expanding maximum for each 15 minutes. So I tried the following code:

df['max'] = df['value'].groupby(pd.Grouper(freq='15min')).expanding().max()

Unfortunately this gives the following error:

IndexError: index 97 is out of bounds for axis 0 with size 97

I don't understand why this fails. Can someone please explain?

CodePudding user response:

It turns out you don't need to use the expanding() function. After fiddling around a while I fixed it by using the cummax() function of pandas (thanks to Cummax of data

CodePudding user response:

Try this:

df.groupby(pd.Grouper(freq='15min')).cummax()

You dont need the expanding function, it is redundant. Your error was caused, because the expanding function grabbed the grouped by object, and tried to apply the max, but without the min window frame it didnt know what to do. So it gave you that error. Also nice question

  • Related