diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index 4d83d1d78ee35..7ca4a1165c782 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -916,11 +916,13 @@ def _maybe_coerce_merge_keys(self): # Houston, we have a problem! # let's coerce to object if name in self.left.columns: - typ = lk.categories.dtype if is_categorical_dtype(lk) else object + cat = is_categorical_dtype(lk) + typ = lk.categories.dtype if cat else object self.left = self.left.assign( **{name: self.left[name].astype(typ)}) if name in self.right.columns: - typ = rk.categories.dtype if is_categorical_dtype(rk) else object + cat = is_categorical_dtype(rk) + typ = rk.categories.dtype if cat else object self.right = self.right.assign( **{name: self.right[name].astype(typ)})