Skip to content

Commit 48098ce

Browse files
authored
TYP: Annotations (#35933)
1 parent 9632f2e commit 48098ce

File tree

8 files changed

+36
-15
lines changed

8 files changed

+36
-15
lines changed

pandas/core/algorithms.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import numpy as np
1111

1212
from pandas._libs import Timestamp, algos, hashtable as htable, iNaT, lib
13-
from pandas._typing import AnyArrayLike, ArrayLike, DtypeObj
13+
from pandas._typing import AnyArrayLike, ArrayLike, DtypeObj, FrameOrSeriesUnion
1414
from pandas.util._decorators import doc
1515

1616
from pandas.core.dtypes.cast import (
@@ -58,7 +58,7 @@
5858
from pandas.core.indexers import validate_indices
5959

6060
if TYPE_CHECKING:
61-
from pandas import Series
61+
from pandas import DataFrame, Series
6262

6363
_shared_docs: Dict[str, str] = {}
6464

@@ -1101,6 +1101,9 @@ def __init__(self, obj, n: int, keep: str):
11011101
if self.keep not in ("first", "last", "all"):
11021102
raise ValueError('keep must be either "first", "last" or "all"')
11031103

1104+
def compute(self, method: str) -> FrameOrSeriesUnion:
1105+
raise NotImplementedError
1106+
11041107
def nlargest(self):
11051108
return self.compute("nlargest")
11061109

@@ -1133,7 +1136,7 @@ class SelectNSeries(SelectN):
11331136
nordered : Series
11341137
"""
11351138

1136-
def compute(self, method):
1139+
def compute(self, method: str) -> "Series":
11371140

11381141
n = self.n
11391142
dtype = self.obj.dtype
@@ -1207,7 +1210,7 @@ def __init__(self, obj, n: int, keep: str, columns):
12071210
columns = list(columns)
12081211
self.columns = columns
12091212

1210-
def compute(self, method):
1213+
def compute(self, method: str) -> "DataFrame":
12111214

12121215
from pandas import Int64Index
12131216

pandas/core/arrays/base.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,10 @@ class ExtensionOpsMixin:
11671167
with NumPy arrays.
11681168
"""
11691169

1170+
@classmethod
1171+
def _create_arithmetic_method(cls, op):
1172+
raise AbstractMethodError(cls)
1173+
11701174
@classmethod
11711175
def _add_arithmetic_ops(cls):
11721176
cls.__add__ = cls._create_arithmetic_method(operator.add)
@@ -1186,6 +1190,10 @@ def _add_arithmetic_ops(cls):
11861190
cls.__divmod__ = cls._create_arithmetic_method(divmod)
11871191
cls.__rdivmod__ = cls._create_arithmetic_method(ops.rdivmod)
11881192

1193+
@classmethod
1194+
def _create_comparison_method(cls, op):
1195+
raise AbstractMethodError(cls)
1196+
11891197
@classmethod
11901198
def _add_comparison_ops(cls):
11911199
cls.__eq__ = cls._create_comparison_method(operator.eq)
@@ -1195,6 +1203,10 @@ def _add_comparison_ops(cls):
11951203
cls.__le__ = cls._create_comparison_method(operator.le)
11961204
cls.__ge__ = cls._create_comparison_method(operator.ge)
11971205

1206+
@classmethod
1207+
def _create_logical_method(cls, op):
1208+
raise AbstractMethodError(cls)
1209+
11981210
@classmethod
11991211
def _add_logical_ops(cls):
12001212
cls.__and__ = cls._create_logical_method(operator.and_)

pandas/core/groupby/base.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,30 @@
44
SeriesGroupBy and the DataFrameGroupBy objects.
55
"""
66
import collections
7+
from typing import List
78

89
from pandas.core.dtypes.common import is_list_like, is_scalar
910

11+
from pandas.core.base import PandasObject
12+
1013
OutputKey = collections.namedtuple("OutputKey", ["label", "position"])
1114

1215

13-
class GroupByMixin:
16+
class GroupByMixin(PandasObject):
1417
"""
1518
Provide the groupby facilities to the mixed object.
1619
"""
1720

21+
_attributes: List[str]
22+
1823
def _gotitem(self, key, ndim, subset=None):
1924
"""
2025
Sub-classes to define. Return a sliced object.
2126
2227
Parameters
2328
----------
2429
key : string / list of selections
25-
ndim : 1,2
30+
ndim : {1, 2}
2631
requested ndim of result
2732
subset : object, default None
2833
subset to act on

pandas/core/indexes/base.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3541,10 +3541,7 @@ def _join_multi(self, other, how, return_indexers=True):
35413541
if not overlap:
35423542
raise ValueError("cannot join with no overlapping index names")
35433543

3544-
self_is_mi = isinstance(self, ABCMultiIndex)
3545-
other_is_mi = isinstance(other, ABCMultiIndex)
3546-
3547-
if self_is_mi and other_is_mi:
3544+
if isinstance(self, MultiIndex) and isinstance(other, MultiIndex):
35483545

35493546
# Drop the non-matching levels from left and right respectively
35503547
ldrop_names = list(self_names - overlap)
@@ -3590,7 +3587,7 @@ def _join_multi(self, other, how, return_indexers=True):
35903587
# Case where only one index is multi
35913588
# make the indices into mi's that match
35923589
flip_order = False
3593-
if self_is_mi:
3590+
if isinstance(self, MultiIndex):
35943591
self, other = other, self
35953592
flip_order = True
35963593
# flip if join method is right or left

pandas/core/indexes/datetimelike.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Base and utility classes for tseries type pandas objects.
33
"""
4-
from datetime import datetime
4+
from datetime import datetime, tzinfo
55
from typing import Any, List, Optional, TypeVar, Union, cast
66

77
import numpy as np
@@ -630,6 +630,8 @@ class DatetimeTimedeltaMixin(DatetimeIndexOpsMixin, Int64Index):
630630
but not PeriodIndex
631631
"""
632632

633+
tz: Optional[tzinfo]
634+
633635
# Compat for frequency inference, see GH#23789
634636
_is_monotonic_increasing = Index.is_monotonic_increasing
635637
_is_monotonic_decreasing = Index.is_monotonic_decreasing

pandas/core/indexes/numeric.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class NumericIndex(Index):
4545
This is an abstract class.
4646
"""
4747

48+
_default_dtype: np.dtype
49+
4850
_is_numeric_dtype = True
4951

5052
def __new__(cls, data=None, dtype=None, copy=False, name=None):

pandas/core/internals/blocks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,7 @@ def where_func(cond, values, other):
13821382
cond = cond.swapaxes(axis, 0)
13831383
mask = np.array([cond[i].all() for i in range(cond.shape[0])], dtype=bool)
13841384

1385-
result_blocks = []
1385+
result_blocks: List["Block"] = []
13861386
for m in [mask, ~mask]:
13871387
if m.any():
13881388
taken = result.take(m.nonzero()[0], axis=axis)

pandas/core/internals/managers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ def reduce(self: T, func) -> T:
334334
# If 2D, we assume that we're operating column-wise
335335
assert self.ndim == 2
336336

337-
res_blocks = []
337+
res_blocks: List[Block] = []
338338
for blk in self.blocks:
339339
nbs = blk.reduce(func)
340340
res_blocks.extend(nbs)
@@ -728,7 +728,7 @@ def _combine(self, blocks: List[Block], copy: bool = True) -> "BlockManager":
728728
indexer = np.sort(np.concatenate([b.mgr_locs.as_array for b in blocks]))
729729
inv_indexer = lib.get_reverse_indexer(indexer, self.shape[0])
730730

731-
new_blocks = []
731+
new_blocks: List[Block] = []
732732
for b in blocks:
733733
b = b.copy(deep=copy)
734734
b.mgr_locs = inv_indexer[b.mgr_locs.indexer]

0 commit comments

Comments
 (0)