|
34 | 34 | is_array_like,
|
35 | 35 | is_bool_dtype,
|
36 | 36 | is_numeric_dtype,
|
37 |
| - is_numeric_v_string_like, |
38 | 37 | is_object_dtype,
|
39 | 38 | needs_i8_conversion,
|
40 | 39 | )
|
@@ -64,75 +63,56 @@ def check_value_size(value, mask: npt.NDArray[np.bool_], length: int):
|
64 | 63 | return value
|
65 | 64 |
|
66 | 65 |
|
67 |
| -def mask_missing(arr: ArrayLike, values_to_mask) -> npt.NDArray[np.bool_]: |
| 66 | +def mask_missing(arr: ArrayLike, value) -> npt.NDArray[np.bool_]: |
68 | 67 | """
|
69 | 68 | Return a masking array of same size/shape as arr
|
70 |
| - with entries equaling any member of values_to_mask set to True |
| 69 | + with entries equaling value set to True. |
71 | 70 |
|
72 | 71 | Parameters
|
73 | 72 | ----------
|
74 | 73 | arr : ArrayLike
|
75 |
| - values_to_mask: list, tuple, or scalar |
| 74 | + value : scalar-like |
| 75 | + Caller has ensured `not is_list_like(value)` and that it can be held |
| 76 | + by `arr`. |
76 | 77 |
|
77 | 78 | Returns
|
78 | 79 | -------
|
79 | 80 | np.ndarray[bool]
|
80 | 81 | """
|
81 |
| - # When called from Block.replace/replace_list, values_to_mask is a scalar |
82 |
| - # known to be holdable by arr. |
83 |
| - # When called from Series._single_replace, values_to_mask is tuple or list |
84 |
| - dtype, values_to_mask = infer_dtype_from(values_to_mask) |
| 82 | + dtype, value = infer_dtype_from(value) |
85 | 83 |
|
86 |
| - if isinstance(dtype, np.dtype): |
87 |
| - values_to_mask = np.array(values_to_mask, dtype=dtype) |
88 |
| - else: |
89 |
| - cls = dtype.construct_array_type() |
90 |
| - if not lib.is_list_like(values_to_mask): |
91 |
| - values_to_mask = [values_to_mask] |
92 |
| - values_to_mask = cls._from_sequence(values_to_mask, dtype=dtype, copy=False) |
93 |
| - |
94 |
| - potential_na = False |
95 |
| - if is_object_dtype(arr.dtype): |
96 |
| - # pre-compute mask to avoid comparison to NA |
97 |
| - potential_na = True |
98 |
| - arr_mask = ~isna(arr) |
99 |
| - |
100 |
| - na_mask = isna(values_to_mask) |
101 |
| - nonna = values_to_mask[~na_mask] |
| 84 | + if isna(value): |
| 85 | + return isna(arr) |
102 | 86 |
|
103 | 87 | # GH 21977
|
104 | 88 | mask = np.zeros(arr.shape, dtype=bool)
|
105 | 89 | if (
|
106 | 90 | is_numeric_dtype(arr.dtype)
|
107 | 91 | and not is_bool_dtype(arr.dtype)
|
108 |
| - and is_bool_dtype(nonna.dtype) |
| 92 | + and lib.is_bool(value) |
109 | 93 | ):
|
| 94 | + # e.g. test_replace_ea_float_with_bool, see GH#62048 |
110 | 95 | pass
|
111 | 96 | elif (
|
112 |
| - is_bool_dtype(arr.dtype) |
113 |
| - and is_numeric_dtype(nonna.dtype) |
114 |
| - and not is_bool_dtype(nonna.dtype) |
| 97 | + is_bool_dtype(arr.dtype) and is_numeric_dtype(dtype) and not lib.is_bool(value) |
115 | 98 | ):
|
| 99 | + # e.g. test_replace_ea_float_with_bool, see GH#62048 |
116 | 100 | pass
|
| 101 | + elif is_numeric_dtype(arr.dtype) and isinstance(value, str): |
| 102 | + # GH#29553 prevent numpy deprecation warnings |
| 103 | + pass |
| 104 | + elif is_object_dtype(arr.dtype): |
| 105 | + # pre-compute mask to avoid comparison to NA |
| 106 | + # e.g. test_replace_na_in_obj_column |
| 107 | + arr_mask = ~isna(arr) |
| 108 | + mask[arr_mask] = arr[arr_mask] == value |
117 | 109 | else:
|
118 |
| - for x in nonna: |
119 |
| - if is_numeric_v_string_like(arr, x): |
120 |
| - # GH#29553 prevent numpy deprecation warnings |
121 |
| - pass |
122 |
| - else: |
123 |
| - if potential_na: |
124 |
| - new_mask = np.zeros(arr.shape, dtype=np.bool_) |
125 |
| - new_mask[arr_mask] = arr[arr_mask] == x |
126 |
| - else: |
127 |
| - new_mask = arr == x |
128 |
| - |
129 |
| - if not isinstance(new_mask, np.ndarray): |
130 |
| - # usually BooleanArray |
131 |
| - new_mask = new_mask.to_numpy(dtype=bool, na_value=False) |
132 |
| - mask |= new_mask |
133 |
| - |
134 |
| - if na_mask.any(): |
135 |
| - mask |= isna(arr) |
| 110 | + new_mask = arr == value |
| 111 | + |
| 112 | + if not isinstance(new_mask, np.ndarray): |
| 113 | + # usually BooleanArray |
| 114 | + new_mask = new_mask.to_numpy(dtype=bool, na_value=False) |
| 115 | + mask = new_mask |
136 | 116 |
|
137 | 117 | return mask
|
138 | 118 |
|
|
0 commit comments