Question: I'm having trouble with specifying a lambda function. I would like to have something like the lambda below, but not quite. The code should compare a rejected_time with any paid_out_time within the group and return True, if a rejected_time occurs within 5 minutes after any paid_out_time. f = lambda x: ((x['rejected_time'].dropna() - x['paid_out_time'].dropna()).between(pd.Timedelta(0), pd.Timedelta(minutes=5))) Using x['paid_out_time'].min() generates about 100k True values, but dropping .min() results in dramatic reduction. I can't figure out how to use all paid_out_times in the comparison against a row-wise rejected_time and see if the rejected time occurs 0-5minutes after the paid_out_time. I've been testing this code: cols = ['paid_out_time', 'rejected_time'] df[cols] = df[cols].apply(pd.to_datetime, errors='coerce') f = lambda x: ((x['rejected_time'].dropna() - x['paid_out_time'].dropna().min()).between(pd.Timedelta(0), pd.Timedelta(minutes=5))) df['paid_out_auto_rejection'] = df.groupby('personal_id', group_keys=False).apply(f).astype(int)  Here's some test data:    personal_id application_id rejected_time paid_out_time expected     26A 1ab 2022-09-12 09:20:40.592 NaT 1   26A 1ab 2022-08-23 07:40:03.447463 NaT 0   26A 1ab 2022-08-02 23:16:59.545392 NaT 1   26A 1ab 2022-08-02 23:16:59.545392 NaT 1   26A 1ab 2022-09-12 09:20:40.592000 2022-08-02 23:16:59.545392 1   26A 1ab 2022-09-02 18:33:42.226000 NaT 0   26A 8f0 2022-09-12 09:20:40.592000 NaT 1   26A 8f0 2022-09-12 09:20:40.592000 NaT 1   26A 8f0 NaT 2022-09-12 09:20:40.592 0   26A 8f0 2022-09-12 09:21:08.604000 NaT 1   26A 8f0 2022-09-22 08:27:45.693060 NaT 0   

Answer: EDIT: For improve performance is used merge_asof with tolerance parameter: cols = ['paid_out_time', 'rejected_time'] df[cols] = df[cols].apply(pd.to_datetime, errors='coerce') df1 = df[['personal_id','application_id','rejected_time']].reset_index() df2 = df[['personal_id','application_id','paid_out_time']] df3 = pd.merge_asof(df1.sort_values('rejected_time').dropna(subset=['rejected_time']), df2.sort_values('paid_out_time').dropna(subset=['paid_out_time']), left_on='rejected_time', right_on='paid_out_time', by='personal_id', direction="nearest", tolerance=pd.Timedelta("5Min") ).set_index('index') df['new'] = (df3['rejected_time'].sub(df3['paid_out_time']).notna() .reindex(df1.index, fill_value=0) .astype(int))   print (df) personal_id application_id rejected_time \ 0 26A 1ab 2022-09-12 09:20:40.592000 1 26A 1ab 2022-08-23 07:40:03.447463 2 26A 1ab 2022-08-02 23:16:59.545392 3 26A 1ab 2022-08-02 23:16:59.545392 4 26A 1ab 2022-09-12 09:20:40.592000 5 26A 1ab 2022-09-02 18:33:42.226000 6 26A 8f0 2022-09-12 09:20:40.592000 7 26A 8f0 2022-09-12 09:20:40.592000 8 26A 8f0 NaT 9 26A 8f0 2022-09-12 09:21:08.604000 10 26A 8f0 2022-09-22 08:27:45.693060 paid_out_time new 0 NaT 1 1 NaT 0 2 NaT 1 3 NaT 1 4 2022-08-02 23:16:59.545392 1 5 NaT 0 6 NaT 1 7 NaT 1 8 2022-09-12 09:20:40.592000 0 9 NaT 1 10 NaT 0  If need compare all non missing values use numpy broadcasting: cols = ['paid_out_time', 'rejected_time'] df[cols] = df[cols].apply(pd.to_datetime, errors='coerce') def f(x): arr = x['rejected_time'].dropna().to_numpy() - x['paid_out_time'].dropna().to_numpy()[:, None] m = (arr >= pd.Timedelta(0)) & (arr <= pd.Timedelta(minutes=5)) x.loc[x['rejected_time'].notna(), 'new'] = np.any(m, axis=0) return x out = (df.groupby('personal_id', group_keys=False).apply(f) .fillna({'new':False}).astype({'new':int}))   print (out) personal_id application_id rejected_time \ 0 26A 1ab 2022-09-12 09:20:40.592000 1 26A 1ab 2022-08-23 07:40:03.447463 2 26A 1ab 2022-08-02 23:16:59.545392 3 26A 1ab 2022-08-02 23:16:59.545392 4 26A 1ab 2022-09-12 09:20:40.592000 5 26A 1ab 2022-09-02 18:33:42.226000 6 26A 8f0 2022-09-12 09:20:40.592000 7 26A 8f0 2022-09-12 09:20:40.592000 8 26A 8f0 NaT 9 26A 8f0 2022-09-12 09:21:08.604000 10 26A 8f0 2022-09-22 08:27:45.693060 paid_out_time new 0 NaT 1 1 NaT 0 2 NaT 1 3 NaT 1 4 2022-08-02 23:16:59.545392 1 5 NaT 0 6 NaT 1 7 NaT 1 8 2022-09-12 09:20:40.592000 0 9 NaT 1 10 NaT 0