Uis234 Uis234 - 4 months ago 12
Python Question

Timegrouper of Timegrouper

Hi I have the following Timegrouper dataframe where the maximum value is calculated for every quarter of an hour from data that was separated per minute:

in:
newdf2 = newdf.groupby(pd.TimeGrouper(freq='15T')).max().fillna(0)
Out:

Count
time
2016-04-20 05:30:00 4.0
2016-04-20 05:45:00 0.0
2016-04-20 06:00:00 0.0
2016-04-20 06:15:00 6.0
2016-04-20 06:30:00 5.0


I would like this to go back to minutes like this:

Count
time
2016-04-20 05:31:00 4.0
2016-04-20 05:32:00 4.0
2016-04-20 05:33:00 4.0
2016-04-20 05:34:00 4.0
2016-04-20 05:35:00 4.0
...
2016-04-20 05:44:00 4.0
2016-04-20 05:45:00 0.0


I tried to do the timegrouper again with interval= '1T' but this returns nothing.

Can i achieve this by a simple pandas function?

Answer

I think you need resample with ffill:

df = df.resample('1T').ffill()
print (df)
                     Count
time                      
2016-04-20 05:30:00    4.0
2016-04-20 05:31:00    4.0
2016-04-20 05:32:00    4.0
2016-04-20 05:33:00    4.0
2016-04-20 05:34:00    4.0
2016-04-20 05:35:00    4.0
2016-04-20 05:36:00    4.0
2016-04-20 05:37:00    4.0
2016-04-20 05:38:00    4.0
2016-04-20 05:39:00    4.0
2016-04-20 05:40:00    4.0
2016-04-20 05:41:00    4.0
2016-04-20 05:42:00    4.0
2016-04-20 05:43:00    4.0
2016-04-20 05:44:00    4.0
2016-04-20 05:45:00    0.0
2016-04-20 05:46:00    0.0
...
...
Comments