Skip to content

Backport PR #28680 on branch 0.25.x (BUG: Fix RangeIndex.get_indexer for decreasing RangeIndex) #28745

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
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/v0.25.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Indexing
^^^^^^^^

- Fix regression in :meth:`DataFrame.reindex` not following ``limit`` argument (:issue:`28631`).
- Fix regression in :meth:`RangeIndex.get_indexer` for decreasing :class:`RangeIndex` where target values may be improperly identified as missing/present (:issue:`28678`)
-
-
-
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,9 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None):
if self.step > 0:
start, stop, step = self.start, self.stop, self.step
else:
# Work on reversed range for simplicity:
start, stop, step = (self.stop - self.step, self.start + 1, -self.step)
# GH 28678: work on reversed range for simplicity
reverse = self._range[::-1]
start, stop, step = reverse.start, reverse.stop, reverse.step

target_array = np.asarray(target)
if not (is_integer_dtype(target_array) and target_array.ndim == 1):
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/indexes/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,14 @@ def test_get_indexer_limit(self):
expected = np.array([0, 1, 2, 3, 3, -1], dtype=np.intp)
tm.assert_numpy_array_equal(result, expected)

@pytest.mark.parametrize("stop", [0, -1, -2])
def test_get_indexer_decreasing(self, stop):
# GH 28678
index = RangeIndex(7, stop, -3)
result = index.get_indexer(range(9))
expected = np.array([-1, 2, -1, -1, 1, -1, -1, 0, -1], dtype=np.intp)
tm.assert_numpy_array_equal(result, expected)

def test_join_outer(self):
# join with Int64Index
other = Int64Index(np.arange(25, 14, -1))
Expand Down