I recently updated Pandas and noticed that it is no longer possible to merge DataFrames with mismatched dtypes. I have a script which relied on merging and seemed to work in the past despite having mismatched dtypes. I need to display to the user which columns in two dataframes are causing problems, so the user can then adjust the types accordingly. (Specifically, one dataframe is read in from a database and represents what is currently in the DB, while the second dataframe includes any changes/new data to be applied to the database. Once the the user finds the problem columns they can determine if the DataFrame that contains the changes was meant to change the database's type or whether there is an error in the changes). The following code appears to work, but I feel like pandas must have a built in better way to deal with this problem.
def get_mismatched_dtypes(self, df1, df2):
mismatch = {}
for key, val in df1.dtypes.iteritems():
if key in df2.dtypes and df2.dtypes[key] != val:
mismatch[key] = (f"df1:{val}, df2: {df2.dtypes[key]}")
print(mismatch)
return mismatch