在 python 中获取最近合并前后 30 分钟的记录

get the records before and after the nearest merge by 30 minutes in python

我在 csv 文件中有两个数据框。第一个数据描述了交通事件(df1),第二个数据有每 15 分钟的交通记录数据(df2)。我想根据最近的时间在它们之间合并。我使用 python pandas_merge_asof 得到了最近的匹配。但我想从交通记录数据中获取比赛前后 30 分钟的记录。我想加入最接近交通数据时间的事件。如果事件发生 14:02:00,它将与记录在 14:00:00

的交通日期合并

例如:

1- 事件数据

Date                detector_id              Inident_type
09/30/2015 8:00:00      1                      crash
09/30/2015 8:02:00      1                    congestion
04/22/2014 15:30:00     9                    congestion
04/22/2014 15:33:00     9                  Emergency vehicle 

    

2 - 交通数据

Date              detector_id               traffic_volume
09/30/2015 7:30:00      1                         55
09/30/2015 7:45:00      1                         45
09/30/2015 8:00:00      1                         60
09/30/2015 8:15:00      1                         200
09/30/2015 8:30:00      1                         70
04/22/2014 15:00:00     9                         15
04/22/2014 15:15:00     9                          7
04/22/2014 15:30:00     9                         50
04/22/2014 15:45:00     9                         11
04/22/2014 16:00:00     9                         7

2-想要的table

Date              detector_id               traffic_volume     Incident_type
09/30/2015 7:30:00      1                         55                  NA
09/30/2015 7:45:00      1                         45                  NA
09/30/2015 8:00:00      1                         60                Crash
09/30/2015 8:00:00      1                         60              congestion   
09/30/2015 8:15:00      1                         200                 NA
09/30/2015 8:30:00      1                         70                  NA
04/22/2014 15:00:00     9                         15                  NA
04/22/2014 15:15:00     9                          7                  NA
04/22/2014 15:30:00     9                         50              Congestion
04/22/2014 15:30:00     9                         50        Emergency vehicle   
04/22/2014 15:45:00     9                         11                  NA
04/22/2014 16:00:00     9                         7                   NA

我使用的代码如下

Merge = pd.merge_asof(df2, df1, left_index = True, right_index = True, allow_exact_maches = False,
on='Date', by='detector_id', direction='nearest')

但它给了我这个 table。

Date              detector_id               traffic_volume     Incident_type
09/30/2015 8:00:00      1                         60                Crash
04/22/2014 15:30:00     9                         50              Congestion

我想知道事发前后的情况

有什么想法吗? 谢谢。

*如果我这样问错了,请告诉我。

如果有人遇到同样的问题并想使用pandas.merge_asof进行合并,则必须使用容差功能。此功能可帮助您调整两个数据集之间的时间差异。

但是您可能会遇到两个与Timedelta 和排序索引相关的问题。所以 Timedelta 的解决方案是将时间转换为日期时间,如下所示:

df1.Date = pd.to_datetime(df1.Date)
df2.Date = pd.to_datetime(df2.Date)

以及您需要的排序索引在您的主代码中应用排序,如下所示:

x = pd.merge_asof(df1.sort_values('Date'), #sort_values fix the error"left Key must be sorted"
                  df2.sort_values('Date'), 
                  on = 'Date', 
                  by = 'Detector_id',
                  direction = 'backward', 
                  tolerance =pd.Timedelta('45 min'))

方向可能最近,在我的情况下,select 45 分钟内比赛记录前后的所有记录都一致。

方向可以向后,将在精确匹配或最接近匹配后的 45 分钟内合并所有记录 Forward 将 select 精确或最接近匹配前 45 分钟内的所有记录。

谢谢,希望这对以后的任何人都有帮助。