Skip to content

PERF: sort_index on RangeIndex with level= copies data blocks unnecessarily #64883

@repeating

Description

@repeating

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this issue exists on the latest version of pandas.

  • I have confirmed this issue exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np
import timeit

n = 1_000_000
df = pd.DataFrame({"a": np.random.randn(n)})
df.index.name = "idx"

runs = 20

t1 = timeit.timeit(lambda: df.sort_index(), number=runs) / runs * 1000
t2 = timeit.timeit(lambda: df.sort_index(level="idx"), number=runs) / runs * 1000

print(f"pandas version: {pd.version}")
print(f"sort_index() : {t1:.2f} ms")
print(f"sort_index(level='idx') : {t2:.2f} ms")
print(f"overhead ratio : {t2 / t1:.1f}x")

Installed Versions

Details

INSTALLED VERSIONS

commit : aac1998
python : 3.13.7
python-bits : 64
OS : Darwin
OS-release : 24.6.0
Version : Darwin Kernel Version 24.6.0: Wed Nov 5 21:32:38 PST 2025; root:xnu-11417.140.69.705.2~1/RELEASE_ARM64_T6031
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 0+untagged.3.gaac1998.dirty
numpy : 2.4.2
dateutil : 2.9.0.post0
pip : 25.2
Cython : 3.2.4
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : 6.151.9
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
psycopg2 : None
pymysql : None
pyarrow : 23.0.1
pyiceberg : None
pyreadstat : None
pytest : 9.0.2
python-calamine : None
pytz : 2026.1.post1
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
qtpy : None
pyqt5 : None

Prior Performance

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    PerformanceMemory or execution speed performance

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions