diff --git a/pandas/core/ops/__init__.py b/pandas/core/ops/__init__.py index 4f027843fbac1..4e85a3ff104c4 100644 --- a/pandas/core/ops/__init__.py +++ b/pandas/core/ops/__init__.py @@ -606,6 +606,9 @@ def _construct_result(left, result, index, name, dtype=None): """ out = left._constructor(result, index=index, dtype=dtype) out = out.__finalize__(left) + + # Set the result's name after __finalize__ is called because __finalize__ + # would set it back to self.name out.name = name return out @@ -660,14 +663,6 @@ def wrapper(self, other): res_name = get_op_result_name(self, other) - # TODO: shouldn't we be applying finalize whenever - # not isinstance(other, ABCSeries)? - finalizer = ( - lambda x: x.__finalize__(self) - if isinstance(other, (np.ndarray, ABCIndexClass)) - else x - ) - if isinstance(other, ABCDataFrame): # pragma: no cover # Defer to DataFrame implementation; fail early return NotImplemented @@ -680,13 +675,7 @@ def wrapper(self, other): res_values = comparison_op(lvalues, rvalues, op) - result = self._constructor(res_values, index=self.index) - result = finalizer(result) - - # Set the result's name after finalizer is called because finalizer - # would set it back to self.name - result.name = res_name - return result + return _construct_result(self, res_values, index=self.index, name=res_name) wrapper.__name__ = op_name return wrapper @@ -703,14 +692,6 @@ def wrapper(self, other): self, other = _align_method_SERIES(self, other, align_asobject=True) res_name = get_op_result_name(self, other) - # TODO: shouldn't we be applying finalize whenever - # not isinstance(other, ABCSeries)? - finalizer = ( - lambda x: x.__finalize__(self) - if not isinstance(other, (ABCSeries, ABCIndexClass)) - else x - ) - if isinstance(other, ABCDataFrame): # Defer to DataFrame implementation; fail early return NotImplemented @@ -719,8 +700,7 @@ def wrapper(self, other): rvalues = extract_array(other, extract_numpy=True) res_values = logical_op(lvalues, rvalues, op) - result = self._constructor(res_values, index=self.index, name=res_name) - return finalizer(result) + return _construct_result(self, res_values, index=self.index, name=res_name) wrapper.__name__ = op_name return wrapper