Это должно сработать. Я не думаю, что вы сможете значительно улучшить итерацию по фрейму данных для назначения идентификаторов, поскольку они основаны на цепочечном соединении с предыдущими значениями в столбце:
d = {"created_at": pd.to_datetime(["2018-07-03 02:12:33", "2018-07-03 02:12:35","2018-07-03 02:12:40","2018-07-03 02:12:45","2018-07-03 02:12:48","2018-07-03 03:15:17","2018-07-03 03:15:20","2018-07-03 03:15:23","2018-07-03 03:15:28","2018-07-03 03:15:33","2018-08-03 09:00:00","2018-09-03 10:15:00"]),
"message": ["knock knock","who's there","Europe","Europe who?","No - you're a poo","knock knock","who's there","the KGB","the KGB who?","SLAP the KGB will ask q's!","Hello?","Hello, again?"]}
import pandas as pd
import numpy as np
#60mins in secs
thresh = 60*60
df = pd.DataFrame(data=d)
#Creating time delta from previous message
df["delta"] = df["created_at"].diff().fillna(0).dt.total_seconds()
#Normalising delta based on threshold as a flag for new convos
df["id"] = np.where(df["delta"] < thresh, 0, 1)
df = df.drop(["delta"], axis=1)
#Assigning ID's to each convo
for i in range(1, len(df)):
df.loc[i, 'id'] += df.loc[i-1, 'id']
print(df)
created_at message id
0 2018-07-03 02:12:33 knock knock 0
1 2018-07-03 02:12:35 who's there 0
2 2018-07-03 02:12:40 Europe 0
3 2018-07-03 02:12:45 Europe who? 0
4 2018-07-03 02:12:48 No - you're a poo 0
5 2018-07-03 03:15:17 knock knock 1
6 2018-07-03 03:15:20 who's there 1
7 2018-07-03 03:15:23 the KGB 1
8 2018-07-03 03:15:28 the KGB who? 1
9 2018-07-03 03:15:33 SLAP the KGB will ask q's! 1
10 2018-08-03 09:00:00 Hello? 2
11 2018-09-03 10:15:00 Hello, again? 3