Skip to content

BUG: arithmetic with reindex pow #32734

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

Merged
merged 5 commits into from
Mar 17, 2020
Merged
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ including other versions of pandas.
Fixed regressions
~~~~~~~~~~~~~~~~~
- Fixed regression in ``resample.agg`` when the underlying data is non-writeable (:issue:`31710`)
- Fixed regression in :class:`DataFrame` exponentiation with reindexing (:issue:`32685`)

.. _whatsnew_103.bug_fixes:

Expand Down
10 changes: 8 additions & 2 deletions pandas/core/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,13 +711,17 @@ def to_series(right):


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

if op is operator.pow or op is rpow:
# GH#32685 pow has special semantics for operating with null values
return False

if not isinstance(right, ABCDataFrame):
return False

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

if _should_reindex_frame_op(self, other, axis, default_axis, fill_value, level):
if _should_reindex_frame_op(
self, other, op, axis, default_axis, fill_value, level
):
return _frame_arith_method_with_reindex(self, other, op)

self, other = _align_method_FRAME(self, other, axis, flex=True, level=level)
Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/frame/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,3 +823,27 @@ def test_align_frame(self):
half = ts[::2]
result = ts + half.take(np.random.permutation(len(half)))
tm.assert_frame_equal(result, expected)


def test_pow_with_realignment():
# GH#32685 pow has special semantics for operating with null values
left = pd.DataFrame({"A": [0, 1, 2]})
right = pd.DataFrame(index=[0, 1, 2])

result = left ** right
expected = pd.DataFrame({"A": [np.nan, 1.0, np.nan]})
tm.assert_frame_equal(result, expected)


# TODO: move to tests.arithmetic and parametrize
def test_pow_nan_with_zero():
left = pd.DataFrame({"A": [np.nan, np.nan, np.nan]})
right = pd.DataFrame({"A": [0, 0, 0]})

expected = pd.DataFrame({"A": [1.0, 1.0, 1.0]})

result = left ** right
tm.assert_frame_equal(result, expected)

result = left["A"] ** right["A"]
tm.assert_series_equal(result, expected["A"])