Skip to content

BUG: fixed index power operation #14996

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

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ Performance Improvements
Bug Fixes
~~~~~~~~~

- Bug in ``Indexes`` Power numerical operations did not operate correctly (:issue:`14973`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug in Index power operations with with reversed operands

- Bug in ``TimedeltaIndex`` addition where overflow was being allowed without error (:issue:`14816`)
- Bug in ``DataFrame`` construction in which unsigned 64-bit integer elements were being converted to objects (:issue:`14881`)
- Bug in ``astype()`` where ``inf`` values were incorrectly converted to integers. Now raises error now with ``astype()`` for Series and DataFrames (:issue:`14265`)
Expand Down
4 changes: 3 additions & 1 deletion pandas/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3534,7 +3534,9 @@ def _evaluate_numeric_binop(self, other):
operator.sub, '__sub__', reversed=True)
cls.__mul__ = cls.__rmul__ = _make_evaluate_binop(
operator.mul, '__mul__')
cls.__pow__ = cls.__rpow__ = _make_evaluate_binop(
cls.__rpow__ = _make_evaluate_binop(
operator.pow, '__pow__', reversed=True)
cls.__pow__ = _make_evaluate_binop(
operator.pow, '__pow__')
cls.__mod__ = _make_evaluate_binop(
operator.mod, '__mod__')
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/indexes/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,21 @@ def test_numeric_compat(self):
for r, e in zip(result, expected):
tm.assert_index_equal(r, e)

#test power calculations both ways
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

model this after the existing code, IOW, use idx which is already define.

add a comment with this issue number.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your guidance. Changes implemented.

num_range = np.arange(1,11)
pow_range = 2.0**num_range
idxs = pd.Float64Index(range(1, 11))

expected_pow_values = pd.Float64Index(pow_range)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use expected and result

actual_pow_values = 2.0**idxs
tm.assert_index_equal(expected_pow_values, actual_pow_values)

pow_range_rev = num_range**2.0
expected_rev_pow_values = pd.Float64Index(pow_range_rev)
actual_rev_pow_values = idxs**2.0
tm.assert_index_equal(expected_rev_pow_values, actual_rev_pow_values)


def test_explicit_conversions(self):

# GH 8608
Expand Down