Skip to content

BUG: sort_values is sorting the index too when ignore_index=False #57531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
2 of 3 tasks
galipremsagar opened this issue Feb 20, 2024 · 1 comment
Open
2 of 3 tasks
Labels
Bug Sorting e.g. sort_index, sort_values

Comments

@galipremsagar
Copy link

galipremsagar commented Feb 20, 2024

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]: pdf = pd.DataFrame({"a": [1, 3, 5, 2, 4], "b": [1, 1, 2, 2, 3], "c": [9, 7, 7, 7, 1]})

In [3]: pdf = pdf.set_index(['a', 'b'])

In [4]: pdf
Out[4]: 
     c
a b   
1 1  9
3 1  7
5 2  7
2 2  7
4 3  1

In [5]: pdf.sort_values(['c'], ignore_index=False)
Out[5]: 
     c
a b   
4 3  1
3 1  7
2 2  7
5 2  7
1 1  9

Issue Description

When there seems to be a tie in column c, the MultiIndex levels seem to be sorted - which is kind of unexpected. The expected behavior would be to preserve the order of occurrence right?

Expected Behavior

In [5]: pdf.sort_values(['c'], ignore_index=False)
Out[5]: 
     c
a b   
4 3  1
3 1  7
5 2  7
2 2  7
1 1  9

Installed Versions

INSTALLED VERSIONS

commit : fd3f571
python : 3.10.13.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-94-generic
Version : #104-Ubuntu SMP Tue Jan 9 15:25:40 UTC 2024
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.4
pytz : 2024.1
dateutil : 2.8.2
setuptools : 69.1.0
pip : 24.0
Cython : 3.0.8
pytest : 7.4.4
hypothesis : 6.98.9
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.27
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

@galipremsagar galipremsagar added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 20, 2024
@mroeschke
Copy link
Member

I cannot seemingly get the sorted result on main

In [1]: In [1]: import pandas as pd
   ...: 
   ...: In [2]: pdf = pd.DataFrame({"a": [1, 3, 5, 2, 4], "b": [1, 1, 2, 2, 3], "c": [9, 7, 7, 7, 1]})
   ...: 
   ...: In [3]: pdf = pdf.set_index(['a', 'b'])

In [2]: pdf.sort_values(['c'], ignore_index=False)
Out[2]: 
     c
a b   
4 3  1
3 1  7
5 2  7
2 2  7
1 1  9

But since the numpy sorts internally are unstable (xref #53558) maybe you're hitting a case where the result gets reordered?

@mroeschke mroeschke added Sorting e.g. sort_index, sort_values and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 20, 2024
@diogojarodrigues diogojarodrigues removed their assignment Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Sorting e.g. sort_index, sort_values
Projects
None yet
Development

No branches or pull requests

3 participants