I know that it is possible to group your data by time intervals of the same length by using the function resample
. But how can I group by time intervals of custom length (i.e. irregular time intervals)?
Here is an example: Say we have a dataframe with time values, like this:
rng = pd.date_range(start='2015-02-11', periods=7, freq='M')
df = pd.DataFrame({ 'Date': rng, 'Val': np.random.randn(len(rng)) })
And we have the following time intervals:
2015-02-12 ----- 2015-05-10
2015-05-10 ----- 2015-08-20
2015-08-20 ----- 2016-01-01
It is clear that rows with index 0,1,2 belong to the first time interval, rows with index 3,4,5 belong to the second time interval and row 7 belongs to the last time interval.
My question is: how do I group these rows according to those specific time intervals, in order to perform aggregate functions (e.g. mean) on them?