I need to create periods and named them like 0,1,2,3 according to time column, for each class in dataframe separately

I have already tried

df = df.set_index(pd.DatetimeIndex(df['time']))
newdf = df.groupby(pd.TimeGrouper('10Min'),as_index=False).apply(lambda x: x['assetId'])
df['period'] = newdf.index.get_level_values(0)

But; This code grouping time but I have same time values in different classes that is why its mixing classes. I want to do it for each class separately. My datafame looks like:

Class  Time
702  2018-09-03T04:08:04Z
702  2018-09-03T04:18:34Z
703  2018-09-03T04:08:04Z
703  2018-09-03T04:40:05Z
703  2018-09-03T04:40:05Z

The result I need to get is;

Class  Time                Period
702  2018-09-03T04:08:04Z    1
702  2018-09-03T04:18:34Z    2
703  2018-09-03T04:08:04Z    1
703  2018-09-03T04:40:05Z    2
703  2018-09-03T04:40:05Z    3

0 Answers