Skip to content

BUG: RangeIndex arithmetic result.name #43962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ Numeric
- 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`)
- Bug in ``numexpr`` engine still being used when the option ``compute.use_numexpr`` is set to ``False`` (:issue:`32556`)
- Bug in :class:`DataFrame` arithmetic ops with a subclass whose :meth:`_constructor` attribute is a callable other than the subclass itself (:issue:`43201`)
- Bug in arithmetic operations involving :class:`RangeIndex` where the result would have the incorrect ``name`` (:issue:`43962`)
-

Conversion
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,9 +885,8 @@ def _arith_method(self, other, op):
step = op

# TODO: if other is a RangeIndex we may have more efficient options
other = extract_array(other, extract_numpy=True, extract_range=True)

left, right = self, other
right = extract_array(other, extract_numpy=True, extract_range=True)
left = self

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

result = type(self)(rstart, rstop, rstep, name=self.name)
res_name = ops.get_op_result_name(self, other)
result = type(self)(rstart, rstop, rstep, name=res_name)

# for compat with numpy / Int64Index
# even if we can represent as a RangeIndex, return
Expand Down
15 changes: 8 additions & 7 deletions pandas/tests/arithmetic/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,11 +693,10 @@ def test_mul_float_series(self, numeric_idx):
tm.assert_series_equal(result, expected)

def test_mul_index(self, numeric_idx):
# in general not true for RangeIndex
idx = numeric_idx
if not isinstance(idx, RangeIndex):
result = idx * idx
tm.assert_index_equal(result, idx ** 2)

result = idx * idx
tm.assert_index_equal(result, idx ** 2)

def test_mul_datelike_raises(self, numeric_idx):
idx = numeric_idx
Expand Down Expand Up @@ -1090,11 +1089,11 @@ def test_ufunc_compat(self, holder):
box = Series if holder is Series else Index

if holder is RangeIndex:
idx = RangeIndex(0, 5)
idx = RangeIndex(0, 5, name="foo")
else:
idx = holder(np.arange(5, dtype="int64"))
idx = holder(np.arange(5, dtype="int64"), name="foo")
result = np.sin(idx)
expected = box(np.sin(np.arange(5, dtype="int64")))
expected = box(np.sin(np.arange(5, dtype="int64")), name="foo")
tm.assert_equal(result, expected)

@pytest.mark.parametrize("holder", [Int64Index, UInt64Index, Float64Index, Series])
Expand Down Expand Up @@ -1212,6 +1211,8 @@ class TestNumericArithmeticUnsorted:
def check_binop(self, ops, scalars, idxs):
for op in ops:
for a, b in combinations(idxs, 2):
a = a._rename("foo")
b = b._rename("bar")
result = op(a, b)
expected = op(Int64Index(a), Int64Index(b))
tm.assert_index_equal(result, expected)
Expand Down