diff --git a/pandas/core/internals.py b/pandas/core/internals.py index f553e1a02c9d6..957b293ac3156 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -1051,7 +1051,7 @@ def f(m, v, i): return [self.make_block(new_values)] - def coerce_to_target_dtype(self, other): + def coerce_to_target_dtype(self, other, force_coericion=False): """ coerce the current block to a dtype compat for other we will return a block, possibly object, and not raise @@ -1067,8 +1067,10 @@ def coerce_to_target_dtype(self, other): return self if self.is_bool or is_object_dtype(dtype) or is_bool_dtype(dtype): - # we don't upcast to bool - return self.astype(object) + if force_coericion and is_float_dtype(dtype) or is_integer_dtype(dtype): + return self.astype(dtype) + else: + return self.astype(object) elif ((self.is_float or self.is_complex) and (is_integer_dtype(dtype) or is_float_dtype(dtype))): @@ -1349,7 +1351,7 @@ def eval(self, func, other, errors='raise', try_cast=False, mgr=None): values, values_mask, other, other_mask = self._try_coerce_args( transf(values), other) except TypeError: - block = self.coerce_to_target_dtype(orig_other) + block = self.coerce_to_target_dtype(orig_other, True) return block.eval(func, orig_other, errors=errors, try_cast=try_cast, mgr=mgr)