jd_h2003
jd_h2003

Reputation: 304

Trying to merge 2 dataframes but receiving value error of merging object and int32 columns

I have been trying to address an issue mentioned here

I had been trying to use a list of dates to filter a dataframe, and a very gracious person was helping me, but now with the current code, I am receiving these errors. You are trying to merge on object and int32 columns. If you wish to proceed you should use pd.concat

# Assign a sequential number to each trading day
df_melt_test_percent = df_melt_test_percent.sort_index().assign(DayNumber=lambda x: range(len(x)))

# Find the indices of the FOMC_dates
tmp = pd.merge(
   df_FOMC_dates, df_melt_test_percent[['DayNumber']],
   left_on='FOMC_date', right_on='DayNumber'
)

# For each row, get the FOMC_dates ± 3 days
tmp['delta'] = tmp.apply(lambda _: range(-3, 4), axis=1)

tmp = tmp.explode('delta')
tmp['DayNumber'] += tmp['delta']

# Assemble the result
result = pd.merge(tmp, df_melt_test_percent, on='DayNumber')

Screenshots of dataframes:

enter image description here enter image description here

If anyone has any advice on how to fix this, it would be greatly appreciated.

EDIT #1: enter image description here

Upvotes: 0

Views: 243

Answers (1)

mozway
mozway

Reputation: 260590

The columns on which you want to merge are not the same types in both dataframes. Likely one is string the other integer. You should convert to the same type before merging. Assuming from the little bit you showed, before your merge, run:

tmp['DayNumber'] = tmp['DayNumber'].astype(int)

Alternatively:

df_melt_test_percent['DayNumber'] = df_melt_test_percent['DayNumber'].astype(str)

NB. This might not work as you did not provide a full example. Either search by yourself the right types or provide a reproducible example.

Upvotes: 1

Related Questions