Skip to content

Commit cfccb68

Browse files
authored
BUG: arithmetic with reindex pow (#32734) (#32777)
1 parent d8a9cb7 commit cfccb68

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
@@ -679,13 +679,17 @@ def to_series(right):
679679

680680

681681
def _should_reindex_frame_op(
682-
left: "DataFrame", right, axis, default_axis: int, fill_value, level
682+
left: "DataFrame", right, op, axis, default_axis: int, fill_value, level
683683
) -> bool:
684684
"""
685685
Check if this is an operation between DataFrames that will need to reindex.
686686
"""
687687
assert isinstance(left, ABCDataFrame)
688688

689+
if op is operator.pow or op is rpow:
690+
# GH#32685 pow has special semantics for operating with null values
691+
return False
692+
689693
if not isinstance(right, ABCDataFrame):
690694
return False
691695

@@ -747,7 +751,9 @@ def _arith_method_FRAME(cls, op, special):
747751
@Appender(doc)
748752
def f(self, other, axis=default_axis, level=None, fill_value=None):
749753

750-
if _should_reindex_frame_op(self, other, axis, default_axis, fill_value, level):
754+
if _should_reindex_frame_op(
755+
self, other, op, axis, default_axis, fill_value, level
756+
):
751757
return _frame_arith_method_with_reindex(self, other, op)
752758

753759
other = _align_method_FRAME(self, other, axis)

pandas/tests/frame/test_arithmetic.py

+24
Original file line numberDiff line numberDiff line change
@@ -756,3 +756,27 @@ def test_frame_single_columns_object_sum_axis_1():
756756
result = df.sum(axis=1)
757757
expected = pd.Series(["A", 1.2, 0])
758758
tm.assert_series_equal(result, expected)
759+
760+
761+
def test_pow_with_realignment():
762+
# GH#32685 pow has special semantics for operating with null values
763+
left = pd.DataFrame({"A": [0, 1, 2]})
764+
right = pd.DataFrame(index=[0, 1, 2])
765+
766+
result = left ** right
767+
expected = pd.DataFrame({"A": [np.nan, 1.0, np.nan]})
768+
tm.assert_frame_equal(result, expected)
769+
770+
771+
# TODO: move to tests.arithmetic and parametrize
772+
def test_pow_nan_with_zero():
773+
left = pd.DataFrame({"A": [np.nan, np.nan, np.nan]})
774+
right = pd.DataFrame({"A": [0, 0, 0]})
775+
776+
expected = pd.DataFrame({"A": [1.0, 1.0, 1.0]})
777+
778+
result = left ** right
779+
tm.assert_frame_equal(result, expected)
780+
781+
result = left["A"] ** right["A"]
782+
tm.assert_series_equal(result, expected["A"])

0 commit comments

Comments
 (0)