考虑计算未来(提前1天)日期,并将其添加到与源/目标合并:
from datetime import timedelta
...
df['Future_Time'] = df['Flight_Time'] + timedelta(days=1)
merged_df = pd.merge(df, df,
left_on = ['Destination', 'Future_Time'],
right_on = ['Origin', 'Flight_Time'],
suffixes=['_o','_d'])
假设您的飞行时间包含影响合并联接的时间组件,请将日期规格化为计算的未来日期之前的最后一个午夜,然后合并:
df['Normalized_Time'] = df['Flight_Time'].dt.normalize()
df['Future_Time'] = df['Normalized_Time'] + timedelta(days=1)
merged_df = pd.merge(df, df,
left_on = ['Destination', 'Future_Time'],
right_on = ['Origin', 'Normalized_Time'],
suffixes=['_o','_d'])
以上可能导致24-48小时配对问题。但从那里你可以根据24小时的时间跨度在更小的一组上过滤:
merged_df[merge_df['Flight_Time_d'] <= merge_df['Flight_Time_o'] + timedelta(days=1)]
或者一起:
merged_df = (pd.merge(df, df,
left_on = ['Destination', 'Future_Time'],
right_on = ['Origin', 'Normalized_Time'],
suffixes=['_o','_d'])
.query("Flight_Time_d <= Flight_Time_o + timedelta(days=1)")
)