Skip to content

TYP: type insert and nsmallest/largest #43865

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 2 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all 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: 6 additions & 1 deletion pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
from textwrap import dedent
from typing import (
TYPE_CHECKING,
Hashable,
Literal,
Sequence,
Union,
cast,
final,
Expand All @@ -27,6 +29,7 @@
AnyArrayLike,
ArrayLike,
DtypeObj,
IndexLabel,
Scalar,
TakeIndexer,
npt,
Expand Down Expand Up @@ -1338,10 +1341,12 @@ class SelectNFrame(SelectN):
nordered : DataFrame
"""

def __init__(self, obj, n: int, keep: str, columns):
def __init__(self, obj: DataFrame, n: int, keep: str, columns: IndexLabel):
super().__init__(obj, n, keep)
if not is_list_like(columns) or isinstance(columns, tuple):
columns = [columns]

columns = cast(Sequence[Hashable], columns)
columns = list(columns)
self.columns = columns

Expand Down
19 changes: 12 additions & 7 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1374,7 +1374,7 @@ def dot(self, other: Series) -> Series:
def dot(self, other: DataFrame | Index | ArrayLike) -> DataFrame:
...

def dot(self, other: AnyArrayLike | DataFrame | Series) -> DataFrame | Series:
def dot(self, other: AnyArrayLike | DataFrame) -> DataFrame | Series:
"""
Compute the matrix multiplication between the DataFrame and other.

Expand Down Expand Up @@ -2155,7 +2155,6 @@ def maybe_reorder(
to_remove = [arr_columns.get_loc(col) for col in arr_exclude]
arrays = [v for i, v in enumerate(arrays) if i not in to_remove]

arr_columns = arr_columns.drop(arr_exclude)
columns = columns.drop(exclude)

manager = get_option("mode.data_manager")
Expand Down Expand Up @@ -4383,7 +4382,13 @@ def predicate(arr: ArrayLike) -> bool:
mgr = self._mgr._get_data_subset(predicate)
return type(self)(mgr).__finalize__(self)

def insert(self, loc, column, value, allow_duplicates: bool = False) -> None:
def insert(
self,
loc: int,
column: Hashable,
value: Scalar | AnyArrayLike,
allow_duplicates: bool = False,
) -> None:
"""
Insert column into DataFrame at specified location.

Expand All @@ -4396,8 +4401,8 @@ def insert(self, loc, column, value, allow_duplicates: bool = False) -> None:
Insertion index. Must verify 0 <= loc <= len(columns).
column : str, number, or hashable object
Label of the inserted column.
value : int, Series, or array-like
allow_duplicates : bool, optional
value : Scalar, Series, or array-like
allow_duplicates : bool, optional default False

See Also
--------
Expand Down Expand Up @@ -6566,7 +6571,7 @@ def value_counts(

return counts

def nlargest(self, n, columns, keep: str = "first") -> DataFrame:
def nlargest(self, n: int, columns: IndexLabel, keep: str = "first") -> DataFrame:
"""
Return the first `n` rows ordered by `columns` in descending order.

Expand Down Expand Up @@ -6673,7 +6678,7 @@ def nlargest(self, n, columns, keep: str = "first") -> DataFrame:
"""
return algorithms.SelectNFrame(self, n=n, keep=keep, columns=columns).nlargest()

def nsmallest(self, n, columns, keep: str = "first") -> DataFrame:
def nsmallest(self, n: int, columns: IndexLabel, keep: str = "first") -> DataFrame:
"""
Return the first `n` rows ordered by `columns` in ascending order.

Expand Down