-
-
Notifications
You must be signed in to change notification settings - Fork 19.7k
Description
Pandas version checks
-
I have checked that this issue has not already been reported.
-
I have confirmed this bug exists on the latest version of pandas.
-
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
df = pd.DataFrame({"a": [1, 2, 3], "foo": [1, 0, 2]}).set_index(["foo"], drop=True)
print(df.sort_index(level="foo")) # Works in pandas 2 and 3
df = pd.DataFrame({"a": [1, 2, 3], "foo": [0, 1, 2]}).set_index(["foo"], drop=True)
print(df.sort_index(level="foo")) # Fails in pandas 3
df = pd.DataFrame({"a": [1, 2, 3]})
df.index.names = ["foo"]
print(df.sort_index(level="foo")) # Fails in pandas 2 and 3Issue Description
From pandas-3.0.0 on the above example fails with
a
foo
0 1
1 2
2 3
Traceback (most recent call last):
File "/tmp/indextest.py", line 8, in <module>
print(df.sort_index(level="foo"))
~~~~~~~~~~~~~^^^^^^^^^^^^^
File "/home/jmu3si/Devel/pandas/pandas/core/frame.py", line 8394, in sort_index
return super().sort_index(
~~~~~~~~~~~~~~~~~~^
axis=axis,
^^^^^^^^^^
...<7 lines>...
key=key,
^^^^^^^^
)
^
File "/home/jmu3si/Devel/pandas/pandas/core/generic.py", line 5187, in sort_index
new_data = self._mgr.take(indexer, axis=baxis, verify=False)
File "/home/jmu3si/Devel/pandas/pandas/core/internals/managers.py", line 1069, in take
return self.reindex_indexer(
~~~~~~~~~~~~~~~~~~~~^
new_axis=new_labels,
^^^^^^^^^^^^^^^^^^^^
...<2 lines>...
allow_dups=True,
^^^^^^^^^^^^^^^^
)
^
File "/home/jmu3si/Devel/pandas/pandas/core/internals/managers.py", line 829, in reindex_indexer
assert isinstance(indexer, np.ndarray)
~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
AssertionError
The issue occurs when the following conditions are met:
- The index is a
RangeIndex. - There is only one index level.
.sort_indexis called with thelevel=argument.
It feels like kind of a regression because as it seems pandas-2.x ended up with an Index in some cases in which pandas-3.x ends up with a RangeIndex. So there are cases that worked fine in pandas-2 which don't in pandas-3.
Expected Behavior
Up to pandas-2.3.3 this worked and gave the expected output:
a
foo
0 2
1 1
2 3
a
foo
0 1
1 2
2 3
a
foo
0 1
1 2
2 3
Installed Versions
Details
INSTALLED VERSIONS ------------------ commit : e04b26f python : 3.13.12 python-bits : 64 OS : Linux OS-release : 6.8.0-101-generic Version : #101-Ubuntu SMP PREEMPT_DYNAMIC Mon Feb 9 10:15:05 UTC 2026 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : de_DE.UTF-8 LOCALE : de_DE.UTF-8pandas : 3.0.1
numpy : 2.4.2
dateutil : 2.9.0.post0
pip : None
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
psycopg2 : None
pymysql : None
pyarrow : None
pyiceberg : None
pyreadstat : None
pytest : None
python-calamine : None
pytz : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
qtpy : None
pyqt5 : None