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

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

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

Comments

@ezerkar
Copy link

@ezerkar 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 changed the title ENH: ENH: Raise error on ascending='False' May 23, 2021
@vishalkumarprasad
Copy link

@vishalkumarprasad vishalkumarprasad commented May 24, 2021

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

@ezerkar
Copy link
Author

@ezerkar ezerkar commented May 24, 2021

@lithomas1
Copy link
Member

@lithomas1 lithomas1 commented May 24, 2021

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

@vishalkumarprasad vishalkumarprasad commented May 24, 2021

take

@ezerkar
Copy link
Author

@ezerkar ezerkar commented May 24, 2021

@Lokeshrathi
Copy link

@Lokeshrathi Lokeshrathi commented May 24, 2021

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

@vishalkumarprasad vishalkumarprasad commented May 26, 2021

Hi Eric, I am already working on this enhancement.

@Anupam-USP
Copy link
Contributor

@Anupam-USP Anupam-USP commented May 27, 2021

Hey, is this issue still open or taken?

@ezerkar
Copy link
Author

@ezerkar ezerkar commented May 27, 2021

@vishalkumarprasad
Copy link

@vishalkumarprasad vishalkumarprasad commented May 27, 2021

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

@ninasiam ninasiam commented May 28, 2021

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

@vishalkumarprasad vishalkumarprasad commented May 29, 2021

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

@ninasiam ninasiam commented May 29, 2021

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

@vishalkumarprasad vishalkumarprasad commented May 29, 2021

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

@gaikwadrahul20 gaikwadrahul20 commented Jul 23, 2021

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment