Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groupby with TimeGrouper 'backwards'

I have a DataFrame containing a time series:

rng = pd.date_range('2016-06-01', periods=24*7, freq='H')
ones = pd.Series([1]*24*7, rng)
rdf = pd.DataFrame({'a': ones})

Last entry is 2016-06-07 23:00:00. I now want to group this by, say two days, basically like so:

rdf.groupby(pd.TimeGrouper('2D')).sum()

However, I want to group starting from my last data point backwards, so instead of getting this result:

            a
2016-06-01  48
2016-06-03  48
2016-06-05  48
2016-06-07  24

I'd much rather expect this:

            a
2016-06-01  24
2016-06-03  48
2016-06-05  48
2016-06-07  48

and when grouping by '3D':

            a
2016-06-01  24
2016-06-04  72
2016-06-07  72

Expected outcome when grouping by '4D' is:

            a
2016-06-03  72
2016-06-07  96

I am not able to get this with every combination of closed, label etc. I could think of.

How can I achieve this?

like image 1000
TomTom101 Avatar asked Oct 31 '22 01:10

TomTom101


1 Answers

Since I primarily want to group by 7 days, aka one week, I am using this method now to come to the desired bins:

from pandas.tseries.offsets import Week

# Let's not make full weeks
hours = 24*6*4
rng = pd.date_range('2016-06-01', periods=hours, freq='H')

# Set week start to whatever the last weekday of the range is
print("Last day is %s" % rng[-1])
freq = Week(weekday=rng[-1].weekday())

ones = pd.Series([1]*hours, rng)
rdf = pd.DataFrame({'a': ones})
rdf.groupby(pd.TimeGrouper(freq=freq, closed='right', label='right')).sum()

This gives me the desired output of

2016-06-25  96
2016-07-02  168
2016-07-09  168
like image 69
TomTom101 Avatar answered Nov 09 '22 09:11

TomTom101