Skip to content

Commit b4ea300

Browse files
jbrockmendelproost
authored andcommitted
CLN: unify __finalize__ treatment for Series ops (pandas-dev#28590)
1 parent 54f966a commit b4ea300

File tree

1 file changed

+5
-25
lines changed

1 file changed

+5
-25
lines changed

pandas/core/ops/__init__.py

+5-25
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,9 @@ def _construct_result(left, result, index, name, dtype=None):
574574
"""
575575
out = left._constructor(result, index=index, dtype=dtype)
576576
out = out.__finalize__(left)
577+
578+
# Set the result's name after __finalize__ is called because __finalize__
579+
# would set it back to self.name
577580
out.name = name
578581
return out
579582

@@ -628,14 +631,6 @@ def wrapper(self, other):
628631

629632
res_name = get_op_result_name(self, other)
630633

631-
# TODO: shouldn't we be applying finalize whenever
632-
# not isinstance(other, ABCSeries)?
633-
finalizer = (
634-
lambda x: x.__finalize__(self)
635-
if isinstance(other, (np.ndarray, ABCIndexClass))
636-
else x
637-
)
638-
639634
if isinstance(other, ABCDataFrame): # pragma: no cover
640635
# Defer to DataFrame implementation; fail early
641636
return NotImplemented
@@ -648,13 +643,7 @@ def wrapper(self, other):
648643

649644
res_values = comparison_op(lvalues, rvalues, op)
650645

651-
result = self._constructor(res_values, index=self.index)
652-
result = finalizer(result)
653-
654-
# Set the result's name after finalizer is called because finalizer
655-
# would set it back to self.name
656-
result.name = res_name
657-
return result
646+
return _construct_result(self, res_values, index=self.index, name=res_name)
658647

659648
wrapper.__name__ = op_name
660649
return wrapper
@@ -671,14 +660,6 @@ def wrapper(self, other):
671660
self, other = _align_method_SERIES(self, other, align_asobject=True)
672661
res_name = get_op_result_name(self, other)
673662

674-
# TODO: shouldn't we be applying finalize whenever
675-
# not isinstance(other, ABCSeries)?
676-
finalizer = (
677-
lambda x: x.__finalize__(self)
678-
if not isinstance(other, (ABCSeries, ABCIndexClass))
679-
else x
680-
)
681-
682663
if isinstance(other, ABCDataFrame):
683664
# Defer to DataFrame implementation; fail early
684665
return NotImplemented
@@ -687,8 +668,7 @@ def wrapper(self, other):
687668
rvalues = extract_array(other, extract_numpy=True)
688669

689670
res_values = logical_op(lvalues, rvalues, op)
690-
result = self._constructor(res_values, index=self.index, name=res_name)
691-
return finalizer(result)
671+
return _construct_result(self, res_values, index=self.index, name=res_name)
692672

693673
wrapper.__name__ = op_name
694674
return wrapper

0 commit comments

Comments
 (0)