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`) diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index dd72ce30d290b..6837310ca8459 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -1015,8 +1015,9 @@ def _arith_method(self, other, op): if not is_integer(rstep) or not rstep: raise ValueError + # 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 4edec0bf95982..b964fb43f1560 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)