Skip to content

DOC: Update sort_index docs #31898

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

Merged
merged 3 commits into from
Feb 19, 2020
Merged

DOC: Update sort_index docs #31898

merged 3 commits into from
Feb 19, 2020

Conversation

dsaxton
Copy link
Member

@dsaxton dsaxton commented Feb 11, 2020

@@ -4989,8 +4989,9 @@ def sort_index(
and 1 identifies the columns.
level : int or level name or list of ints or list of level names
If not None, sort on values in specified index level(s).
ascending : bool, default True
Sort ascending vs. descending.
ascending : bool or list of bools, default True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update the annotation for this function to reflect this?

Copy link
Member Author

@dsaxton dsaxton Feb 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So originally I had given it a Union type but mypy complained because here the ascending argument seemingly has to be a singleton bool: https://github.com/pandas-dev/pandas/blob/master/pandas/core/frame.py#L5055. I guess this is unavoidable because it has no way of knowing that it shouldn't get here when ascending is a list?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't that conflict with the documentation updates?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think it's okay since it "can" accept a list of bools, just conditional on the index being a MultiIndex?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure - looks like this works (probably shouldn't)

>>> pd.DataFrame([[1]]).sort_index(ascending=[True, False, True])
   0
0  1

@WillAyd WillAyd added Docs Typing type annotations, mypy/pyright type checking labels Feb 12, 2020
@@ -3000,8 +3000,9 @@ def sort_index(
Axis to direct sorting. This can only be 0 for Series.
level : int, optional
If not None, sort on values in specified index level(s).
ascending : bool, default true
Sort ascending vs. descending.
ascending : bool or list of bools, default True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if list of bools is allowed, should that show up in the annotation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the discussion with WillAyd above; mypy doesn't seem to like that because nargsort gets called when we don't have a MultiIndex and it expects a single bool

@dsaxton
Copy link
Member Author

dsaxton commented Feb 18, 2020

@WillAyd Good to merge or were there changes you'd like me to make? Not sure if there's a "nice" way to get around the mypy annoyance

@WillAyd WillAyd added this to the 1.1 milestone Feb 19, 2020
@WillAyd
Copy link
Member

WillAyd commented Feb 19, 2020

Yea would be nice to get the typing figured out, but OK for another day

@WillAyd WillAyd merged commit 0c107bd into pandas-dev:master Feb 19, 2020
@WillAyd
Copy link
Member

WillAyd commented Feb 19, 2020

Thanks @dsaxton

@dsaxton dsaxton deleted the doc-sort-idx branch February 19, 2020 01:36
roberthdevries pushed a commit to roberthdevries/pandas that referenced this pull request Mar 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MultiLevelIndex sort_index already does support ascending as a list of booleans
4 participants