From e56b5fa876e82dd989cf24231c7a6d69267176ce Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 23 Sep 2019 18:38:33 -0700 Subject: [PATCH 1/2] CLN: unify __finalize__ treatment for Series ops --- pandas/core/ops/__init__.py | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/pandas/core/ops/__init__.py b/pandas/core/ops/__init__.py index 0c1e1e90c003b..f69d0c33e9bb8 100644 --- a/pandas/core/ops/__init__.py +++ b/pandas/core/ops/__init__.py @@ -660,14 +660,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 +672,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 +689,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 +697,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 From 9661e62bb282b198cd46cbdcefa055e324730d83 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 24 Sep 2019 09:01:21 -0700 Subject: [PATCH 2/2] comment --- pandas/core/ops/__init__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/core/ops/__init__.py b/pandas/core/ops/__init__.py index f69d0c33e9bb8..2d0ed6805141c 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