Skip to content

Commit 949accc

Browse files
committed
BUG: Fix replacing in string series with NA (pandas-dev#32621)
1 parent a73e2eb commit 949accc

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

pandas/core/internals/managers.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -1911,7 +1911,9 @@ def _merge_blocks(
19111911
return blocks
19121912

19131913

1914-
def _compare_or_regex_search(a, b, regex=False):
1914+
def _compare_or_regex_search(
1915+
a: Union[ArrayLike, Scalar], b: Union[ArrayLike, Scalar], regex: bool = False
1916+
) -> Union[ArrayLike, bool]:
19151917
"""
19161918
Compare two array_like inputs of the same shape or two scalar values
19171919
@@ -1930,13 +1932,13 @@ def _compare_or_regex_search(a, b, regex=False):
19301932
"""
19311933

19321934
def _check_comparison_types(
1933-
result: Union[ArrayLike, Scalar],
1935+
result: Union[ArrayLike, bool],
19341936
a: Union[ArrayLike, Scalar],
19351937
b: Union[ArrayLike, Scalar],
1936-
) -> Union[ArrayLike, Scalar]:
1938+
) -> Union[ArrayLike, bool]:
19371939
"""
1938-
Raises an error if the two arrays cannot be compared,
1939-
otherwise returns the comparison result as expected.
1940+
Raises an error if the two arrays (a,b) cannot be compared.
1941+
Otherwise, returns the comparison result as expected.
19401942
"""
19411943
if is_scalar(result) and (
19421944
isinstance(a, np.ndarray) or isinstance(b, np.ndarray)
@@ -1958,22 +1960,21 @@ def _check_comparison_types(
19581960
op = lambda x: operator.eq(x, b)
19591961
else:
19601962
op = np.vectorize(
1961-
lambda x: bool(re.search(b, x)) if isinstance(x, str) else False
1963+
lambda x: bool(re.search(b, x))
1964+
if isinstance(x, str) and isinstance(b, str)
1965+
else False
19621966
)
19631967

1964-
is_a_array = isinstance(a, np.ndarray)
1965-
is_b_array = isinstance(b, np.ndarray)
1966-
19671968
# GH#32621 use mask to avoid comparing to NAs
1968-
if is_a_array and not is_b_array:
1969+
if isinstance(a, np.ndarray) and not isinstance(b, np.ndarray):
19691970
mask = np.reshape(~(isna(a)), a.shape)
1970-
elif is_b_array and not is_a_array:
1971+
elif isinstance(b, np.ndarray) and not isinstance(a, np.ndarray):
19711972
mask = np.reshape(~(isna(b)), b.shape)
1972-
elif is_a_array and is_b_array:
1973+
elif isinstance(a, np.ndarray) and isinstance(b, np.ndarray):
19731974
mask = ~(isna(a) | isna(b))
1974-
if is_a_array:
1975+
if isinstance(a, np.ndarray):
19751976
a = a[mask]
1976-
if is_b_array:
1977+
if isinstance(b, np.ndarray):
19771978
b = b[mask]
19781979

19791980
if is_datetimelike_v_numeric(a, b) or is_numeric_v_string_like(a, b):

0 commit comments

Comments
 (0)