Timo
Timo

Reputation: 513

Pandas looking for time as index and ignore date

I'va actually 2 dataframe :

  1. First one contain some information with date/time
  2. The second one contain other information with date/time

I need to combine the 2 dataframe by using nearest time as key (ignore date)

I need with a given time in the first dataframe to find the nearest time index (or value) in the second dataframe

I hope you understand

EDIT :

FROM CSV dataFrame 1 :

date;index
01/01/90 00:00:00;2
01/01/90 00:00:30;9

FROM CSV dataFrame 2 :

date;value
02/02/00 00:00:02;300

NEEDED :

date;value;index
02/02/00 00:00:02;300;2

Upvotes: 0

Views: 220

Answers (1)

jezrael
jezrael

Reputation: 862511

You can use reindex with method='nearest':

#new indexes with same dates, but different times
df1.index = pd.to_datetime(df1['date'].dt.strftime('%H:%M:%S'))
df2.index = pd.to_datetime(df2['date'].dt.strftime('%H:%M:%S'))
print (df1)
                                   date  index
date                                          
2017-02-20 00:00:00 1990-01-01 00:00:00      2
2017-02-20 00:30:00 1990-01-01 00:00:30      9

print (df2)
                                   date  value
date                                          
2017-02-20 00:02:00 2000-02-02 00:00:02    300

df3 = df1.reindex(df2.index, method='nearest')
#add values from df2
df = pd.concat([df3.drop('date', axis=1), df2], axis=1).reset_index(drop=True)
print (df)
   index                date  value
0      2 2000-02-02 00:00:02    300

Upvotes: 1

Related Questions