I'm just trying to understand grouper
a little more. I know calling the frequency
makes some functions void. But I was wondering if there was a work around to count the last segment when using grouper
. For example, I want the max
count to include the 15min segment between 13:30:00 and 13:45:00
for the df
below.
df = pd.DataFrame({
'Time' : ['1904-01-01 13:00:00','1904-01-01 13:10:00','1904-01-01 13:15:00','1904-01-01 13:25:00','1904-01-01 13:35:00'],
'Number' : [2,2,1,1,1],
})
df['Time'] = pd.to_datetime(df['Time'])
df = df.groupby(pd.Grouper(freq='15T', key='Time', closed = 'left'))['Number'].max().ffill()
df = df.reset_index(level=['Time'])
Out:
Time Number
0 1904-01-01 13:00:00 2
1 1904-01-01 13:15:00 1
2 1904-01-01 13:30:00 1
This can be achieved by using the label = 'right'
. But the output is:
Time Number
0 1904-01-01 13:15:00 2
1 1904-01-01 13:30:00 1
2 1904-01-01 13:45:00 1
I'm hoping to return the df
:
Time Number
0 1904-01-01 13:00:00 2
1 1904-01-01 13:15:00 1
2 1904-01-01 13:30:00 1
3 1904-01-01 13:45:00 1