Skip to content

Commit e98c7c9

Browse files
committed
BUG: Fix replacing in string series with NA (pandas-dev#32621)
1 parent 719369d commit e98c7c9

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

pandas/core/internals/managers.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from pandas.core.dtypes.concat import concat_compat
2929
from pandas.core.dtypes.dtypes import ExtensionDtype
3030
from pandas.core.dtypes.generic import ABCExtensionArray, ABCSeries
31-
from pandas.core.dtypes.missing import isna
31+
from pandas.core.dtypes.missing import isna, na_value_for_dtype
3232

3333
import pandas.core.algorithms as algos
3434
from pandas.core.arrays.sparse import SparseDtype
@@ -1937,23 +1937,22 @@ def _compare_or_regex_search(a, b, regex=False):
19371937
is_b_array = isinstance(b, np.ndarray)
19381938

19391939
def _get_nan_value(x):
1940-
if np.issubdtype(x.dtype, np.datetime64):
1941-
return np.datetime64("NaT")
1942-
elif np.issubdtype(x.dtype, np.timedelta64):
1943-
return np.timedelta64("NaT")
1944-
return np.nan
1945-
1946-
# Replace all definitions of missing values (isna=True) to a numpy.nan
1940+
# GH#32621 replace all pd.NAs to avoid failure of element-wise comparison
1941+
mask = isna(a) | isna(b)
19471942
if is_a_array:
1948-
a = np.where(isna(a), _get_nan_value(a), a)
1943+
a = np.where(mask, na_value_for_dtype(a.dtype, compat=False), a)
19491944
if is_b_array:
1950-
b = np.where(isna(b), _get_nan_value(b), b)
1945+
b = np.where(mask, na_value_for_dtype(b.dtype, compat=False), b)
19511946

19521947
if is_datetimelike_v_numeric(a, b) or is_numeric_v_string_like(a, b):
19531948
# GH#29553 avoid deprecation warnings from numpy
19541949
result = False
19551950
else:
19561951
result = op(a)
1952+
if isinstance(result, np.ndarray):
1953+
result[mask] = na_value_for_dtype(result.dtype, compat=False)
1954+
elif isna(result):
1955+
result = na_value_for_dtype(np.bool, compat=False)
19571956

19581957
if is_scalar(result) and (is_a_array or is_b_array):
19591958
type_names = [type(a).__name__, type(b).__name__]

0 commit comments

Comments
 (0)