如何使pandas merge_asof不仅包括所有事件

时间:2018-02-07 09:37:03

标签: python pandas datetime merge

我有一个数据集,包括班次的开始和结束:

schedule = pd.DataFrame({
    "start": pd.to_datetime(['2017-01-01 00:59:00', '2017-01-01 04:59:00', '2017-01-02 00:59:00', '2017-01-02 08:00:00', '2017-01-02 09:59:00']),
    "end": pd.to_datetime(['2017-01-01 09:59:00', '2017-01-01 18:00:00', '2017-01-02 09:59:00', '2017-01-02 15:59:00', '2017-01-02 18:00:00']),
    "employee": ['KC', 'IT', 'ED', 'NK', 'IT']
})

我希望最终能够知道有多少人(以及谁)在白天的特定时间工作。所以我尝试创建一个新的DataFrame,其时间戳为我想要的频率:

shifts = pd.DataFrame()
shifts['timestamp'] = pd.date_range(start=schedule.start.min(), end=schedule.end.max(), freq='2H')

和[有条件地]将其与我原来的时间表合并如下:

mrg = pd.merge_asof(shifts, schedule, left_on='timestamp', right_on='start').query('timestamp <= end')

结果如下:

timestamp                   employee   end         start
0 2017-01-01 00:59:00       KC 2017-01-01 09:59:00 2017-01-01 00:59:00   
1 2017-01-01 02:59:00       KC 2017-01-01 09:59:00 2017-01-01 00:59:00   
2 2017-01-01 04:59:00       IT 2017-01-01 18:00:00 2017-01-01 04:59:00   
3 2017-01-01 06:59:00       IT 2017-01-01 18:00:00 2017-01-01 04:59:00   
4 2017-01-01 08:59:00       IT 2017-01-01 18:00:00 2017-01-01 04:59:00   
5 2017-01-01 10:59:00       IT 2017-01-01 18:00:00 2017-01-01 04:59:00

现在我的问题是,当KC和IT都在工作时,2017-01-01 04:59:00和2017-01-01 09:59:00之间的时间戳,但是mrg数据帧只保留对应的行对IT。为什么我和我发送到merge_asof的参数中缺少什么?

1 个答案:

答案 0 :(得分:1)

您似乎需要将所有employeestimestamps相结合,然后添加参数by

from  itertools import product

t = pd.date_range(start=schedule.start.min(), end=schedule.end.max(), freq='2H')
e = schedule['employee'].unique().tolist()
shifts = pd.DataFrame(list(product(t,e)), columns=['timestamp','employee'])
print (shifts.head(10))
            timestamp employee
0 2017-01-01 00:59:00       KC
1 2017-01-01 00:59:00       IT
2 2017-01-01 00:59:00       ED
3 2017-01-01 00:59:00       NK
4 2017-01-01 02:59:00       KC
5 2017-01-01 02:59:00       IT
6 2017-01-01 02:59:00       ED
7 2017-01-01 02:59:00       NK
8 2017-01-01 04:59:00       KC
9 2017-01-01 04:59:00       IT
mrg = pd.merge_asof(shifts, 
                    schedule, 
                    left_on='timestamp', 
                    right_on='start', 
                    by='employee').query('timestamp <= end')
print (mrg)
             timestamp employee                 end               start
0  2017-01-01 00:59:00       KC 2017-01-01 09:59:00 2017-01-01 00:59:00
4  2017-01-01 02:59:00       KC 2017-01-01 09:59:00 2017-01-01 00:59:00
8  2017-01-01 04:59:00       KC 2017-01-01 09:59:00 2017-01-01 00:59:00
9  2017-01-01 04:59:00       IT 2017-01-01 18:00:00 2017-01-01 04:59:00
12 2017-01-01 06:59:00       KC 2017-01-01 09:59:00 2017-01-01 00:59:00
13 2017-01-01 06:59:00       IT 2017-01-01 18:00:00 2017-01-01 04:59:00
16 2017-01-01 08:59:00       KC 2017-01-01 09:59:00 2017-01-01 00:59:00
17 2017-01-01 08:59:00       IT 2017-01-01 18:00:00 2017-01-01 04:59:00
21 2017-01-01 10:59:00       IT 2017-01-01 18:00:00 2017-01-01 04:59:00
25 2017-01-01 12:59:00       IT 2017-01-01 18:00:00 2017-01-01 04:59:00
29 2017-01-01 14:59:00       IT 2017-01-01 18:00:00 2017-01-01 04:59:00
33 2017-01-01 16:59:00       IT 2017-01-01 18:00:00 2017-01-01 04:59:00
50 2017-01-02 00:59:00       ED 2017-01-02 09:59:00 2017-01-02 00:59:00
54 2017-01-02 02:59:00       ED 2017-01-02 09:59:00 2017-01-02 00:59:00
58 2017-01-02 04:59:00       ED 2017-01-02 09:59:00 2017-01-02 00:59:00
62 2017-01-02 06:59:00       ED 2017-01-02 09:59:00 2017-01-02 00:59:00
66 2017-01-02 08:59:00       ED 2017-01-02 09:59:00 2017-01-02 00:59:00
67 2017-01-02 08:59:00       NK 2017-01-02 15:59:00 2017-01-02 08:00:00
69 2017-01-02 10:59:00       IT 2017-01-02 18:00:00 2017-01-02 09:59:00
71 2017-01-02 10:59:00       NK 2017-01-02 15:59:00 2017-01-02 08:00:00
73 2017-01-02 12:59:00       IT 2017-01-02 18:00:00 2017-01-02 09:59:00
75 2017-01-02 12:59:00       NK 2017-01-02 15:59:00 2017-01-02 08:00:00
77 2017-01-02 14:59:00       IT 2017-01-02 18:00:00 2017-01-02 09:59:00
79 2017-01-02 14:59:00       NK 2017-01-02 15:59:00 2017-01-02 08:00:00
81 2017-01-02 16:59:00       IT 2017-01-02 18:00:00 2017-01-02 09:59:00