Skip to content

Commit 1bcb841

Browse files
authored
TYP: type insert and nsmallest/largest (pandas-dev#43865)
1 parent bfdac81 commit 1bcb841

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

pandas/core/algorithms.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
from textwrap import dedent
99
from typing import (
1010
TYPE_CHECKING,
11+
Hashable,
1112
Literal,
13+
Sequence,
1214
Union,
1315
cast,
1416
final,
@@ -27,6 +29,7 @@
2729
AnyArrayLike,
2830
ArrayLike,
2931
DtypeObj,
32+
IndexLabel,
3033
Scalar,
3134
TakeIndexer,
3235
npt,
@@ -1255,10 +1258,12 @@ class SelectNFrame(SelectN):
12551258
nordered : DataFrame
12561259
"""
12571260

1258-
def __init__(self, obj, n: int, keep: str, columns):
1261+
def __init__(self, obj: DataFrame, n: int, keep: str, columns: IndexLabel):
12591262
super().__init__(obj, n, keep)
12601263
if not is_list_like(columns) or isinstance(columns, tuple):
12611264
columns = [columns]
1265+
1266+
columns = cast(Sequence[Hashable], columns)
12621267
columns = list(columns)
12631268
self.columns = columns
12641269

pandas/core/frame.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -1357,7 +1357,7 @@ def dot(self, other: Series) -> Series:
13571357
def dot(self, other: DataFrame | Index | ArrayLike) -> DataFrame:
13581358
...
13591359

1360-
def dot(self, other: AnyArrayLike | DataFrame | Series) -> DataFrame | Series:
1360+
def dot(self, other: AnyArrayLike | DataFrame) -> DataFrame | Series:
13611361
"""
13621362
Compute the matrix multiplication between the DataFrame and other.
13631363
@@ -2202,7 +2202,6 @@ def maybe_reorder(
22022202
to_remove = [arr_columns.get_loc(col) for col in arr_exclude]
22032203
arrays = [v for i, v in enumerate(arrays) if i not in to_remove]
22042204

2205-
arr_columns = arr_columns.drop(arr_exclude)
22062205
columns = columns.drop(exclude)
22072206

22082207
manager = get_option("mode.data_manager")
@@ -4437,7 +4436,13 @@ def predicate(arr: ArrayLike) -> bool:
44374436
mgr = self._mgr._get_data_subset(predicate)
44384437
return type(self)(mgr).__finalize__(self)
44394438

4440-
def insert(self, loc, column, value, allow_duplicates: bool = False) -> None:
4439+
def insert(
4440+
self,
4441+
loc: int,
4442+
column: Hashable,
4443+
value: Scalar | AnyArrayLike,
4444+
allow_duplicates: bool = False,
4445+
) -> None:
44414446
"""
44424447
Insert column into DataFrame at specified location.
44434448
@@ -4450,8 +4455,8 @@ def insert(self, loc, column, value, allow_duplicates: bool = False) -> None:
44504455
Insertion index. Must verify 0 <= loc <= len(columns).
44514456
column : str, number, or hashable object
44524457
Label of the inserted column.
4453-
value : int, Series, or array-like
4454-
allow_duplicates : bool, optional
4458+
value : Scalar, Series, or array-like
4459+
allow_duplicates : bool, optional default False
44554460
44564461
See Also
44574462
--------
@@ -6633,7 +6638,7 @@ def value_counts(
66336638

66346639
return counts
66356640

6636-
def nlargest(self, n, columns, keep: str = "first") -> DataFrame:
6641+
def nlargest(self, n: int, columns: IndexLabel, keep: str = "first") -> DataFrame:
66376642
"""
66386643
Return the first `n` rows ordered by `columns` in descending order.
66396644
@@ -6740,7 +6745,7 @@ def nlargest(self, n, columns, keep: str = "first") -> DataFrame:
67406745
"""
67416746
return algorithms.SelectNFrame(self, n=n, keep=keep, columns=columns).nlargest()
67426747

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

0 commit comments

Comments
 (0)