I'm trying to identify individuals who have events before or after events of their first occurrence of an event of a specific type.
For example, I'm interested in events of type 'y'. I want to know which individuals have had events of types 'x', 'z', 'p' etc. either before or after the first time they are linked to events of type 'y'.
An example input dataframe would be:
df=pd.DataFrame({'IDs':['a','a','a','b','b','c'],'Event_types':['x','y','z','p','y','x'],'date':['2020-01-01','2020-01-06','2020-01-11','2020-01-11','2020-01-14','2020-01-27']})
Giving:
IDs Event_types date
0 a x 2020-01-01
1 a y 2020-01-06
2 a z 2020-01-11
3 b p 2020-01-11
4 b y 2020-01-14
5 c x 2020-01-27
If I'm focussing on events of type 'y then desired output would then be
IDs Event_type_x_before Event_type_x_after Event_type_z_before Event_type_z_after
0 a 1 0 0 1
1 b 0 0 0 0
2 c 0 1 0 0
The best solution I've found is Pandas count the number of times an event has occurred in last n days by group which is close to what I want but it runs too slowly for the size of the dataframe and also only deals with events in the past (or future if the timedelta was modified). Any help would be much appreciated.