|
15 | 15 | from pandas import compat
|
16 | 16 | from pandas.compat import iteritems, PY36, OrderedDict
|
17 | 17 | from pandas.core.dtypes.generic import ABCSeries, ABCIndex, ABCIndexClass
|
18 |
| -from pandas.core.dtypes.common import is_integer |
| 18 | +from pandas.core.dtypes.common import ( |
| 19 | + is_integer, is_bool_dtype, is_extension_array_dtype, is_array_like |
| 20 | +) |
19 | 21 | from pandas.core.dtypes.inference import _iterable_not_string
|
20 | 22 | from pandas.core.dtypes.missing import isna, isnull, notnull # noqa
|
21 | 23 | from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
|
@@ -100,17 +102,45 @@ def maybe_box_datetimelike(value):
|
100 | 102 |
|
101 | 103 |
|
102 | 104 | def is_bool_indexer(key):
|
103 |
| - if isinstance(key, (ABCSeries, np.ndarray, ABCIndex)): |
| 105 | + # type: (Any) -> bool |
| 106 | + """ |
| 107 | + Check whether `key` is a valid boolean indexer. |
| 108 | +
|
| 109 | + Parameters |
| 110 | + ---------- |
| 111 | + key : Any |
| 112 | + Only list-likes may be considered boolean indexers. |
| 113 | + All other types are not considered a boolean indexer. |
| 114 | + For array-like input, boolean ndarrays or ExtensionArrays |
| 115 | + with ``_is_boolean`` set are considered boolean indexers. |
| 116 | +
|
| 117 | + Returns |
| 118 | + ------- |
| 119 | + bool |
| 120 | +
|
| 121 | + Raises |
| 122 | + ------ |
| 123 | + ValueError |
| 124 | + When the array is an object-dtype ndarray or ExtensionArray |
| 125 | + and contains missing values. |
| 126 | + """ |
| 127 | + na_msg = 'cannot index with vector containing NA / NaN values' |
| 128 | + if (isinstance(key, (ABCSeries, np.ndarray, ABCIndex)) or |
| 129 | + (is_array_like(key) and is_extension_array_dtype(key.dtype))): |
104 | 130 | if key.dtype == np.object_:
|
105 | 131 | key = np.asarray(values_from_object(key))
|
106 | 132 |
|
107 | 133 | if not lib.is_bool_array(key):
|
108 | 134 | if isna(key).any():
|
109 |
| - raise ValueError('cannot index with vector containing ' |
110 |
| - 'NA / NaN values') |
| 135 | + raise ValueError(na_msg) |
111 | 136 | return False
|
112 | 137 | return True
|
113 |
| - elif key.dtype == np.bool_: |
| 138 | + elif is_bool_dtype(key.dtype): |
| 139 | + # an ndarray with bool-dtype by definition has no missing values. |
| 140 | + # So we only need to check for NAs in ExtensionArrays |
| 141 | + if is_extension_array_dtype(key.dtype): |
| 142 | + if np.any(key.isna()): |
| 143 | + raise ValueError(na_msg) |
114 | 144 | return True
|
115 | 145 | elif isinstance(key, list):
|
116 | 146 | try:
|
|
0 commit comments