Just to put the previous answer into perspective. Shubham Sharma's 'magic' line worked for me in the following context:
- Convert the
Date column to pandas.Timestamp object using the appropriate input format, so as to be able to apply the normalize() function to it - if it is not in this format, an exception will be raised.
- Set the
Date column as index.
- Use Shubham Sharma's 'magic' line from the previous answer.
The code might look something like this:
import pandas as pd
df1 = pd.DataFrame([['2019-08-26 13:00:00', 'a', 1],
['2019-08-26 13:30:00', 'b', 2],
['2019-08-26 14:00:00', 'c', 3],
['2019-08-26 14:30:00', 'd', 4],
['2019-08-26 15:00:00', 'e', 5]], columns=['Date', 'A', 'B'])
df1['Date'] = pd.to_datetime(df1['Date'].astype(str), format='%Y-%m-%d %H:%M:%S')
df1.set_index('Date', inplace=True)
df2 = pd.DataFrame([['2019-08-25', 'X'], ['2019-08-26', 'Y'], ['2019-08-27', 'Z']], columns=['Date', 'A'])
df2['Date'] = pd.to_datetime(df2['Date'].astype(str), format='%Y-%m-%d %H:%M:%S')
df2.set_index('Date', inplace=True)
# Now comes Shubham Sharma's magic line
df3 = df1.assign(key=df1.index.normalize()).merge(df2, left_on='key', right_index=True, how='left').drop('key', 1)
# Set column names (except the index) to A, B and C
df3.columns = ['A', 'B', 'C']