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 1 commit
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
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
10 changes: 10 additions & 0 deletions pandas/tests/frame/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,3 +823,13 @@ 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)