|
28 | 28 | from pandas.core.dtypes.concat import concat_compat
|
29 | 29 | from pandas.core.dtypes.dtypes import ExtensionDtype
|
30 | 30 | 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 |
32 | 32 |
|
33 | 33 | import pandas.core.algorithms as algos
|
34 | 34 | from pandas.core.arrays.sparse import SparseDtype
|
@@ -1937,23 +1937,22 @@ def _compare_or_regex_search(a, b, regex=False):
|
1937 | 1937 | is_b_array = isinstance(b, np.ndarray)
|
1938 | 1938 |
|
1939 | 1939 | 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) |
1947 | 1942 | 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) |
1949 | 1944 | 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) |
1951 | 1946 |
|
1952 | 1947 | if is_datetimelike_v_numeric(a, b) or is_numeric_v_string_like(a, b):
|
1953 | 1948 | # GH#29553 avoid deprecation warnings from numpy
|
1954 | 1949 | result = False
|
1955 | 1950 | else:
|
1956 | 1951 | 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) |
1957 | 1956 |
|
1958 | 1957 | if is_scalar(result) and (is_a_array or is_b_array):
|
1959 | 1958 | type_names = [type(a).__name__, type(b).__name__]
|
|
0 commit comments