you are trying to merge on datetime64[ns] and object columns. if you wish to proceed you should use pd.concat in python

To merge on columns with different data types in pandas, we can convert one column to match the data type of the other column. In this case, we can convert the datetime64[ns] column to object using the astype() method. Then, we can use pd.concat() to join the two columns together. Here's an example:

main.py
import pandas as pd

# create sample dataframes
df1 = pd.DataFrame({
    'date': pd.date_range('20220101', periods=3),
    'value': [1, 2, 3]
})

df2 = pd.DataFrame({
    'date_str': ['2022-01-01', '2022-01-02', '2022-01-03'],
    'value': [4, 5, 6]
})

# convert datetime64[ns] column to object
df1['date'] = df1['date'].astype(str)

# concatenate dataframes on matching columns
df = pd.concat([df1.set_index('date'), df2.set_index('date_str')], axis=1, join='inner')

# reset index
df = df.reset_index()

print(df)
517 chars
24 lines

Output:

main.py
        index  value  value
0  2022-01-01      1      4
1  2022-01-02      2      5
2  2022-01-03      3      6
112 chars
5 lines

Note that we converted the date column to a string using the astype() method before joining the two dataframes together. We then used pd.concat() to join the two dataframes, setting the axis parameter to 1 to join along the columns, and join parameter to inner to perform an inner join. Finally, we reset the index using reset_index() to obtain the desired output.

related categories

gistlibby LogSnag