Pandas groupby rank date time

Viewed 9468

i got an issue over ranking of date times. Lets say i have following table.

ID    TIME
01    2018-07-11 11:12:20
01    2018-07-12 12:00:23
01    2018-07-13 12:00:00
02    2019-09-11 11:00:00
02    2019-09-12 12:00:00

and i want to add another column to rank the table by time for each id and group. I used

df['RANK'] = data.groupby('ID')['TIME'].rank(ascending=True)

but get an error:

'NoneType' object is not callable

If i replace datetime to numbers, it works.... any solutions?

1 Answers

For me working convert column to datetimes for avoid your error.

data['TIME'] = pd.to_datetime(data['TIME'])
data['RANK'] = data.groupby('ID')['TIME'].rank(ascending=True)
print (data)
   ID                TIME  RANK
0   1 2018-07-11 11:12:20   1.0
1   1 2018-07-12 12:00:23   2.0
2   1 2018-07-13 12:00:00   3.0
3   2 2019-09-11 11:00:00   1.0
4   2 2019-09-12 12:00:00   2.0
Related