Skip to content

Commit c871284

Browse files
authored
FIX RangeIndex rsub by constant (#53288)
* FIX RangeIndex rsub by constant * changelog added * resolved conversations; take neg only for rsub
1 parent eb4658f commit c871284

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ Timezones
337337

338338
Numeric
339339
^^^^^^^
340+
- Bug in :class:`RangeIndex` setting ``step`` incorrectly when being the subtrahend with minuend a numeric value (:issue:`53255`)
340341
- Bug in :meth:`Series.corr` and :meth:`Series.cov` raising ``AttributeError`` for masked dtypes (:issue:`51422`)
341342
- 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`)
342343
- Bug in :meth:`DataFrame.corrwith` raising ``NotImplementedError`` for pyarrow-backed dtypes (:issue:`52314`)

pandas/core/indexes/range.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1015,8 +1015,9 @@ def _arith_method(self, other, op):
10151015
if not is_integer(rstep) or not rstep:
10161016
raise ValueError
10171017

1018+
# GH#53255
10181019
else:
1019-
rstep = left.step
1020+
rstep = -left.step if op == ops.rsub else left.step
10201021

10211022
with np.errstate(all="ignore"):
10221023
rstart = op(left.start, right)

pandas/tests/indexes/ranges/test_range.py

+6
Original file line numberDiff line numberDiff line change
@@ -612,3 +612,9 @@ def test_sort_values_key(self):
612612

613613
def test_cast_string(self, dtype):
614614
pytest.skip("casting of strings not relevant for RangeIndex")
615+
616+
def test_range_index_rsub_by_const(self):
617+
# GH#53255
618+
result = 3 - RangeIndex(0, 4, 1)
619+
expected = RangeIndex(3, -1, -1)
620+
tm.assert_index_equal(result, expected)

0 commit comments

Comments
 (0)