Skip to content

Commit cab3e0e

Browse files
TYP: misc fixes for numpy types (#36098)
1 parent de9c771 commit cab3e0e

File tree

5 files changed

+11
-10
lines changed

5 files changed

+11
-10
lines changed

pandas/_typing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
# other
6363

6464
Dtype = Union[
65-
"ExtensionDtype", str, np.dtype, Type[Union[str, float, int, complex, bool]]
65+
"ExtensionDtype", str, np.dtype, Type[Union[str, float, int, complex, bool, object]]
6666
]
6767
DtypeObj = Union[np.dtype, "ExtensionDtype"]
6868
FilePathOrBuffer = Union[str, Path, IO[AnyStr], IOBase]

pandas/core/algorithms.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import operator
88
from textwrap import dedent
9-
from typing import TYPE_CHECKING, Dict, Optional, Tuple, Union
9+
from typing import TYPE_CHECKING, Dict, Optional, Tuple, Union, cast
1010
from warnings import catch_warnings, simplefilter, warn
1111

1212
import numpy as np
@@ -60,7 +60,7 @@
6060
from pandas.core.indexers import validate_indices
6161

6262
if TYPE_CHECKING:
63-
from pandas import DataFrame, Series
63+
from pandas import Categorical, DataFrame, Series
6464

6565
_shared_docs: Dict[str, str] = {}
6666

@@ -429,8 +429,7 @@ def isin(comps: AnyArrayLike, values: AnyArrayLike) -> np.ndarray:
429429
if is_categorical_dtype(comps):
430430
# TODO(extension)
431431
# handle categoricals
432-
# error: "ExtensionArray" has no attribute "isin" [attr-defined]
433-
return comps.isin(values) # type: ignore[attr-defined]
432+
return cast("Categorical", comps).isin(values)
434433

435434
comps, dtype = _ensure_data(comps)
436435
values, _ = _ensure_data(values, dtype=dtype)

pandas/core/arrays/categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2316,7 +2316,7 @@ def _concat_same_type(self, to_concat):
23162316

23172317
return union_categoricals(to_concat)
23182318

2319-
def isin(self, values):
2319+
def isin(self, values) -> np.ndarray:
23202320
"""
23212321
Check whether `values` are contained in Categorical.
23222322

pandas/core/construction.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ def array(
335335
return result
336336

337337

338-
def extract_array(obj, extract_numpy: bool = False):
338+
def extract_array(obj: AnyArrayLike, extract_numpy: bool = False) -> ArrayLike:
339339
"""
340340
Extract the ndarray or ExtensionArray from a Series or Index.
341341
@@ -383,7 +383,9 @@ def extract_array(obj, extract_numpy: bool = False):
383383
if extract_numpy and isinstance(obj, ABCPandasArray):
384384
obj = obj.to_numpy()
385385

386-
return obj
386+
# error: Incompatible return value type (got "Index", expected "ExtensionArray")
387+
# error: Incompatible return value type (got "Series", expected "ExtensionArray")
388+
return obj # type: ignore[return-value]
387389

388390

389391
def sanitize_array(

pandas/core/dtypes/cast.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1488,7 +1488,7 @@ def find_common_type(types: List[DtypeObj]) -> DtypeObj:
14881488
if has_bools:
14891489
for t in types:
14901490
if is_integer_dtype(t) or is_float_dtype(t) or is_complex_dtype(t):
1491-
return object
1491+
return np.dtype("object")
14921492

14931493
return np.find_common_type(types, [])
14941494

@@ -1550,7 +1550,7 @@ def construct_1d_arraylike_from_scalar(
15501550
elif isinstance(dtype, np.dtype) and dtype.kind in ("U", "S"):
15511551
# we need to coerce to object dtype to avoid
15521552
# to allow numpy to take our string as a scalar value
1553-
dtype = object
1553+
dtype = np.dtype("object")
15541554
if not isna(value):
15551555
value = ensure_str(value)
15561556

0 commit comments

Comments
 (0)