Skip to content

Commit c251196

Browse files
committed
Refactor DataFrame and Series methods to enhance attribute finalization logic
1 parent e342a6e commit c251196

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

pandas/core/frame.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8207,10 +8207,6 @@ def to_series(right):
82078207
level=level,
82088208
)
82098209
right = left._maybe_align_series_as_frame(right, axis)
8210-
8211-
# Ensure attributes are consistent between the aligned and original objects
8212-
if right.attrs != getattr(other, "attrs", {}):
8213-
right.attrs = getattr(other, "attrs", {}).copy()
82148210
return left, right
82158211

82168212
def _maybe_align_series_as_frame(self, series: Series, axis: AxisInt):
@@ -8239,7 +8235,7 @@ def _maybe_align_series_as_frame(self, series: Series, axis: AxisInt):
82398235
index=self.index,
82408236
columns=self.columns,
82418237
dtype=rvalues.dtype,
8242-
)
8238+
).__finalize__(series)
82438239

82448240
def _flex_arith_method(
82458241
self, other, op, *, axis: Axis = "columns", level=None, fill_value=None
@@ -8285,13 +8281,13 @@ def _construct_result(self, result, other=None) -> DataFrame:
82858281
-------
82868282
DataFrame
82878283
"""
8288-
if not getattr(self, "attrs", None) and getattr(other, "attrs", None):
8289-
self.__finalize__(other)
82908284
out = self._constructor(result, copy=False).__finalize__(self)
82918285
# Pin columns instead of passing to constructor for compat with
82928286
# non-unique columns case
82938287
out.columns = self.columns
82948288
out.index = self.index
8289+
if not getattr(self, "attrs", None) and getattr(other, "attrs", None):
8290+
out = out.__finalize__(other)
82958291
return out
82968292

82978293
def __divmod__(self, other) -> tuple[DataFrame, DataFrame]:

pandas/core/series.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5970,12 +5970,12 @@ def _construct_result(
59705970
dtype = getattr(result, "dtype", None)
59715971
out = self._constructor(result, index=self.index, dtype=dtype, copy=False)
59725972
out = out.__finalize__(self)
5973+
if getattr(other, "attrs", None):
5974+
out.__finalize__(other)
59735975

59745976
# Set the result's name after __finalize__ is called because __finalize__
59755977
# would set it back to self.name
59765978
out.name = name
5977-
if not getattr(self, "attrs", None) and getattr(other, "attrs", None):
5978-
out.__finalize__(other)
59795979
return out
59805980

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

0 commit comments

Comments
 (0)