Skip to content

ENH: RangeIndex.sort_values can avoid materialization of values. #43584

Closed
@galipremsagar

Description

@galipremsagar

Is your feature request related to a problem?

We can avoid the materialization of RangeIndex when sort_values is invoked on it. The reason being RangeIndex is always sorted.

Describe the solution you'd like

>>> import pandas as pd
>>> pd.__version__
'1.3.3'
>>> idx = pd.RangeIndex(0, 10, 1)
>>> idx
RangeIndex(start=0, stop=10, step=1)
>>> idx.sort_values(ascending=True)
Int64Index([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype='int64')  # Instead we should be returning RangeIndex(start=0, stop=10, step=1)

>>> idx.sort_values(ascending=False)
Int64Index([9, 8, 7, 6, 5, 4, 3, 2, 1, 0], dtype='int64')    #Instead we can return RangeIndex(start=10-1, start=0-1, step=-1), see below
>>> pd.RangeIndex(10-1, 0-1, -1)._values
array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])

API breaking implications

This would break the expected return types of Index of DataFrame & Series in some operations, but would result in speedup of operation as we would not be materializing the RangeIndex.

Metadata

Metadata

Assignees

Labels

IndexRelated to the Index class or subclassesPerformanceMemory or execution speed performance

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions