From a6c9458d88f2bd5245a0c14042e9dd218ef422ba Mon Sep 17 00:00:00 2001 From: Kamal Kamalaldin Date: Tue, 27 Dec 2016 00:29:03 -0500 Subject: [PATCH 1/2] BUG: fixed index power operation * The power operation on a range of indexes was fixed. See issue #14973 --- doc/source/whatsnew/v0.20.0.txt | 1 + pandas/indexes/base.py | 4 +++- pandas/tests/indexes/test_numeric.py | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index 0873e4b34b0b1..2d31300992363 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -282,6 +282,7 @@ Performance Improvements Bug Fixes ~~~~~~~~~ +- Bug in ``Indexes`` Power numerical operations did not operate correctly (:issue:`14973`) - 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`) diff --git a/pandas/indexes/base.py b/pandas/indexes/base.py index 1cc546629589d..1fce5ed17c8cf 100644 --- a/pandas/indexes/base.py +++ b/pandas/indexes/base.py @@ -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__') diff --git a/pandas/tests/indexes/test_numeric.py b/pandas/tests/indexes/test_numeric.py index b362c9716b672..7c59ace81a7db 100644 --- a/pandas/tests/indexes/test_numeric.py +++ b/pandas/tests/indexes/test_numeric.py @@ -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 + 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) + 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 From 14c47a8e34e21f0005b705f35ce0376dfdb2f9b8 Mon Sep 17 00:00:00 2001 From: Kamal Kamalaldin Date: Tue, 27 Dec 2016 17:50:23 -0500 Subject: [PATCH 2/2] DOC: updated wording of a previous fix * Changed variable names to follow code standards * Changed changelog to correct wording * Added Github issue in comment to follow documentation standard --- doc/source/whatsnew/v0.20.0.txt | 2 +- pandas/tests/indexes/test_numeric.py | 20 +++++++------------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index 2d31300992363..8bd794983375d 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -282,7 +282,7 @@ Performance Improvements Bug Fixes ~~~~~~~~~ -- Bug in ``Indexes`` Power numerical operations did not operate correctly (:issue:`14973`) +- Bug in ``Index`` power operations with reversed operands(:issue:`14973`) - 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`) diff --git a/pandas/tests/indexes/test_numeric.py b/pandas/tests/indexes/test_numeric.py index 7c59ace81a7db..f7f072d5b5d2a 100644 --- a/pandas/tests/indexes/test_numeric.py +++ b/pandas/tests/indexes/test_numeric.py @@ -105,21 +105,15 @@ def test_numeric_compat(self): for r, e in zip(result, expected): tm.assert_index_equal(r, e) - #test power calculations both ways - 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) - actual_pow_values = 2.0**idxs - tm.assert_index_equal(expected_pow_values, actual_pow_values) + # test power calculations both ways, GH 14973 + expected = pd.Float64Index(2.0**idx.values) + result = 2.0**idx + tm.assert_index_equal(result, expected) - 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) + expected = pd.Float64Index(idx.values**2.0) + result = idx**2.0 + tm.assert_index_equal(result, expected) - def test_explicit_conversions(self): # GH 8608