Skip to content

ENH: Raise error on ascending='False' #41634

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
ezerkar opened this issue May 23, 2021 · 15 comments · Fixed by #42684
Closed

ENH: Raise error on ascending='False' #41634

ezerkar opened this issue May 23, 2021 · 15 comments · Fixed by #42684
Assignees
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff DataFrame DataFrame data structure Error Reporting Incorrect or improved errors from pandas good first issue
Milestone

Comments

@ezerkar
Copy link

ezerkar commented May 23, 2021

Is your feature request related to a problem?

when sort_values ascending parameter is set to a string (df.sort_values(col_name, ascending='False') it does not error although it's meaningless, this could be quite confusing.

Describe the solution you'd like

Raise an error if ascending is equal to values that are not boolean

API breaking implications

Error raising

Describe alternatives you've considered

Warning is also good, but I think error makes more sense

Additional context

for an hour I was wondering why ascending='False'
is not working

isinstance('False', bool) :)
@ezerkar ezerkar added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels May 23, 2021
@ezerkar ezerkar changed the title ENH: ENH: Raise error on ascending='False' May 23, 2021
@vishalkumarprasad
Copy link

Hello, I am a new to open source contribution. Can I take up this enhancement?

@ezerkar
Copy link
Author

ezerkar commented May 24, 2021 via email

@lithomas1 lithomas1 added Error Reporting Incorrect or improved errors from pandas Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff DataFrame DataFrame data structure and removed Needs Triage Issue that has not been reviewed by a pandas team member Enhancement labels May 24, 2021
@lithomas1 lithomas1 added this to the 1.3 milestone May 24, 2021
@lithomas1 lithomas1 modified the milestones: 1.3, Contributions Welcome May 24, 2021
@lithomas1
Copy link
Member

Hi @ezerkar,
This seems reasonable to me, since we do this for sort_index as well. (see #39451)
@vishalkumarprasad Thanks for volunteering to contribute to pandas. Feel free to take up this issue. You can find our contributing guidelines here. https://pandas.pydata.org/docs/development/contributing.html and if you need help, don't hesitate to ping me :).

@vishalkumarprasad
Copy link

take

@ezerkar
Copy link
Author

ezerkar commented May 24, 2021 via email

@Lokeshrathi
Copy link

You can use sorted() function here.
Code:

a = ("h", "b", "a", "c", "f", "d", "e", "g")
x = sorted(a)
print(x)

Output:
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

@vishalkumarprasad
Copy link

Hi Eric, I am already working on this enhancement.

@Anupam-USP
Copy link
Contributor

Hey, is this issue still open or taken?

@ezerkar
Copy link
Author

ezerkar commented May 27, 2021 via email

@vishalkumarprasad
Copy link

Hi all, while working on this problem, i figured another issue where if you put series.sort_values ascending parameter to 1 or 0, it fails. However according to the code, it shouldn't fail.
Can someone confirm?

@ninasiam
Copy link

Hello all! @vishalkumarprasad I have tried the test case you have described and it indeed fails. However, I have noticed that the is_bool() function - in pandas,core-, which raises the error, also fails with 0, 1 as input arguments. It seems to me that series.sort_values() works as expected.

@vishalkumarprasad
Copy link

Hi all, @ninasiam If you look into the function definition of series.sort_values(), its clearly written that the ascending argument takes either bool or int or a sequence of bool or int. Hence there is mismatch somewhere, either in the function definition or is_bool() function or the implementation of is_bool() function. Please let me know your thoughts.

Screenshot 2021-05-29 at 9 12 08 PM

@ninasiam
Copy link

Hello! @vishalkumarprasad I see your point, I have probably missed that :) I think the mismatch is probably due to the way is_bool() function is implemented. If I try:

pd.core.dtypes.common.is_bool(True)
pd.core.dtypes.common.is_bool(1)  

The output is:

True
False

respectively. An easy fix, to avoid confusion for the time being, could be the update of the sort_values() function definition, to accept only True/False as input for the ascending parameter.

@vishalkumarprasad
Copy link

Hello @ninasiam,
So there is one more point which actually led me towards this issue. sort_values for series does not work currently for 1 and 0, but sort_values for dataframe works perfectly with 1 and 0. Shouldn't we make both of them to work in the same way?

gaikwadrahul20 added a commit to gaikwadrahul20/pandas that referenced this issue Jul 23, 2021
@gaikwadrahul20
Copy link
Contributor

take

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 DataFrame DataFrame data structure Error Reporting Incorrect or improved errors from pandas good first issue
Projects
None yet
8 participants