I would like to materialize for each row of a dataframe the corresponding group key it would get if I was using a groupby operation with a pandas Grouper.
import pandas as pd
# Test data
ts = [pd.Timestamp('2022/03/01 09:00'),
pd.Timestamp('2022/03/01 10:00'),
pd.Timestamp('2022/03/01 10:30'),
pd.Timestamp('2022/03/01 15:00')]
df = pd.DataFrame({'a':range(len(ts)), 'ts': ts})
grouper = pd.Grouper(key='ts', freq='2H', sort=False, origin='start_day')
Is there any way to get for each row the corresponding groupkey? The result I am looking for could be either a list, or a pandas Series or Index, or numpy array, the same length as the initial dataframe, and would then contain following values.
result = pd.Series([pd.Timestamp('2022-03-01 08:00:00'),
pd.Timestamp('2022-03-01 10:00:00'),
pd.Timestamp('2022-03-01 10:00:00'),
pd.Timestamp('2022-03-01 14:00:00')])
Thanks for your help! Bests
