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
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4972,7 +4972,7 @@ def sort_index(
self,
axis=0,
level=None,
ascending: bool = True,
ascending: Union[bool, List[bool]] = True,
inplace: bool = False,
kind: str = "quicksort",
na_position: str = "last",
Expand All @@ -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

Sort ascending vs. descending. When the index is a MultiIndex the
sort direction can be controlled for each level individually.
inplace : bool, default False
If True, perform operation in-place.
kind : {'quicksort', 'mergesort', 'heapsort'}, default 'quicksort'
Expand Down
16 changes: 9 additions & 7 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Optional,
Tuple,
Type,
Union,
)
import warnings

Expand Down Expand Up @@ -2981,11 +2982,11 @@ def sort_index(
self,
axis=0,
level=None,
ascending=True,
inplace=False,
kind="quicksort",
na_position="last",
sort_remaining=True,
ascending: Union[bool, List[bool]] = True,
inplace: bool = False,
kind: str = "quicksort",
na_position: str = "last",
sort_remaining: bool = True,
ignore_index: bool = False,
):
"""
Expand All @@ -3000,8 +3001,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

Sort ascending vs. descending. When the index is a MultiIndex the
sort direction can be controlled for each level individually.
inplace : bool, default False
If True, perform operation in-place.
kind : {'quicksort', 'mergesort', 'heapsort'}, default 'quicksort'
Expand Down