I am struggling with an obviously simple task and hope to find some help here!
I have something like the following DataFrame.
d = [
['2021-06-01 08:00:00',"A"],
['2021-06-01 09:00:00',"A"],
['2021-06-01 12:00:00',"B"],
['2021-06-01 13:00:00',"B"],
['2021-06-01 18:00:00',"B"],
['2021-06-01 19:00:00',"B"],
['2021-06-01 22:00:00',"C"],
['2021-06-01 23:00:00',"C"]]
df=pd.DataFrame(data=d, columns=("timestamp", "session"))
I want to identify time gaps greater than a threshold (e.g. 1h) within a session. time gaps between sessions shall not be considered
for this purpose, I used the .diff() method to locate the gaps.
df["timestamp"]= pd.to_datetime(df["timestamp"])
df["gap"]=df["timestamp"].diff().dt.seconds > 3600
the main task is to find a gap within a session and cut the session into pieces by renaming the parts/sessions with e.g. uuid.uuid4().
in the example, it would lead to new session names in row 5/6.
my approach was a for-iteration over the unique sessions, but I am not able to rename the parts, when I find a True in the "gap" column.
I would like to stay in the "pandas world" since this is a big data task.