diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 97ee96d8be25d..4fb68b1f90288 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -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 ^^^^^ diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 0144aefedaa5f..152965553480e 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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 diff --git a/pandas/core/ops/__init__.py b/pandas/core/ops/__init__.py index af27ff67599ac..e2f9150c918eb 100644 --- a/pandas/core/ops/__init__.py +++ b/pandas/core/ops/__init__.py @@ -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): diff --git a/pandas/core/series.py b/pandas/core/series.py index 48bc07ca022ee..ee50e999cbc3f 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -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() diff --git a/pandas/tests/generic/test_finalize.py b/pandas/tests/generic/test_finalize.py index a7551af68bc2b..50a7b1b5abe90 100644 --- a/pandas/tests/generic/test_finalize.py +++ b/pandas/tests/generic/test_finalize.py @@ -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)