From aebc41933d8ada87a19dfb9258da2ed6b2a8b3cf Mon Sep 17 00:00:00 2001 From: Charlie-XIAO Date: Thu, 18 May 2023 22:37:18 +0800 Subject: [PATCH 1/3] FIX RangeIndex rsub by constant --- pandas/core/indexes/range.py | 3 +++ pandas/tests/indexes/ranges/test_range.py | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index dd72ce30d290b..03a03842ae8ad 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -1015,6 +1015,9 @@ def _arith_method(self, other, op): if not is_integer(rstep) or not rstep: raise ValueError + # GH #53255 + elif op in [operator.sub, ops.rsub]: + rstep = -left.step else: rstep = left.step diff --git a/pandas/tests/indexes/ranges/test_range.py b/pandas/tests/indexes/ranges/test_range.py index 4edec0bf95982..40d48ec2c463b 100644 --- a/pandas/tests/indexes/ranges/test_range.py +++ b/pandas/tests/indexes/ranges/test_range.py @@ -612,3 +612,9 @@ def test_sort_values_key(self): def test_cast_string(self, dtype): pytest.skip("casting of strings not relevant for RangeIndex") + + def test_range_index_rsub_by_const(self): + # GH #53255 + result = 3 - RangeIndex(0, 4, 1) + expected = RangeIndex(3, -1, -1) + tm.assert_index_equal(result, expected) From b31f17121872919858078370f9aa2c710070d5eb Mon Sep 17 00:00:00 2001 From: Charlie-XIAO Date: Thu, 18 May 2023 22:46:01 +0800 Subject: [PATCH 2/3] changelog added --- doc/source/whatsnew/v2.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index e1ac9e3309de7..2375617c19268 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -336,6 +336,7 @@ Timezones Numeric ^^^^^^^ +- Bug in :class:`RangeIndex` setting ``step`` incorrectly when being the subtrahend with minuend a numeric value (:issue:`53255`) - Bug in :meth:`Series.corr` and :meth:`Series.cov` raising ``AttributeError`` for masked dtypes (:issue:`51422`) - Bug in :meth:`Series.mean`, :meth:`DataFrame.mean` with object-dtype values containing strings that can be converted to numbers (e.g. "2") returning incorrect numeric results; these now raise ``TypeError`` (:issue:`36703`, :issue:`44008`) - Bug in :meth:`DataFrame.corrwith` raising ``NotImplementedError`` for pyarrow-backed dtypes (:issue:`52314`) From c07747141eb72a4962e7a07a4892630ee699c210 Mon Sep 17 00:00:00 2001 From: Charlie-XIAO Date: Thu, 18 May 2023 23:23:38 +0800 Subject: [PATCH 3/3] resolved conversations; take neg only for rsub --- pandas/core/indexes/range.py | 6 ++---- pandas/tests/indexes/ranges/test_range.py | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index 03a03842ae8ad..6837310ca8459 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -1015,11 +1015,9 @@ def _arith_method(self, other, op): if not is_integer(rstep) or not rstep: raise ValueError - # GH #53255 - elif op in [operator.sub, ops.rsub]: - rstep = -left.step + # GH#53255 else: - rstep = left.step + rstep = -left.step if op == ops.rsub else left.step with np.errstate(all="ignore"): rstart = op(left.start, right) diff --git a/pandas/tests/indexes/ranges/test_range.py b/pandas/tests/indexes/ranges/test_range.py index 40d48ec2c463b..b964fb43f1560 100644 --- a/pandas/tests/indexes/ranges/test_range.py +++ b/pandas/tests/indexes/ranges/test_range.py @@ -614,7 +614,7 @@ def test_cast_string(self, dtype): pytest.skip("casting of strings not relevant for RangeIndex") def test_range_index_rsub_by_const(self): - # GH #53255 + # GH#53255 result = 3 - RangeIndex(0, 4, 1) expected = RangeIndex(3, -1, -1) tm.assert_index_equal(result, expected)