Skip to content

TYP: sort_index #46300

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 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,9 +307,11 @@ def closed(self) -> bool:
XMLParsers = Literal["lxml", "etree"]

# Interval closed type

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

# datetime and NaTType

DatetimeNaTType = Union[datetime, "NaTType"]

# sort_index
SortKind = Literal["quicksort", "mergesort", "heapsort", "stable"]
NaPosition = Literal["first", "last"]
82 changes: 68 additions & 14 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
Iterator,
Literal,
Sequence,
TypeVar,
cast,
overload,
)
Expand Down Expand Up @@ -61,10 +62,12 @@
IndexKeyFunc,
IndexLabel,
Level,
NaPosition,
PythonFuncType,
ReadBuffer,
Renamer,
Scalar,
SortKind,
StorageOptions,
Suffixes,
TimedeltaConvertibleTypes,
Expand Down Expand Up @@ -220,6 +223,8 @@

from pandas.io.formats.style import Styler

DataFrameT = TypeVar("DataFrameT", bound="DataFrame")

# ---------------------------------------------------------------------
# Docstring templates

Expand Down Expand Up @@ -6426,19 +6431,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,
*,
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: DataFrameT,
Copy link
Member Author

Choose a reason for hiding this comment

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

as far as I remember, pyright doesn't like self annotations that are broader than the class itself (cannot use NDFrameT).

*,
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 = ...,
) -> DataFrameT:
...

@overload
def sort_index(
self: DataFrameT,
*,
axis: Axis = ...,
level: Level | None = ...,
ascending: bool | Sequence[bool] = ...,
inplace: bool = ...,
kind: SortKind = ...,
na_position: NaPosition = ...,
sort_remaining: bool = ...,
ignore_index: bool = ...,
key: IndexKeyFunc = ...,
) -> DataFrameT | 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: DataFrameT,
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,
):
) -> DataFrameT | None:
"""
Sort object by labels (along an axis).

Expand Down Expand Up @@ -6529,15 +6583,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,
*,
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
89 changes: 73 additions & 16 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Iterable,
Literal,
Sequence,
TypeVar,
Union,
cast,
overload,
Expand All @@ -39,7 +40,10 @@
DtypeObj,
FillnaOptions,
IndexKeyFunc,
Level,
NaPosition,
SingleManager,
SortKind,
StorageOptions,
TimedeltaConvertibleTypes,
TimestampConvertibleTypes,
Expand Down Expand Up @@ -159,6 +163,8 @@
from pandas.core.groupby.generic import SeriesGroupBy
from pandas.core.resample import Resampler

SeriesT = TypeVar("SeriesT", bound="Series")

__all__ = ["Series"]

_shared_doc_kwargs = {
Expand Down Expand Up @@ -3575,19 +3581,70 @@ def sort_values(
else:
return result.__finalize__(self, method="sort_values")

@deprecate_nonkeyword_arguments(version=None, allowed_args=["self"])
# error: Signature of "sort_index" incompatible with supertype "NDFrame"
@overload # type: ignore[override]
def sort_index(
self,
axis=0,
level=None,
ascending: bool | int | Sequence[bool | int] = True,
*,
axis: Literal[0] = ...,
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: SeriesT,
*,
axis: Literal[0] = ...,
level: Level | None = ...,
ascending: bool | Sequence[bool] = ...,
inplace: Literal[False] = ...,
kind: SortKind = ...,
na_position: NaPosition = ...,
sort_remaining: bool = ...,
ignore_index: bool = ...,
key: IndexKeyFunc = ...,
) -> SeriesT:
...

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

# error: Argument 1 of "sort_index" is incompatible with supertype "NDFrame";
# supertype defines the argument type as "Union[str, int]"
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self"])
def sort_index( # type: ignore[override]
self: SeriesT,
axis: Literal[0] = 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,
):
) -> SeriesT | None:
"""
Sort Series by index labels.

Expand Down Expand Up @@ -3724,15 +3781,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
Loading