Question: The code below match arrival time between df_CAL_CRA and df_CRA that fall on the sameday, with a 10 min tolerance. However, I am trying to
The code below match arrival time between df_CAL_CRA and df_CRA that fall on the sameday, with a 10 min tolerance. However, I am trying to allow my code match the arrival time that falls on the next day to the closest time in a df_CRA. for example, if the wind leaves df_CAL_CRA on 01/01/2020 at 23:30:00 pm and arrived at df_CRA on 01/02/2020 at 01:20:00 am, I want my code to match the arrival time to the closest next day time in df_CRA. However, there is an error message say "The error message indicates that you are trying to merge two dataframes using merge_asof() with multiple keys on the right dataframe (df_CRA) but only one key on the left dataframe (df_CAL_CRA). merge_asof() only allows merging on a single key for the left dataframe." DO NOT USE ChatGTP
df_merged_CAL_CRA = pd.DataFrame()
if 'ArrivalTime' in df_CAL_CRA.columns: df_CAL_CRA['Date'] = df_CAL_CRA['ArrivalTime'].dt.date df_CAL_CRA['NextDay'] = df_CAL_CRA['Date'] + pd.DateOffset(days=1) df_CAL_CRA['NextDay'] = pd.to_datetime(df_CAL_CRA['NextDay']) df_CRA['Date'] = df_CRA['Time'].dt.date df_CAL_CRA = df_CAL_CRA.sort_values(['Date', 'ArrivalTime']) df_CRA = df_CRA.sort_values(['Date', 'Time'])
df_CAL_CRA.dropna(subset=['ArrivalTime'], inplace=True) df_merged_CAL_CRA = pd.merge_asof(df_CAL_CRA, df_CRA, left_on=['Date', 'ArrivalTime'], right_on=['Date', 'Time'], tolerance=pd.Timedelta('10 minutes')) # match next day arrival times next_day_match = pd.merge_asof(df_CAL_CRA, df_CRA, left_on=['NextDay', 'ArrivalTime'], right_on=['Date', 'Time'], tolerance=pd.Timedelta('10 minutes')) df_merged_CAL_CRA = pd.concat([df_merged_CAL_CRA, next_day_match]) df_merged_CAL_CRA.to_excel(writer, sheet, index=False)
for s in sheets: process(s) writer.save()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
