Skip to content

Commit 0edd2d9

Browse files
authored
annotations (#32193)
1 parent 7b99f03 commit 0edd2d9

File tree

3 files changed

+27
-41
lines changed

3 files changed

+27
-41
lines changed

pandas/core/internals/managers.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import numpy as np
99

1010
from pandas._libs import Timedelta, Timestamp, internals as libinternals, lib
11+
from pandas._typing import DtypeObj
1112
from pandas.util._validators import validate_bool_kwarg
1213

1314
from pandas.core.dtypes.cast import (
@@ -847,7 +848,7 @@ def to_dict(self, copy: bool = True):
847848

848849
return {dtype: self.combine(blocks, copy=copy) for dtype, blocks in bd.items()}
849850

850-
def fast_xs(self, loc):
851+
def fast_xs(self, loc: int):
851852
"""
852853
get a cross sectional for a given location in the
853854
items ; handle dups
@@ -883,12 +884,12 @@ def fast_xs(self, loc):
883884
for i, rl in enumerate(blk.mgr_locs):
884885
result[rl] = blk.iget((i, loc))
885886

886-
if is_extension_array_dtype(dtype):
887+
if isinstance(dtype, ExtensionDtype):
887888
result = dtype.construct_array_type()._from_sequence(result, dtype=dtype)
888889

889890
return result
890891

891-
def consolidate(self):
892+
def consolidate(self) -> "BlockManager":
892893
"""
893894
Join together blocks having same dtype
894895
@@ -940,7 +941,7 @@ def get(self, item):
940941
new_axis=self.items[indexer], indexer=indexer, axis=0, allow_dups=True
941942
)
942943

943-
def iget(self, i):
944+
def iget(self, i: int) -> "SingleBlockManager":
944945
"""
945946
Return the data as a SingleBlockManager.
946947
"""
@@ -1377,7 +1378,7 @@ def canonicalize(block):
13771378
block.equals(oblock) for block, oblock in zip(self_blocks, other_blocks)
13781379
)
13791380

