Skip to content

Commit 37ea95e

Browse files
committed
fixup
1 parent d4f0adc commit 37ea95e

File tree

13 files changed

+84
-89
lines changed

13 files changed

+84
-89
lines changed

ci/code_checks.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then
121121
# Check for imports from pandas.core.common instead of `import pandas.core.common as com`
122122
# Check for imports from collections.abc instead of `from collections import abc`
123123
MSG='Check for non-standard imports' ; echo $MSG
124-
invgrep -R --include="*.py*" --exclude="__init__.py" -E "from pandas.core.common import" pandas
124+
invgrep -R --include="*.py*" -E "from pandas.core.common import" pandas
125125
RET=$(($RET + $?)) ; echo $MSG "DONE"
126126
invgrep -R --include="*.py*" -E "from pandas.core import common" pandas
127127
RET=$(($RET + $?)) ; echo $MSG "DONE"

doc/source/reference/extensions.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ objects.
1818
api.extensions.register_series_accessor
1919
api.extensions.register_index_accessor
2020
api.extensions.ExtensionDtype
21-
api.extensions.is_bool_indexer
22-
api.extensions.check_bool_array_indexer
2321

2422
.. autosummary::
2523
:toctree: api/
@@ -28,7 +26,6 @@ objects.
2826
api.extensions.ExtensionArray
2927
arrays.PandasArray
3028

31-
3229
.. We need this autosummary so that methods and attributes are generated.
3330
.. Separate block, since they aren't classes.
3431

pandas/api/extensions/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,3 @@
1111
)
1212
from pandas.core.algorithms import take # noqa: F401
1313
from pandas.core.arrays import ExtensionArray, ExtensionScalarOpsMixin # noqa: F401
14-
from pandas.core.common import is_bool_indexer # noqa: F401
15-
from pandas.core.indexing import check_bool_array_indexer # noqa: F401

pandas/core/arrays/boolean.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ def _hasna(self) -> bool:
317317

318318
def __getitem__(self, item):
319319
# import here to avoid circular import. Probably need to restructure
320-
from pandas.core.indexing import check_bool_array_indexer
320+
from pandas.core.indexers import check_bool_array_indexer
321321

322322
if is_integer(item):
323323
if self._mask[item]:

pandas/core/arrays/categorical.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1990,7 +1990,7 @@ def __getitem__(self, key):
19901990
"""
19911991
Return an item.
19921992
"""
1993-
from pandas.core.indexing import check_bool_array_indexer
1993+
from pandas.core.indexers import check_bool_array_indexer
19941994

19951995
if isinstance(key, (int, np.integer)):
19961996
i = self._codes[key]

pandas/core/arrays/datetimelike.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ def __getitem__(self, key):
437437
return type(self)(val, dtype=self.dtype)
438438

439439
if com.is_bool_indexer(key):
440-
from pandas.core.indexing import check_bool_array_indexer
440+
from pandas.core.indexers import check_bool_array_indexer
441441

442442
key = check_bool_array_indexer(self, key)
443443
if key.all():

pandas/core/arrays/integer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ def _from_factorized(cls, values, original):
366366

367367
def __getitem__(self, item):
368368
# Importing this at the top-level causes many unrelated(?) mypy failures
369-
from pandas.core.indexing import check_bool_array_indexer
369+
from pandas.core.indexers import check_bool_array_indexer
370370

371371
if is_integer(item):
372372
if self._mask[item]:

pandas/core/arrays/numpy_.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
233233

234234
def __getitem__(self, item):
235235
# Avoid mypy failures when importing at the top-level
236-
from pandas.core.indexing import check_bool_array_indexer
236+
from pandas.core.indexers import check_bool_array_indexer
237237

238238
if isinstance(item, type(self)):
239239
item = item._ndarray

pandas/core/common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ def is_bool_indexer(key: Any) -> bool:
121121
122122
See Also
123123
--------
124-
api.extensions.check_bool_array_indexer : Check that `key`
125-
is a valid mask for an array, and convert to an ndarary.
124+
check_bool_array_indexer : Check that `key`
125+
is a valid mask for an array, and convert to an ndarray.
126126
"""
127127
na_msg = "cannot mask with array containing NA / NaN values"
128128
if isinstance(key, (ABCSeries, np.ndarray, ABCIndex)) or (

pandas/core/indexers.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
"""
44
import numpy as np
55

6+
from pandas._typing import AnyArrayLike
7+
68
from pandas.core.dtypes.common import is_list_like
79
from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries
810

@@ -240,3 +242,66 @@ def length_of_indexer(indexer, target=None) -> int:
240242
elif not is_list_like_indexer(indexer):
241243
return 1
242244
raise AssertionError("cannot find the length of the indexer")
245+
246+
247+
def check_bool_array_indexer(array: AnyArrayLike, mask: AnyArrayLike) -> np.ndarray:
248+
"""
249+
Check if `mask` is a valid boolean indexer for `array`.
250+
251+
`array` and `mask` are checked to have the same length, and the
252+
dtype is validated.
253+
254+
Parameters
255+
----------
256+
array : array
257+
The array that's being masked.
258+
mask : array
259+
The boolean array that's masking.
260+
261+
Returns
262+
-------
263+
numpy.ndarray
264+
The validated boolean mask.
265+
266+
Raises
267+
------
268+
IndexError
269+
When the lengths don't match.
270+
ValueError
271+
When `mask` cannot be converted to a bool-dtype ndarray.
272+
273+
See Also
274+
--------
275+
api.extensions.is_bool_indexer : Check if `key` is a boolean indexer.
276+
277+
Examples
278+
--------
279+
A boolean ndarray is returned when the arguments are all valid.
280+
281+
>>> mask = pd.array([True, False])
282+
>>> arr = pd.Series([1, 2])
283+
>>> pd.api.extensions.check_bool_array_indexer(arr, mask)
284+
array([ True, False])
285+
286+
An IndexError is raised when the lengths don't match.
287+
288+
>>> mask = pd.array([True, False, True])
289+
>>> pd.api.extensions.check_bool_array_indexer(arr, mask)
290+
Traceback (most recent call last):
291+
...
292+
IndexError: Item wrong length 3 instead of 2.
293+
294+
A ValueError is raised when the mask cannot be converted to
295+
a bool-dtype ndarray.
296+
297+
>>> mask = pd.array([True, pd.NA])
298+
>>> pd.api.extensions.check_bool_array_indexer(arr, mask)
299+
Traceback (most recent call last):
300+
...
301+
ValueError: cannot convert to bool numpy array in presence of missing values
302+
"""
303+
result = np.asarray(mask, dtype=bool)
304+
# GH26658
305+
if len(result) != len(array):
306+
raise IndexError(f"Item wrong length {len(result)} instead of {len(array)}.")
307+
return result

0 commit comments

Comments
 (0)