Python时间序列:统计30分钟时间窗口内同时发生的事件

Python时间序列:统计30分钟时间窗口内同时发生的事件,python,pandas,dataframe,count,python-datetime,Python,Pandas,Dataframe,Count,Python Datetime,例如,我的dataframe df存储机器工作时间的数据 start_time end_time machine 2019-01-01 00:00:03 2019-01-01 17:10:03 A 2019-01-01 00:31:03 2019-01-01 18:11:03 B 2019-01-01 12:00:00 2019-01-01 13:08:03

例如,我的dataframe df存储机器工作时间的数据

start_time                  end_time                  machine
2019-01-01 00:00:03         2019-01-01 17:10:03       A
2019-01-01 00:31:03         2019-01-01 18:11:03       B
2019-01-01 12:00:00         2019-01-01 13:08:03       C
我想知道在任何30分钟的时间窗内,有多少台机器同时工作,也就是说

time_window_start       machine_count
2019-01-01 00:00:00     1
2019-01-01 00:30:00     2
        ...             ...
2019-01-01 12:00:00     3
        ...             ...
2019-01-01 13:30:00     2           
我首先创建了一个包含时间引用的数据帧:

ref_date_range = pd.date_range(start='31/1/2018 00:00:00', end='1/1/2020 23:30:00', freq='30Min')
ref_df = pd.DataFrame(np.random.randint(1, 20, (ref_date_range.shape[0], 1)))
ref_df.index = ref_date_range  
但如何匹配两个数据帧,包括不同数量的机器?

我在这里找到了解决方案: