Skip to content
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

DataFrame.sort_index() with ascending passed as a list on a single level index gives wrong result #32334

Closed
aniaan opened this issue Feb 28, 2020 · 4 comments · Fixed by #36767
Assignees
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Bug good first issue Index Related to the Index class or subclasses

Comments

@aniaan
Copy link
Contributor

aniaan commented Feb 28, 2020

I found some problems while using data_frame sort_index, first, create dataframe

data = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), columns=['a', 'b', 'c'])
data = data.set_index('a')
data.sort_index(level=['a'], ascending=[False])

Then I want to sort index "a" in descending order,

data.sort_index(level=['a'], ascending=[False])

The above code does not sort index a in descending order, I found the reason is that this dataframe is a single index structure, so when ascending = list is passed, it does not take effect

pandas/pandas/core/frame.py

Lines 4777 to 4783 in bf613c1

if level is not None:
new_axis, indexer = labels.sortlevel(
level, ascending=ascending, sort_remaining=sort_remaining
)
elif isinstance(labels, ABCMultiIndex):

Because when it is used, the index may be single index or multi-index. In order to use the effect uniformly,I think there should be a judgment here. If index is a single index and ascending type == list, ascending should be equal to ascending [0],This may be more user friendly

If yes, can I mention pr

@Dr-Irv Dr-Irv changed the title Unified data_frame sort_index calling format DataFrame.sort_index() with ascending passed as a list on a single level index gives wrong result Sep 27, 2020
@Dr-Irv Dr-Irv added Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Bug Index Related to the Index class or subclasses labels Sep 27, 2020
@Dr-Irv
Copy link
Contributor

Dr-Irv commented Sep 27, 2020

It seems that this works fine:

 data.sort_index(level=['a'], ascending=False)

But the following does not

data.sort_index(level=['a'], ascending=[False])

PR welcome

@Dr-Irv
Copy link
Contributor

Dr-Irv commented Sep 27, 2020

Possible fix is to do 2 things:

  1. In indexes/base.py in sortlevel() add a check on the type of ascending, and if a list and length 1, convert to a True/False value.
  2. If a list and length greater than 1, raise an Exception

@heyitskevin
Copy link

take

@aniaan aniaan removed their assignment Oct 1, 2020
@aniaan
Copy link
Contributor Author

aniaan commented Oct 1, 2020

take

aniaan pushed a commit to aniaan/pandas that referenced this issue Oct 1, 2020
aniaan pushed a commit to aniaan/pandas that referenced this issue Oct 1, 2020
Dr-Irv pushed a commit that referenced this issue Oct 1, 2020
* BUG: Index sortlevel ascending add type checking #32334

* DOC: add v1.2 whatsnew entry #32334

* BUG: adjust judgment conditions, len(ascending) > 1 => len(ascending) != 1

* DOC: adjustment whatsnew

Co-authored-by: beanan <fanjianan916@hotmail.com>
kesmit13 pushed a commit to kesmit13/pandas that referenced this issue Nov 2, 2020
…ndas-dev#36767)

* BUG: Index sortlevel ascending add type checking pandas-dev#32334

* DOC: add v1.2 whatsnew entry pandas-dev#32334

* BUG: adjust judgment conditions, len(ascending) > 1 => len(ascending) != 1

* DOC: adjustment whatsnew

Co-authored-by: beanan <fanjianan916@hotmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Bug good first issue Index Related to the Index class or subclasses
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants