这里的想法是将所有寄存器打开时间标记为+1,并将所有寄存器关闭时间标记为-1。然后按时间排序,并在+/- 1值上执行累加总和,以在给定时间打开计数。
# initialize interval start times as 1, end times as -1start_times= df.assign(time=df['time'] - pd.Timedelta(hours=2), count=1)all_times = start_times.append(df.assign(count=-1), ignore_index=True)# sort by time and perform a cumulative sum get the count of overlaps at a given time# (subtract 1 since you don't want to include the current value in the overlap)all_times = all_times.sort_values(by='time')all_times['count'] = all_times['count'].cumsum() - 1# reassign to the original dataframe, keeping only the original timesdf['count'] = all_times['count']
结果输出:
time count0 2013-01-01 12:56:00 11 2013-01-01 12:00:12 12 2013-01-01 10:34:28 23 2013-01-01 09:34:54 14 2013-01-01 08:34:55 05 2013-01-01 16:35:19 06 2013-01-01 16:35:30 1



