Skip to content

BUG: metadata does not always propagate in binary operations #49931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ Styler
Metadata
^^^^^^^^
- Fixed metadata propagation in :meth:`DataFrame.corr` and :meth:`DataFrame.cov` (:issue:`28283`)
-
- Fixed metadata propagation in binary operations (:issue:`49931`)

Other
^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7484,7 +7484,7 @@ def _arith_method(self, other, op):
self, other = ops.align_method_FRAME(self, other, axis, flex=True, level=None)

new_data = self._dispatch_frame_op(other, op, axis=axis)
return self._construct_result(new_data)
return self._construct_result(new_data).__finalize__(other)

_logical_method = _arith_method

Expand Down
4 changes: 3 additions & 1 deletion pandas/core/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,9 @@ def _maybe_align_series_as_frame(frame: DataFrame, series: Series, axis: AxisInt
rvalues = rvalues.reshape(1, -1)

rvalues = np.broadcast_to(rvalues, frame.shape)
return type(frame)(rvalues, index=frame.index, columns=frame.columns)
return type(frame)(rvalues, index=frame.index, columns=frame.columns).__finalize__(
series
)


def flex_arith_method_FRAME(op):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -5950,7 +5950,7 @@ def _logical_method(self, other, op):

def _arith_method(self, other, op):
self, other = ops.align_method_SERIES(self, other)
return base.IndexOpsMixin._arith_method(self, other, op)
return base.IndexOpsMixin._arith_method(self, other, op).__finalize__(other)


Series._add_numeric_operations()
Expand Down
6 changes: 5 additions & 1 deletion pandas/tests/generic/test_finalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,10 +498,14 @@ def test_binops(request, args, annotate, all_binary_operators):
left, right = args
if annotate == "both" and isinstance(left, int) or isinstance(right, int):
return
elif annotate == "left" and isinstance(left, int):
return
elif annotate == "right" and isinstance(right, int):
return

if annotate in {"left", "both"} and not isinstance(left, int):
left.attrs = {"a": 1}
if annotate in {"left", "both"} and not isinstance(right, int):
if annotate in {"right", "both"} and not isinstance(right, int):
right.attrs = {"a": 1}

result = all_binary_operators(left, right)
Expand Down