Skip to content

BUG: sort_index not preserving index when ascending=False #57293

@galipremsagar

Description

@galipremsagar

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

In [1]: import pandas as pd

In [2]: import numpy as np

In [3]: pdf = pd.DataFrame(
   ...:         {
   ...:             "b": [1.0, 3.0, np.nan],
   ...:             "a": [1, 4, 3],
   ...:             1: ["a", "b", "c"],
   ...:             "e": [3, 1, 4],
   ...:             "d": [1, 2, 8],
   ...:         }
   ...:     )

In [4]: pdf = pdf.set_index(["b", "a", 1])

In [5]: pdf.sort_index(axis='columns')
Out[5]: 
         d  e
b   a 1      
1.0 1 a  1  3
3.0 4 b  2  1
NaN 3 c  8  4

In [6]: pdf.sort_index(axis='columns', ignore_index=True)
Out[6]: 
         0  1
b   a 1      
1.0 1 a  1  3
3.0 4 b  2  1
NaN 3 c  8  4

In [7]: pdf.sort_index(axis='columns', ignore_index=True, ascending=True)
Out[7]: 
         0  1
b   a 1      
1.0 1 a  1  3
3.0 4 b  2  1
NaN 3 c  8  4

In [8]: pdf.sort_index(axis='columns', ignore_index=True, ascending=False)
Out[8]: 
   e  d
0  3  1
1  1  2
2  4  8

In [9]: pd.__version__
Out[9]: '2.2.0'

Issue Description

When ascending=False the index is being dropped.

Expected Behavior

Not dropping index.

Installed Versions

INSTALLED VERSIONS

commit : fd3f571
python : 3.10.13.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-91-generic
Version : #101-Ubuntu SMP Tue Nov 14 13:30:08 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.0
numpy : 1.26.3
pytz : 2024.1
dateutil : 2.8.2
setuptools : 69.0.3
pip : 24.0
Cython : 3.0.8
pytest : 7.4.4
hypothesis : 6.98.1
sphinx : 6.2.1
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.3
IPython : 8.21.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2024.2.0
gcsfs : None
matplotlib : None
numba : 0.59.0
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 14.0.1
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : 2024.2.0
scipy : 1.12.0
sqlalchemy : 2.0.25
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.4
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexRelated to the Index class or subclasses

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions