pandas.api.indexers.check_bool_array_indexer

pandas.api.indexers.check_bool_array_indexer(array: ~AnyArrayLike, mask: ~AnyArrayLike) → numpy.ndarray[source]

Check if mask is a valid boolean indexer for array.

array and mask are checked to have the same length, and the dtype is validated.

New in version 1.0.0.

Parameters
arrayarray

The array that’s being masked.

maskarray

The boolean array that’s masking.

Returns
numpy.ndarray

The validated boolean mask.

Raises
IndexError

When the lengths don’t match.

ValueError

When mask cannot be converted to a bool-dtype ndarray.

See also

api.types.is_bool_dtype

Check if key is of boolean dtype.

Examples

A boolean ndarray is returned when the arguments are all valid.

>>> mask = pd.array([True, False])
>>> arr = pd.array([1, 2])
>>> pd.api.extensions.check_bool_array_indexer(arr, mask)
array([ True, False])

An IndexError is raised when the lengths don’t match.

>>> mask = pd.array([True, False, True])
>>> pd.api.extensions.check_bool_array_indexer(arr, mask)
Traceback (most recent call last):
...
IndexError: Item wrong length 3 instead of 2.

A ValueError is raised when the mask cannot be converted to a bool-dtype ndarray.

>>> mask = pd.array([True, pd.NA])
>>> pd.api.extensions.check_bool_array_indexer(arr, mask)
Traceback (most recent call last):
...
ValueError: cannot convert to bool numpy array in presence of missing values