Skip to content

Commit

Permalink
CLN: unify __finalize__ treatment for Series ops (#28590)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and jreback committed Sep 26, 2019
1 parent e7cb6b3 commit 53ad101
Showing 1 changed file with 5 additions and 25 deletions.
30 changes: 5 additions & 25 deletions pandas/core/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,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

Expand Down Expand Up @@ -628,14 +631,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
Expand All @@ -648,13 +643,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
Expand All @@ -671,14 +660,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
Expand All @@ -687,8 +668,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
Expand Down

0 comments on commit 53ad101

Please sign in to comment.