Skip to content

BUG: sort_index method ignores ascending parameter of type tuple #43884

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

Closed
1 of 3 tasks
vezeli opened this issue Oct 4, 2021 · 5 comments · Fixed by #43963
Closed
1 of 3 tasks

BUG: sort_index method ignores ascending parameter of type tuple #43884

vezeli opened this issue Oct 4, 2021 · 5 comments · Fixed by #43963
Labels
good first issue MultiIndex Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@vezeli
Copy link

vezeli commented Oct 4, 2021

  • 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 master branch of pandas.

Reproducible Example

import pandas as pd

df = pd.DataFrame({
    "animal": ["dog", "duck", "horse", "penguin", "kangaroo"], 
    "legs": [4, 2, 4, 2, 2], 
    "class": ["mammal", "bird", "mammal", "bird", "mammal"]
})

df.set_index(["class", "animal"], inplace=True)

sorted1 = df.sort_index(level=(0, 1), ascending=[True, False])
sorted2 = df.sort_index(level=(0, 1), ascending=(True, False))

Issue Description

In the above example, the sort_index method on DataFrame object does not sort MultiIndex properly when the ascending parameter is passed as tuple. The sorted2.index should be sorted in ascending order for level 0 (class) and descending order for level 1 (animal). However, this is not the case and level 1 is also ascending:

>>> print(sorted2.index)
MultiIndex([(  'bird',     'duck'),
            (  'bird',  'penguin'),
            ('mammal',      'dog'),
            ('mammal',    'horse'),
            ('mammal', 'kangaroo')],
           names=['class', 'animal'])

When the ascending parameter is list than the sort_index method works as expected such that level 0 is ascending and level 1 is descending:

>>> print(sorted1.index)
MultiIndex([(  'bird',  'penguin'),
            (  'bird',     'duck'),
            ('mammal', 'kangaroo'),
            ('mammal',    'horse'),
            ('mammal',      'dog')],
           names=['class', 'animal'])

Expected Behavior

I would expect sorted1.index and sorted2.index to be equal (i.e., include a test case with assert sorted1.index == sorted2.index that passes).

Installed Versions

INSTALLED VERSIONS

commit : b5958ee
python : 3.6.9.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-84-generic
Version : #94~18.04.1-Ubuntu SMP Thu Aug 26 23:17:46 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.5
numpy : 1.19.5
pytz : 2021.3
dateutil : 2.8.2
pip : 21.2.4
setuptools : 58.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 7.16.1
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@vezeli vezeli added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 4, 2021
@attack68
Copy link
Contributor

attack68 commented Oct 4, 2021

The docs confirm list-like should be accepted. Would you be interested in taking a look and submitting a PR?

@phofl
Copy link
Member

phofl commented Oct 4, 2021

This works on master, may need tests

@phofl
Copy link
Member

phofl commented Oct 4, 2021

This started working on 1.2. @vezeli please don't check the box persist on the latest version of pandas if you did not check

@phofl phofl added good first issue MultiIndex Needs Tests Unit test(s) needed to prevent regressions and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 4, 2021
@vezeli
Copy link
Author

vezeli commented Oct 5, 2021

Sorry about that @phofl I thought I was using the latest version but I was probably in wrong virtual environment... If it's OK with you I could submit a PR with a test that provides ascending param with a tuple?

@phofl
Copy link
Member

phofl commented Oct 5, 2021

Yeah for sure. You could also check if there is an existing test and parametrize that one. Not sure if there is though. Have checked yesterday if we have a test for tuples but could not find anything

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue MultiIndex Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants