Home > Software engineering >  unexpected output with pandas merge_asof (missing rows)
unexpected output with pandas merge_asof (missing rows)

Time:09-28

I want to merge the two data frames based on the nearest timestamp, the case is as following

Time stamp  Value1  Time Stamp 2    Value 2
13:30   A   13:32   40
13:30   B       
13:30   C       
13:30   D       
13:30   E       
13:30   F       

the above is the dataframe where value 1 is (A,B,C,D,E) and value 2 is 40 with a different time stamp, i want to merge the 40 with (A,B,C,D,E) so i get the following result:

13:30   A   40
13:30   B   40
13:30   C   40
13:30   D   40
13:30   E   40
13:30   F   40

I have already used merge_asof method and i got this result

13:30   F   40

I used the following code:

df = pd.merge_asof(df_downtime,df_centerline,on='Time',direction='nearest',by='Desc Variable',allow_exact_matches=False)

CodePudding user response:

Although your example is ambiguous and not reproducible, if you obtained the result you provided it is likely that you have used an incorrect order of the parameters.

merges_asof is a left merge, so you should probably use:

df = pd.merge_asof(df_centerline, df_downtime,
                   on='Time', direction='nearest',
                   by='Desc Variable', allow_exact_matches=False)
  • Related