@@ -1941,6 +1941,21 @@ def _compare_or_regex_search(a, b, regex=False):
1941
1941
-------
1942
1942
mask : array_like of bool
1943
1943
"""
1944
+ def _check (result , a , b ):
1945
+ if is_scalar (result ) and (isinstance (a , np .ndarray ) or isinstance (b , np .ndarray )):
1946
+ type_names = [type (a ).__name__ , type (b ).__name__ ]
1947
+
1948
+ if is_a_array :
1949
+ type_names [0 ] = f"ndarray(dtype={ a .dtype } )"
1950
+
1951
+ if is_b_array :
1952
+ type_names [1 ] = f"ndarray(dtype={ b .dtype } )"
1953
+
1954
+ raise TypeError (
1955
+ f"Cannot compare types { repr (type_names [0 ])} and { repr (type_names [1 ])} "
1956
+ )
1957
+ return result
1958
+
1944
1959
if not regex :
1945
1960
op = lambda x : operator .eq (x , b )
1946
1961
else :
@@ -1951,42 +1966,29 @@ def _compare_or_regex_search(a, b, regex=False):
1951
1966
is_a_array = isinstance (a , np .ndarray )
1952
1967
is_b_array = isinstance (b , np .ndarray )
1953
1968
1969
+ # GH#32621 use mask to avoid comparing to NAs
1970
+ if is_a_array and not is_b_array :
1971
+ mask = np .reshape (~ (isna (a )), a .shape )
1972
+ elif is_b_array and not is_a_array :
1973
+ mask = np .reshape (~ (isna (b )), b .shape )
1974
+ elif is_a_array and is_b_array :
1975
+ mask = ~ (isna (a ) | isna (b ))
1976
+ if is_a_array :
1977
+ a = a [mask ]
1978
+ if is_b_array :
1979
+ b = b [mask ]
1980
+
1954
1981
if is_datetimelike_v_numeric (a , b ) or is_numeric_v_string_like (a , b ):
1955
1982
# GH#29553 avoid deprecation warnings from numpy
1956
- result = False
1983
+ return _check ( False , a , b )
1957
1984
else :
1958
- # GH#32621 use mask to avoid comparing to NAs
1959
- if is_a_array and not is_b_array :
1960
- mask = np .reshape (~ (isna (a )), a .shape )
1961
- elif is_b_array and not is_a_array :
1962
- mask = np .reshape (~ (isna (b )), b .shape )
1963
- elif is_a_array and is_b_array :
1964
- mask = ~ (isna (a ) | isna (b ))
1965
-
1966
- if is_a_array :
1967
- a = a [mask ]
1968
- if is_b_array :
1969
- b = b [mask ]
1970
1985
result = op (a )
1971
-
1972
1986
if isinstance (result , np .ndarray ):
1973
1987
tmp = np .zeros (mask .shape , dtype = np .bool )
1974
1988
tmp [mask ] = result
1975
1989
result = tmp
1976
1990
1977
- if is_scalar (result ) and (is_a_array or is_b_array ):
1978
- type_names = [type (a ).__name__ , type (b ).__name__ ]
1979
-
1980
- if is_a_array :
1981
- type_names [0 ] = f"ndarray(dtype={ a .dtype } )"
1982
-
1983
- if is_b_array :
1984
- type_names [1 ] = f"ndarray(dtype={ b .dtype } )"
1985
-
1986
- raise TypeError (
1987
- f"Cannot compare types { repr (type_names [0 ])} and { repr (type_names [1 ])} "
1988
- )
1989
- return result
1991
+ return _check (result , a , b )
1990
1992
1991
1993
1992
1994
def _fast_count_smallints (arr ):
0 commit comments