Skip to content

TYP: misc fixes for numpy types #36098

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 3 commits into from
Sep 5, 2020
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
2 changes: 1 addition & 1 deletion pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
# other

Dtype = Union[
"ExtensionDtype", str, np.dtype, Type[Union[str, float, int, complex, bool]]
"ExtensionDtype", str, np.dtype, Type[Union[str, float, int, complex, bool, object]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

object here is for when we use object as a shorthand for np.dtype(object)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, but Dtype is also used to type the public facing api. ideally we don't want object here, but we don't want users to have false positives either. saying that we have not yet come to an agreement on how we will make the types public. see #28142

]
DtypeObj = Union[np.dtype, "ExtensionDtype"]
FilePathOrBuffer = Union[str, Path, IO[AnyStr], IOBase]
Expand Down
7 changes: 3 additions & 4 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import operator
from textwrap import dedent
from typing import TYPE_CHECKING, Dict, Optional, Tuple, Union
from typing import TYPE_CHECKING, Dict, Optional, Tuple, Union, cast
from warnings import catch_warnings, simplefilter, warn

import numpy as np
Expand Down Expand Up @@ -60,7 +60,7 @@
from pandas.core.indexers import validate_indices

if TYPE_CHECKING:
from pandas import DataFrame, Series
from pandas import Categorical, DataFrame, Series

_shared_docs: Dict[str, str] = {}

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

comps, dtype = _ensure_data(comps)
values, _ = _ensure_data(values, dtype=dtype)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2316,7 +2316,7 @@ def _concat_same_type(self, to_concat):

return union_categoricals(to_concat)

def isin(self, values):
def isin(self, values) -> np.ndarray:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so here we know that this returns a bool dtyped ndarray, is it worth somehow annotating this as such?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC there has been some discussion in numpy about making it viable to annotate something like np.ndarray[bool], but it is still hypothetical (cc @shoyer?)

"""
Check whether `values` are contained in Categorical.

Expand Down
6 changes: 4 additions & 2 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def array(
return result


def extract_array(obj, extract_numpy: bool = False):
def extract_array(obj: AnyArrayLike, extract_numpy: bool = False) -> ArrayLike:
"""
Extract the ndarray or ExtensionArray from a Series or Index.

Expand Down Expand Up @@ -383,7 +383,9 @@ def extract_array(obj, extract_numpy: bool = False):
if extract_numpy and isinstance(obj, ABCPandasArray):
obj = obj.to_numpy()

return obj
# error: Incompatible return value type (got "Index", expected "ExtensionArray")
# error: Incompatible return value type (got "Series", expected "ExtensionArray")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we avoid this by assigning to a new name before returning?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll look some more, but we are using ABC types here which always create issues with eliminating types so I just added the ignore for now so that the return types can be added.

return obj # type: ignore[return-value]


def sanitize_array(
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1488,7 +1488,7 @@ def find_common_type(types: List[DtypeObj]) -> DtypeObj:
if has_bools:
for t in types:
if is_integer_dtype(t) or is_float_dtype(t) or is_complex_dtype(t):
return object
return np.dtype("object")

return np.find_common_type(types, [])

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

Expand Down