Skip to content

CLN: trim unreachable branches in _compare_or_regex_search #33598

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 1 commit into from
Apr 17, 2020
Merged
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
34 changes: 11 additions & 23 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1739,7 +1739,7 @@ def form_blocks(arrays, names: Index, axes) -> List[Block]:
return blocks


def _simple_blockify(tuples, dtype):
def _simple_blockify(tuples, dtype) -> List[Block]:
"""
return a single array of a block that has a single dtype; if dtype is
not None, coerce to this dtype
Expand Down Expand Up @@ -1862,7 +1862,7 @@ def _merge_blocks(


def _compare_or_regex_search(
a: Union[ArrayLike, Scalar], b: Union[ArrayLike, Scalar], regex: bool = False
a: ArrayLike, b: Scalar, regex: bool = False
) -> Union[ArrayLike, bool]:
"""
Compare two array_like inputs of the same shape or two scalar values
Expand All @@ -1872,8 +1872,8 @@ def _compare_or_regex_search(

Parameters
----------
a : array_like or scalar
b : array_like or scalar
a : array_like
b : scalar
regex : bool, default False

Returns
Expand All @@ -1882,29 +1882,21 @@ def _compare_or_regex_search(
"""

def _check_comparison_types(
result: Union[ArrayLike, bool],
a: Union[ArrayLike, Scalar],
b: Union[ArrayLike, Scalar],
) -> Union[ArrayLike, bool]:
result: Union[ArrayLike, bool], a: ArrayLike, b: Scalar,
):
"""
Raises an error if the two arrays (a,b) cannot be compared.
Otherwise, returns the comparison result as expected.
"""
if is_scalar(result) and (
isinstance(a, np.ndarray) or isinstance(b, np.ndarray)
):
if is_scalar(result) and isinstance(a, np.ndarray):
type_names = [type(a).__name__, type(b).__name__]

if isinstance(a, np.ndarray):
type_names[0] = f"ndarray(dtype={a.dtype})"

if isinstance(b, np.ndarray):
type_names[1] = f"ndarray(dtype={b.dtype})"

raise TypeError(
f"Cannot compare types {repr(type_names[0])} and {repr(type_names[1])}"
)
return result

if not regex:
op = lambda x: operator.eq(x, b)
Expand All @@ -1918,18 +1910,13 @@ def _check_comparison_types(
# GH#32621 use mask to avoid comparing to NAs
if isinstance(a, np.ndarray) and not isinstance(b, np.ndarray):
mask = np.reshape(~(isna(a)), a.shape)
elif isinstance(b, np.ndarray) and not isinstance(a, np.ndarray):
mask = np.reshape(~(isna(b)), b.shape)
elif isinstance(a, np.ndarray) and isinstance(b, np.ndarray):
mask = ~(isna(a) | isna(b))
if isinstance(a, np.ndarray):
a = a[mask]
if isinstance(b, np.ndarray):
b = b[mask]

if is_datetimelike_v_numeric(a, b) or is_numeric_v_string_like(a, b):
# GH#29553 avoid deprecation warnings from numpy
return _check_comparison_types(False, a, b)
_check_comparison_types(False, a, b)
return False

result = op(a)

Expand All @@ -1940,7 +1927,8 @@ def _check_comparison_types(
tmp[mask] = result
result = tmp

return _check_comparison_types(result, a, b)
_check_comparison_types(result, a, b)
return result


def _fast_count_smallints(arr: np.ndarray) -> np.ndarray:
Expand Down