Skip to content

Commit 218e55f

Browse files
authored
BUG: RangeIndex arithmetic result.name (#43962)
1 parent ba64ab5 commit 218e55f

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ Numeric
411411
- Bug in :meth:`DataFrame.rank` treating missing values and extreme values as equal (for example ``np.nan`` and ``np.inf``), causing incorrect results when ``na_option="bottom"`` or ``na_option="top`` used (:issue:`41931`)
412412
- Bug in ``numexpr`` engine still being used when the option ``compute.use_numexpr`` is set to ``False`` (:issue:`32556`)
413413
- Bug in :class:`DataFrame` arithmetic ops with a subclass whose :meth:`_constructor` attribute is a callable other than the subclass itself (:issue:`43201`)
414+
- Bug in arithmetic operations involving :class:`RangeIndex` where the result would have the incorrect ``name`` (:issue:`43962`)
414415
-
415416

416417
Conversion

pandas/core/indexes/range.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -885,9 +885,8 @@ def _arith_method(self, other, op):
885885
step = op
886886

887887
# TODO: if other is a RangeIndex we may have more efficient options
888-
other = extract_array(other, extract_numpy=True, extract_range=True)
889-
890-
left, right = self, other
888+
right = extract_array(other, extract_numpy=True, extract_range=True)
889+
left = self
891890

892891
try:
893892
# apply if we have an override
@@ -907,7 +906,8 @@ def _arith_method(self, other, op):
907906
rstart = op(left.start, right)
908907
rstop = op(left.stop, right)
909908

910-
result = type(self)(rstart, rstop, rstep, name=self.name)
909+
res_name = ops.get_op_result_name(self, other)
910+
result = type(self)(rstart, rstop, rstep, name=res_name)
911911

912912
# for compat with numpy / Int64Index
913913
# even if we can represent as a RangeIndex, return

pandas/tests/arithmetic/test_numeric.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -693,11 +693,10 @@ def test_mul_float_series(self, numeric_idx):
693693
tm.assert_series_equal(result, expected)
694694

695695
def test_mul_index(self, numeric_idx):
696-
# in general not true for RangeIndex
697696
idx = numeric_idx
698-
if not isinstance(idx, RangeIndex):
699-
result = idx * idx
700-
tm.assert_index_equal(result, idx ** 2)
697+
698+
result = idx * idx
699+
tm.assert_index_equal(result, idx ** 2)
701700

702701
def test_mul_datelike_raises(self, numeric_idx):
703702
idx = numeric_idx
@@ -1090,11 +1089,11 @@ def test_ufunc_compat(self, holder):
10901089
box = Series if holder is Series else Index
10911090

10921091
if holder is RangeIndex:
1093-
idx = RangeIndex(0, 5)
1092+
idx = RangeIndex(0, 5, name="foo")
10941093
else:
1095-
idx = holder(np.arange(5, dtype="int64"))
1094+
idx = holder(np.arange(5, dtype="int64"), name="foo")
10961095
result = np.sin(idx)
1097-
expected = box(np.sin(np.arange(5, dtype="int64")))
1096+
expected = box(np.sin(np.arange(5, dtype="int64")), name="foo")
10981097
tm.assert_equal(result, expected)
10991098

11001099
@pytest.mark.parametrize("holder", [Int64Index, UInt64Index, Float64Index, Series])
@@ -1212,6 +1211,8 @@ class TestNumericArithmeticUnsorted:
12121211
def check_binop(self, ops, scalars, idxs):
12131212
for op in ops:
12141213
for a, b in combinations(idxs, 2):
1214+
a = a._rename("foo")
1215+
b = b._rename("bar")
12151216
result = op(a, b)
12161217
expected = op(Int64Index(a), Int64Index(b))
12171218
tm.assert_index_equal(result, expected)

0 commit comments

Comments
 (0)