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

TYP: sort_index #46300

Merged
merged 12 commits into from
Mar 18, 2022
6 changes: 4 additions & 2 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,12 @@ def closed(self) -> bool:
XMLParsers = Literal["lxml", "etree"]

# Interval closed type

IntervalClosedType = Literal["left", "right", "both", "neither"]

# datetime and NaTType

DatetimeNaTType = Union[datetime, "NaTType"]
DateTimeErrorChoices = Literal["ignore", "raise", "coerce"]

# sort_index
SortKind = Literal["quicksort", "mergesort", "heapsort", "stable"]
NaPosition = Literal["first", "last"]
79 changes: 65 additions & 14 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@
IndexKeyFunc,
IndexLabel,
Level,
NaPosition,
PythonFuncType,
ReadBuffer,
Renamer,
Scalar,
SortKind,
StorageOptions,
Suffixes,
TimedeltaConvertibleTypes,
Expand Down Expand Up @@ -6426,19 +6428,68 @@ def sort_values( # type: ignore[override]
else:
return result.__finalize__(self, method="sort_values")

@deprecate_nonkeyword_arguments(version=None, allowed_args=["self"])
@overload
def sort_index(
self,
*,
Dr-Irv marked this conversation as resolved.
Show resolved Hide resolved
axis: Axis = ...,
level: Level | None = ...,
ascending: bool | Sequence[bool] = ...,
inplace: Literal[True],
kind: SortKind = ...,
na_position: NaPosition = ...,
sort_remaining: bool = ...,
ignore_index: bool = ...,
key: IndexKeyFunc = ...,
) -> None:
...

@overload
def sort_index(
self,
*,
axis: Axis = ...,
level: Level | None = ...,
ascending: bool | Sequence[bool] = ...,
inplace: Literal[False] = ...,
kind: SortKind = ...,
na_position: NaPosition = ...,
sort_remaining: bool = ...,
ignore_index: bool = ...,
key: IndexKeyFunc = ...,
) -> DataFrame:
...

@overload
def sort_index(
self,
*,
axis: Axis = ...,
level: Level | None = ...,
ascending: bool | Sequence[bool] = ...,
inplace: bool = ...,
kind: SortKind = ...,
na_position: NaPosition = ...,
sort_remaining: bool = ...,
ignore_index: bool = ...,
key: IndexKeyFunc = ...,
) -> DataFrame | None:
...

# error: Signature of "sort_index" incompatible with supertype "NDFrame"
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self"])
def sort_index( # type: ignore[override]
Dr-Irv marked this conversation as resolved.
Show resolved Hide resolved
self,
axis: Axis = 0,
level: Level | None = None,
ascending: bool | int | Sequence[bool | int] = True,
ascending: bool | Sequence[bool] = True,
inplace: bool = False,
kind: str = "quicksort",
na_position: str = "last",
kind: SortKind = "quicksort",
na_position: NaPosition = "last",
sort_remaining: bool = True,
ignore_index: bool = False,
key: IndexKeyFunc = None,
):
) -> DataFrame | None:
"""
Sort object by labels (along an axis).

Expand Down Expand Up @@ -6529,15 +6580,15 @@ def sort_index(
d 4
"""
return super().sort_index(
axis,
level,
ascending,
inplace,
kind,
na_position,
sort_remaining,
ignore_index,
key,
axis=axis,
level=level,
ascending=ascending,
inplace=inplace,
kind=kind,
na_position=na_position,
sort_remaining=sort_remaining,
ignore_index=ignore_index,
key=key,
)