1380-
def unstack(self, unstacker_func, fill_value):
1381+
def unstack(self, unstacker_func, fill_value) -> "BlockManager":
13811382
"""
13821383
Return a BlockManager with all blocks unstacked..
13831384
@@ -1396,8 +1397,8 @@ def unstack(self, unstacker_func, fill_value):
13961397
dummy = unstacker_func(np.empty((0, 0)), value_columns=self.items)
13971398
new_columns = dummy.get_new_columns()
13981399
new_index = dummy.get_new_index()
1399-
new_blocks = []
1400-
columns_mask = []
1400+
new_blocks: List[Block] = []
1401+
columns_mask: List[np.ndarray] = []
14011402

14021403
for blk in self.blocks:
14031404
blocks, mask = blk._unstack(
@@ -1478,7 +1479,7 @@ def _post_setstate(self):
14781479
pass
14791480

14801481
@property
1481-
def _block(self):
1482+
def _block(self) -> Block:
14821483
return self.blocks[0]
14831484

14841485
@property
@@ -1495,14 +1496,14 @@ def _blklocs(self):
14951496
""" compat with BlockManager """
14961497
return None
14971498

1498-
def get_slice(self, slobj, axis=0):
1499+
def get_slice(self, slobj: slice, axis: int = 0) -> "SingleBlockManager":
14991500
if axis >= self.ndim:
15001501
raise IndexError("Requested axis not found in manager")
15011502

1502-
return type(self)(self._block._slice(slobj), self.index[slobj], fastpath=True,)
1503+
return type(self)(self._block._slice(slobj), self.index[slobj], fastpath=True)
15031504

15041505
@property
1505-
def index(self):
1506+
def index(self) -> Index:
15061507
return self.axes[0]
15071508

15081509
@property
@@ -1516,7 +1517,7 @@ def array_dtype(self):
15161517
def get_dtype_counts(self):
15171518
return {self.dtype.name: 1}
15181519

1519-
def get_dtypes(self):
1520+
def get_dtypes(self) -> np.ndarray:
15201521
return np.array([self._block.dtype])
15211522

15221523
def external_values(self):
@@ -1527,15 +1528,15 @@ def internal_values(self):
15271528
"""The array that Series._values returns"""
15281529
return self._block.internal_values()
15291530

1530-
def get_values(self):
1531+
def get_values(self) -> np.ndarray:
15311532
""" return a dense type view """
15321533
return np.array(self._block.to_dense(), copy=False)
15331534

15341535
@property
15351536
def _can_hold_na(self) -> bool:
15361537
return self._block._can_hold_na
15371538

1538-
def is_consolidated(self):
1539+
def is_consolidated(self) -> bool:
15391540
return True
15401541

15411542
def _consolidate_check(self):
@@ -1813,9 +1814,7 @@ def _shape_compat(x):
18131814
return stacked, placement
18141815

18151816

1816-
def _interleaved_dtype(
1817-
blocks: List[Block],
1818-
) -> Optional[Union[np.dtype, ExtensionDtype]]:
1817+
def _interleaved_dtype(blocks: Sequence[Block]) -> Optional[DtypeObj]:
18191818
"""
18201819
Find the common dtype for `blocks`.
18211820
@@ -1825,7 +1824,7 @@ def _interleaved_dtype(
18251824
18261825
Returns
18271826
-------
1828-
dtype : Optional[Union[np.dtype, ExtensionDtype]]
1827+
dtype : np.dtype, ExtensionDtype, or None
18291828
None is returned when `blocks` is empty.
18301829
"""
18311830
if not len(blocks):

pandas/core/ops/__init__.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,17 @@
55
"""
66
import datetime
77
import operator
8-
from typing import TYPE_CHECKING, Optional, Set, Tuple, Union
8+
from typing import TYPE_CHECKING, Optional, Set, Tuple
99

1010
import numpy as np
1111

1212
from pandas._libs import Timedelta, Timestamp, lib
1313
from pandas._libs.ops_dispatch import maybe_dispatch_ufunc_to_dunder_op # noqa:F401
14-
from pandas._typing import Level
14+
from pandas._typing import ArrayLike, Level
1515
from pandas.util._decorators import Appender
1616

1717
from pandas.core.dtypes.common import is_list_like, is_timedelta64_dtype
18-
from pandas.core.dtypes.generic import (
19-
ABCDataFrame,
20-
ABCExtensionArray,
21-
ABCIndexClass,
22-
ABCSeries,
23-
)
18+
from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries
2419
from pandas.core.dtypes.missing import isna
2520

2621
from pandas.core.construction import extract_array
@@ -451,10 +446,7 @@ def _align_method_SERIES(left, right, align_asobject=False):
451446

452447

453448
def _construct_result(
454-
left: ABCSeries,
455-
result: Union[np.ndarray, ABCExtensionArray],
456-
index: ABCIndexClass,
457-
name,
449+
left: ABCSeries, result: ArrayLike, index: ABCIndexClass, name,
458450
):
459451
"""
460452
Construct an appropriately-labelled Series from the result of an op.

pandas/core/ops/array_ops.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
"""
55
from functools import partial
66
import operator
7-
from typing import Any, Optional, Union
7+
from typing import Any, Optional
88

99
import numpy as np
1010

1111
from pandas._libs import Timedelta, Timestamp, lib, ops as libops
12+
from pandas._typing import ArrayLike
1213

1314
from pandas.core.dtypes.cast import (
1415
construct_1d_object_array_from_listlike,
@@ -155,9 +156,7 @@ def na_arithmetic_op(left, right, op, str_rep: str):
155156
return missing.dispatch_fill_zeros(op, left, right, result)
156157

157158

158-
def arithmetic_op(
159-
left: Union[np.ndarray, ABCExtensionArray], right: Any, op, str_rep: str
160-
):
159+
def arithmetic_op(left: ArrayLike, right: Any, op, str_rep: str):
161160
"""
162161
Evaluate an arithmetic operation `+`, `-`, `*`, `/`, `//`, `%`, `**`, ...
163162
@@ -200,9 +199,7 @@ def arithmetic_op(
200199
return res_values
201200

202201

203-
def comparison_op(
204-
left: Union[np.ndarray, ABCExtensionArray], right: Any, op
205-
) -> Union[np.ndarray, ABCExtensionArray]:
202+
def comparison_op(left: ArrayLike, right: Any, op) -> ArrayLike:
206203
"""
207204
Evaluate a comparison operation `=`, `!=`, `>=`, `>`, `<=`, or `<`.
208205
@@ -215,7 +212,7 @@ def comparison_op(
215212
216213
Returns
217214
-------
218-
ndarrray or ExtensionArray
215+
ndarray or ExtensionArray
219216
"""
220217
# NB: We assume extract_array has already been called on left and right
221218
lvalues = left
@@ -302,9 +299,7 @@ def na_logical_op(x: np.ndarray, y, op):
302299
return result.reshape(x.shape)
303300

304301

305-
def logical_op(
306-
left: Union[np.ndarray, ABCExtensionArray], right: Any, op
307-
) -> Union[np.ndarray, ABCExtensionArray]:
302+
def logical_op(left: ArrayLike, right: Any, op) -> ArrayLike:
308303
"""
309304
Evaluate a logical operation `|`, `&`, or `^`.
310305

0 commit comments

Comments
 (0)