BUG: fix sort_index AssertionError with RangeIndex and level parameter#64386
Open
repeating wants to merge 2 commits intopandas-dev:mainfrom
Open
BUG: fix sort_index AssertionError with RangeIndex and level parameter#64386repeating wants to merge 2 commits intopandas-dev:mainfrom
repeating wants to merge 2 commits intopandas-dev:mainfrom
Conversation
jbrockmendel
reviewed
Mar 7, 2026
| rng = range(len(self)) | ||
| return sorted_index, RangeIndex(rng) | ||
| indexer = np.arange(len(self), dtype=np.intp) | ||
| return sorted_index, indexer |
Member
There was a problem hiding this comment.
what are the consequences of this? knowing you have a RangeIndex can be good for performance downstream
Author
There was a problem hiding this comment.
the indexer here is only consumed by self._mgr.take(indexer, ...) which asserts isinstance(indexer, np.ndarray) — nothing downstream of sort_values(return_indexer=True) uses the indexer as a RangeIndex, it's purely a positional array for the block manager's take path. the base class Index.sort_values already returns np.ndarray for this, so this just brings the RangeIndex override in line with that contract.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
closes #64383
RangeIndex.sort_values(return_indexer=True)was returning aRangeIndexobject as the indexer, but the block manager (managers.py) assertsisinstance(indexer, np.ndarray)before calling.take(). this causedsort_index(level=...)to crash withAssertionErrorany time the index stayed aRangeIndex(e.g. an already-sorted range index, or a default index with.namesset).the base class
Index.sort_valuesalways returnsnp.arange(..., dtype=np.intp)as the indexer —RangeIndex.sort_valueswas overriding for performance but breaking that contract. the fix is to returnnp.arange(len(self), dtype=np.intp)(or the reversed equivalent) instead ofRangeIndex(rng).added a regression test covering both reported cases, and a whatsnew entry under v3.0.2.