Skip to content

Commit 3a80cf9

Browse files
committed
Refactor DataFrame and Series methods to improve attribute handling and finalize behavior
1 parent 1e6197c commit 3a80cf9

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

pandas/core/frame.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7875,19 +7875,13 @@ class diet
78757875
def _cmp_method(self, other, op):
78767876
axis: Literal[1] = 1 # only relevant for Series other case
78777877

7878-
if not getattr(self, "attrs", None) and getattr(other, "attrs", None):
7879-
self.__finalize__(other)
7880-
78817878
self, other = self._align_for_op(other, axis, flex=False, level=None)
78827879

78837880
# See GH#4537 for discussion of scalar op behavior
78847881
new_data = self._dispatch_frame_op(other, op, axis=axis)
78857882
return self._construct_result(new_data, other=other)
78867883

78877884
def _arith_method(self, other, op):
7888-
if not getattr(self, "attrs", None) and getattr(other, "attrs", None):
7889-
self.__finalize__(other)
7890-
78917885
if self._should_reindex_frame_op(other, op, 1, None, None):
78927886
return self._arith_method_with_reindex(other, op)
78937887

@@ -8107,6 +8101,9 @@ def _align_for_op(
81078101
left : DataFrame
81088102
right : Any
81098103
"""
8104+
if not getattr(self, "attrs", None) and getattr(other, "attrs", None):
8105+
self.__finalize__(other)
8106+
81108107
left, right = self, other
81118108

81128109
def to_series(right):

pandas/core/series.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5938,7 +5938,7 @@ def _construct_result(
59385938
self,
59395939
result: ArrayLike | tuple[ArrayLike, ArrayLike],
59405940
name: Hashable,
5941-
other=None,
5941+
other: AnyArrayLike | DataFrame | None = None,
59425942
) -> Series | tuple[Series, Series]:
59435943
"""
59445944
Construct an appropriately-labelled Series from the result of an op.
@@ -5947,14 +5947,13 @@ def _construct_result(
59475947
----------
59485948
result : ndarray or ExtensionArray
59495949
name : Label
5950+
other : Series, DataFrame or array-like, default None
59505951
59515952
Returns
59525953
-------
59535954
Series
59545955
In the case of __divmod__ or __rdivmod__, a 2-tuple of Series.
59555956
"""
5956-
if not getattr(self, "attrs", None) and getattr(other, "attrs", None):
5957-
self.__finalize__(other)
59585957
if isinstance(result, tuple):
59595958
# produced by divmod or rdivmod
59605959

@@ -5975,6 +5974,8 @@ def _construct_result(
59755974
# Set the result's name after __finalize__ is called because __finalize__
59765975
# would set it back to self.name
59775976
out.name = name
5977+
if not getattr(self, "attrs", None) and getattr(other, "attrs", None):
5978+
out.__finalize__(other)
59785979
return out
59795980

59805981
def _flex_method(self, other, op, *, level=None, fill_value=None, axis: Axis = 0):

0 commit comments

Comments
 (0)