|
7 | 7 | import numpy as np
|
8 | 8 |
|
9 | 9 | from pandas._libs import algos, lib
|
10 |
| -from pandas._typing import DtypeObj |
| 10 | +from pandas._typing import ArrayLike, DtypeObj |
11 | 11 | from pandas.compat._optional import import_optional_dependency
|
12 | 12 |
|
13 | 13 | from pandas.core.dtypes.cast import infer_dtype_from_array
|
14 | 14 | from pandas.core.dtypes.common import (
|
15 | 15 | ensure_float64,
|
16 | 16 | is_integer_dtype,
|
17 | 17 | is_numeric_v_string_like,
|
18 |
| - is_scalar, |
19 | 18 | needs_i8_conversion,
|
20 | 19 | )
|
21 | 20 | from pandas.core.dtypes.missing import isna
|
22 | 21 |
|
23 | 22 |
|
24 |
| -def mask_missing(arr, values_to_mask): |
| 23 | +def mask_missing(arr: ArrayLike, values_to_mask) -> np.ndarray: |
25 | 24 | """
|
26 | 25 | Return a masking array of same size/shape as arr
|
27 | 26 | with entries equaling any member of values_to_mask set to True
|
28 |
| - """ |
29 |
| - dtype, values_to_mask = infer_dtype_from_array(values_to_mask) |
30 | 27 |
|
31 |
| - try: |
32 |
| - values_to_mask = np.array(values_to_mask, dtype=dtype) |
| 28 | + Parameters |
| 29 | + ---------- |
| 30 | + arr : ArrayLike |
| 31 | + values_to_mask: list, tuple, or scalar |
33 | 32 |
|
34 |
| - except Exception: |
35 |
| - values_to_mask = np.array(values_to_mask, dtype=object) |
| 33 | + Returns |
| 34 | + ------- |
| 35 | + np.ndarray[bool] |
| 36 | + """ |
| 37 | + # When called from Block.replace/replace_list, values_to_mask is a scalar |
| 38 | + # known to be holdable by arr. |
| 39 | + # When called from Series._single_replace, values_to_mask is tuple or list |
| 40 | + dtype, values_to_mask = infer_dtype_from_array(values_to_mask) |
| 41 | + values_to_mask = np.array(values_to_mask, dtype=dtype) |
36 | 42 |
|
37 | 43 | na_mask = isna(values_to_mask)
|
38 | 44 | nonna = values_to_mask[~na_mask]
|
39 | 45 |
|
40 |
| - mask = None |
| 46 | + # GH 21977 |
| 47 | + mask = np.zeros(arr.shape, dtype=bool) |
41 | 48 | for x in nonna:
|
42 |
| - if mask is None: |
43 |
| - if is_numeric_v_string_like(arr, x): |
44 |
| - # GH#29553 prevent numpy deprecation warnings |
45 |
| - mask = False |
46 |
| - else: |
47 |
| - mask = arr == x |
48 |
| - |
49 |
| - # if x is a string and arr is not, then we get False and we must |
50 |
| - # expand the mask to size arr.shape |
51 |
| - if is_scalar(mask): |
52 |
| - mask = np.zeros(arr.shape, dtype=bool) |
| 49 | + if is_numeric_v_string_like(arr, x): |
| 50 | + # GH#29553 prevent numpy deprecation warnings |
| 51 | + pass |
53 | 52 | else:
|
54 |
| - if is_numeric_v_string_like(arr, x): |
55 |
| - # GH#29553 prevent numpy deprecation warnings |
56 |
| - mask |= False |
57 |
| - else: |
58 |
| - mask |= arr == x |
| 53 | + mask |= arr == x |
59 | 54 |
|
60 | 55 | if na_mask.any():
|
61 |
| - if mask is None: |
62 |
| - mask = isna(arr) |
63 |
| - else: |
64 |
| - mask |= isna(arr) |
65 |
| - |
66 |
| - # GH 21977 |
67 |
| - if mask is None: |
68 |
| - mask = np.zeros(arr.shape, dtype=bool) |
| 56 | + mask |= isna(arr) |
69 | 57 |
|
70 | 58 | return mask
|
71 | 59 |
|
|
0 commit comments