def value_counts(
Expand Down
64 changes: 57 additions & 7 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@
JSONSerializable,
Level,
Manager,
NaPosition,
NDFrameT,
RandomState,
Renamer,
SortKind,
StorageOptions,
T,
TimedeltaConvertibleTypes,
Expand Down Expand Up @@ -4683,18 +4685,66 @@ def sort_values(
"""
raise AbstractMethodError(self)

@overload
def sort_index(
self,
axis=0,
level=None,
ascending: bool_t | int | Sequence[bool_t | int] = True,
*,
Dr-Irv marked this conversation as resolved.
Show resolved Hide resolved
axis: Axis = ...,
level: Level | None = ...,
ascending: bool_t | Sequence[bool_t] = ...,
inplace: Literal[True],
kind: SortKind = ...,
na_position: NaPosition = ...,
sort_remaining: bool_t = ...,
ignore_index: bool_t = ...,
key: IndexKeyFunc = ...,
) -> None:
...

@overload
def sort_index(
self: NDFrameT,
*,
axis: Axis = ...,
level: Level | None = ...,
ascending: bool_t | Sequence[bool_t] = ...,
inplace: Literal[False] = ...,
kind: SortKind = ...,
na_position: NaPosition = ...,
sort_remaining: bool_t = ...,
ignore_index: bool_t = ...,
key: IndexKeyFunc = ...,
) -> NDFrameT:
...

@overload
def sort_index(
self: NDFrameT,
*,
axis: Axis = ...,
level: Level | None = ...,
ascending: bool_t | Sequence[bool_t] = ...,
inplace: bool_t = ...,
kind: SortKind = ...,
na_position: NaPosition = ...,
sort_remaining: bool_t = ...,
ignore_index: bool_t = ...,
key: IndexKeyFunc = ...,
) -> NDFrameT | None:
...

def sort_index(
self: NDFrameT,
axis: Axis = 0,
level: Level | None = None,
ascending: bool_t | Sequence[bool_t] = True,
inplace: bool_t = False,
kind: str = "quicksort",
na_position: str = "last",
kind: SortKind = "quicksort",
na_position: NaPosition = "last",
sort_remaining: bool_t = True,
ignore_index: bool_t = False,
key: IndexKeyFunc = None,
):
) -> NDFrameT | None:

inplace = validate_bool_kwarg(inplace, "inplace")
axis = self._get_axis_number(axis)
Expand All @@ -4715,7 +4765,7 @@ def sort_index(
if ignore_index:
result.index = default_index(len(self))
if inplace:
return
return None
else:
return result

Expand Down
84 changes: 68 additions & 16 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@
DtypeObj,
FillnaOptions,
IndexKeyFunc,
Level,
NaPosition,
SingleManager,
SortKind,
StorageOptions,
TimedeltaConvertibleTypes,
TimestampConvertibleTypes,
Expand Down Expand Up @@ -3575,19 +3578,68 @@ def sort_values(
else:
return result.__finalize__(self, method="sort_values")

@deprecate_nonkeyword_arguments(version=None, allowed_args=["self"])
@overload
def sort_index(
self,
axis=0,
level=None,
ascending: bool | int | Sequence[bool | int] = True,
*,
Dr-Irv marked this conversation as resolved.
Show resolved Hide resolved
axis: Axis = ...,
level: Level | None = ...,
ascending: bool | Sequence[bool] = ...,
inplace: Literal[True],
kind: SortKind = ...,
na_position: NaPosition = ...,
sort_remaining: bool = ...,
ignore_index: bool = ...,
key: IndexKeyFunc = ...,
) -> None:
...

@overload
def sort_index(
self,
*,
axis: Axis = ...,
level: Level | None = ...,
ascending: bool | Sequence[bool] = ...,
inplace: Literal[False] = ...,
kind: SortKind = ...,
na_position: NaPosition = ...,
sort_remaining: bool = ...,
ignore_index: bool = ...,
key: IndexKeyFunc = ...,
) -> Series:
...

@overload
def sort_index(
self,
*,
axis: Axis = ...,
level: Level | None = ...,
ascending: bool | Sequence[bool] = ...,
inplace: bool = ...,
kind: SortKind = ...,
na_position: NaPosition = ...,
sort_remaining: bool = ...,
ignore_index: bool = ...,
key: IndexKeyFunc = ...,
) -> Series | None:
...

# error: Signature of "sort_index" incompatible with supertype "NDFrame"
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self"])
def sort_index( # type: ignore[override]
self,
axis: Axis = 0,
level: Level | None = None,
ascending: bool | Sequence[bool] = True,
inplace: bool = False,
kind: str = "quicksort",
na_position: str = "last",
kind: SortKind = "quicksort",
na_position: NaPosition = "last",
sort_remaining: bool = True,
ignore_index: bool = False,
key: IndexKeyFunc = None,
):
) -> Series | None:
"""
Sort Series by index labels.

Expand Down Expand Up @@ -3724,15 +3776,15 @@ def sort_index(
"""

return super().sort_index(
axis,
level,
ascending,
inplace,
kind,
na_position,
sort_remaining,
ignore_index,
key,
axis=axis,
level=level,
ascending=ascending,
inplace=inplace,
kind=kind,
na_position=na_position,
sort_remaining=sort_remaining,
ignore_index=ignore_index,
key=key,
)

def argsort(self, axis=0, kind="quicksort", order=None) -> Series:
Expand Down
18 changes: 13 additions & 5 deletions pandas/core/sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Hashable,
Iterable,
Sequence,
cast,
)
import warnings

Expand All @@ -22,7 +23,10 @@
from pandas._libs.hashtable import unique_label_indices
from pandas._typing import (
IndexKeyFunc,
Level,
NaPosition,
Shape,
SortKind,
npt,
)

Expand All @@ -47,10 +51,10 @@

def get_indexer_indexer(
target: Index,
level: str | int | list[str] | list[int],
ascending: Sequence[bool | int] | bool | int,
kind: str,
na_position: str,
level: Level | list[Level] | None,
ascending: Sequence[bool] | bool,
kind: SortKind,
twoertwein marked this conversation as resolved.
Show resolved Hide resolved
na_position: NaPosition,
sort_remaining: bool,
key: IndexKeyFunc,
) -> npt.NDArray[np.intp] | None:
Expand Down Expand Up @@ -92,8 +96,12 @@ def get_indexer_indexer(
):
return None

# ascending can only be a Sequence for MultiIndex
indexer = nargsort(
twoertwein marked this conversation as resolved.
Show resolved Hide resolved
target, kind=kind, ascending=ascending, na_position=na_position
target,
kind=kind,
ascending=cast(bool, ascending),
na_position=na_position,
)
return indexer

Expand Down