I am aggregating dataframes that contain data evenly sampled using pd.Grouper to group the data and then apply a function. Internally, once a group is defined by time (imagine groups of 1 second), the consecutive group starts at the following second without overlapping.
Imagine that the data is this table:
| time | col1 |
|:----:|:----:|
| 0.0s | 0.15 |
| 0.1s | 0.21 |
| 0.2s | 0.05 |
| ... | ... |
Then I used this dataframe to aggregate the data with groupby and grouper to aggregate by 1 second:
grouped = df.groupby([pd.Grouper(level=df.time, freq='1s', dropna=True) ])
If the data on the dataframe has a constant sampling (0.1 Hz), the result will be an object of type <pandas.core.groupby.generic.SeriesGroupBy> in which each group will have a dataframe with 10 rows. However, I cannot specify that I want groups with 10 rows but with an advance of 5 rows between groups (something similar to what Welch algorithm does to compute FFT, but with a DataFrame).
My question is: Is it possible to define the groupby() with an overlap between groups? I know that it is better to do it witouth the groupby(), but I want that my result is of the same type pandas.core.groupby.generic.SeriesGroupBy
What I tried was to create a generator in the form:
def custom_grouper(df, bucket, overlap):
start= 0
step = bucket - overlap
while start+bucket < df.shape[0]:
yield start, df.iloc[start:start+bucket]
start += step
However, I cannot use the functions of a groupby object. Can I transform this generator into a custom groupby?