Skip to content

Commit c32a2cc

Browse files
committed
BUG: Fix replacing in string series with NA (#32621)
1 parent 8b6d224 commit c32a2cc

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

pandas/core/internals/managers.py

+29-27
Original file line numberDiff line numberDiff line change
@@ -1941,6 +1941,21 @@ def _compare_or_regex_search(a, b, regex=False):
19411941
-------
19421942
mask : array_like of bool
19431943
"""
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+
19441959
if not regex:
19451960
op = lambda x: operator.eq(x, b)
19461961
else:
@@ -1951,42 +1966,29 @@ def _compare_or_regex_search(a, b, regex=False):
19511966
is_a_array = isinstance(a, np.ndarray)
19521967
is_b_array = isinstance(b, np.ndarray)
19531968

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+
19541981
if is_datetimelike_v_numeric(a, b) or is_numeric_v_string_like(a, b):
19551982
# GH#29553 avoid deprecation warnings from numpy
1956-
result = False
1983+
return _check(False, a, b)
19571984
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]
19701985
result = op(a)
1971-
19721986
if isinstance(result, np.ndarray):
19731987
tmp = np.zeros(mask.shape, dtype=np.bool)
19741988
tmp[mask] = result
19751989
result = tmp
19761990

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)
19901992

19911993

19921994
def _fast_count_smallints(arr):

0 commit comments

Comments
 (0)