Skip to content
Advertisement

Pandas: Unable to merge on two date columns

I have two dataframes that look like:

df1:

JavaScript

df2:

JavaScript

Both date columns have been made using the pd.to_datetime() method, and they both supposedly have <M8[ns] data types when using df1.Date.dtype and df2.Date.dtype. However when trying to merge the dataframes with pd.merge(df,hpi,how="left",on="Date") I get the error:
ValueError: You are trying to merge on object and datetime64[ns] columns. If you wish to proceed you should use pd.concat

Advertisement

Answer

Try to convert the Date column of df1 to a datetime64

Check dtypes first:

JavaScript

Convert and merge:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement