I am struggling to find the correct way to group a DataFrame using some constraints. I have the following Dataframe:
start_dt machine benchmark value1 value2 value3
2021-06-07 07:32:01 A bench1 0 0 0
2021-06-07 07:32:37 A bench1 0 0 0
2021-06-07 07:33:13 A bench1 0 0 0
2021-06-07 07:33:49 A bench1 0 0 0
2021-06-07 07:34:26 A bench1 0 0 0
2021-06-07 08:30:26 A bench1 0 0 10
2021-06-07 11:12:21 A bench1 0 0 6
2021-06-07 12:05:21 A bench1 1 0 10
2021-06-17 12:28:57 A bench2 0 0 0
2021-06-17 12:29:29 A bench2 0 0 0
2021-06-17 12:33:09 A bench2 3 0 1
2021-06-17 12:33:48 A bench2 3 0 1
2021-06-17 12:35:17 A bench2 0 0 0
I want to group base on machine, benchmark, and start_dt columns. However, it has some constraints on the start_dt column. start_dt group criteria must be on 1h chunks. I have tried the following command:
df.groupby(["machine", "benchmark", pd.Grouper(key="start_dt", freq="1h", sort=True, origin="start")]).sum()
However, it will group the dataframe based on the first datetime on for all benchmaks, and I don't want this. What I would like is something like the following, where end_dt is start_dt + 1h.
machine benchmark start_dt end_dt value1 value2 value3
A bench1 2021-06-07 07:32:01 2021-06-07 08:32:01 0 0 10
2021-06-07 11:12:21 2021-06-07 12:12:21 1 0 16
bench2 2021-06-17 12:28:57 2021-06-17 13:28:57 6 0 2
For example for machine A and benchmark bench1 there are at least two time intervals
2021-06-07 07:32:01 2021-06-07 08:32:01
2021-06-07 11:12:21 2021-06-07 12:12:21
but nothing in the middle, consequentially I would like to maintain the time intervals as they appear on the column instead of what pandas Grouper gives to me. Is it possible?
Edits:
- The timestamps are unique