max_group_id += 1
groups[begin, end] = found_common_group
return found_common_group

def my_grouper(date_time):
    return find_merged_group(date_time - 1, date_time + 1)
df['datetime'].apply(my_grouper)
grouped = df.groupby(df['datetime'].apply(my_grouper))
Pandas group by time windows