Skip to content

Commit 53162c5

Browse files
jbrockmendelSeeminSyed
authored andcommitted
BUG: arithmetic with reindex pow (pandas-dev#32734)
1 parent ca93eed commit 53162c5

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

doc/source/whatsnew/v1.0.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ including other versions of pandas.
1616
Fixed regressions
1717
~~~~~~~~~~~~~~~~~
1818
- Fixed regression in ``resample.agg`` when the underlying data is non-writeable (:issue:`31710`)
19+
- Fixed regression in :class:`DataFrame` exponentiation with reindexing (:issue:`32685`)
1920

2021
.. _whatsnew_103.bug_fixes:
2122

pandas/core/ops/__init__.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -711,13 +711,17 @@ def to_series(right):
711711

712712

713713
def _should_reindex_frame_op(
714-
left: "DataFrame", right, axis, default_axis: int, fill_value, level
714+
left: "DataFrame", right, op, axis, default_axis: int, fill_value, level
715715
) -> bool:
716716
"""
717717
Check if this is an operation between DataFrames that will need to reindex.
718718
"""
719719
assert isinstance(left, ABCDataFrame)
720720

721+
if op is operator.pow or op is rpow:
722+
# GH#32685 pow has special semantics for operating with null values
723+
return False
724+
721725
if not isinstance(right, ABCDataFrame):
722726
return False
723727

@@ -779,7 +783,9 @@ def _arith_method_FRAME(cls, op, special):
779783
@Appender(doc)
780784
def f(self, other, axis=default_axis, level=None, fill_value=None):
781785

782-
if _should_reindex_frame_op(self, other, axis, default_axis, fill_value, level):
786+
if _should_reindex_frame_op(
787+
self, other, op, axis, default_axis, fill_value, level
788+
):
783789
return _frame_arith_method_with_reindex(self, other, op)
784790

785791
self, other = _align_method_FRAME(self, other, axis, flex=True, level=level)

pandas/tests/frame/test_arithmetic.py

+24
Original file line numberDiff line numberDiff line change
@@ -823,3 +823,27 @@ def test_align_frame(self):
823823
half = ts[::2]
824824
result = ts + half.take(np.random.permutation(len(half)))
825825
tm.assert_frame_equal(result, expected)
826+
827+
828+
def test_pow_with_realignment():
829+
# GH#32685 pow has special semantics for operating with null values
830+
left = pd.DataFrame({"A": [0, 1, 2]})
831+
right = pd.DataFrame(index=[0, 1, 2])
832+
833+
result = left ** right
834+
expected = pd.DataFrame({"A": [np.nan, 1.0, np.nan]})
835+
tm.assert_frame_equal(result, expected)
836+
837+
838+
# TODO: move to tests.arithmetic and parametrize
839+
def test_pow_nan_with_zero():
840+
left = pd.DataFrame({"A": [np.nan, np.nan, np.nan]})
841+
right = pd.DataFrame({"A": [0, 0, 0]})
842+
843+
expected = pd.DataFrame({"A": [1.0, 1.0, 1.0]})
844+
845+
result = left ** right
846+
tm.assert_frame_equal(result, expected)
847+
848+
result = left["A"] ** right["A"]
849+
tm.assert_series_equal(result, expected["A"])

0 commit comments

Comments
 (0)