pandas 1.4.2

ParametersRaisesReturnsBackRef
check_array_indexer(array: 'AnyArrayLike', indexer: 'Any') -> 'Any'

For a boolean mask, array and :None:None:`indexer` are checked to have the same length. The dtype is validated, and if it is an integer or boolean ExtensionArray, it is checked if there are missing values present, and it is converted to the appropriate numpy array. Other dtypes will raise an error.

Non-array indexers (integer, slice, Ellipsis, tuples, ..) are passed through as is.

versionadded

Parameters

array : array-like

The array that is being indexed (only used for the length).

indexer : array-like or list-like

The array-like that's used to index. List-like input that is not yet a numpy array or an ExtensionArray is converted to one. Other input types are passed through as is.

Raises

IndexError

When the lengths don't match.

ValueError

When :None:None:`indexer` cannot be converted to a numpy ndarray to index (e.g. presence of missing values).

Returns

numpy.ndarray

The validated indexer as a numpy array that can be used to index.

Check if :None:None:`indexer` is a valid array indexer for array .

See Also

api.types.is_bool_dtype

Check if :None:None:`key` is of boolean dtype.

Examples

When checking a boolean mask, a boolean ndarray is returned when the arguments are all valid.

This example is valid syntax, but we were not able to check execution
>>> mask = pd.array([True, False])
... arr = pd.array([1, 2])
... pd.api.indexers.check_array_indexer(arr, mask) array([ True, False])

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

This example is valid syntax, but we were not able to check execution
>>> mask = pd.array([True, False, True])
... pd.api.indexers.check_array_indexer(arr, mask) Traceback (most recent call last): ... IndexError: Boolean index has wrong length: 3 instead of 2.

NA values in a boolean array are treated as False.

This example is valid syntax, but we were not able to check execution
>>> mask = pd.array([True, pd.NA])
... pd.api.indexers.check_array_indexer(arr, mask) array([ True, False])

A numpy boolean mask will get passed through (if the length is correct):

This example is valid syntax, but we were not able to check execution
>>> mask = np.array([True, False])
... pd.api.indexers.check_array_indexer(arr, mask) array([ True, False])

Similarly for integer indexers, an integer ndarray is returned when it is a valid indexer, otherwise an error is (for integer indexers, a matching length is not required):

This example is valid syntax, but we were not able to check execution
>>> indexer = pd.array([0, 2], dtype="Int64")
... arr = pd.array([1, 2, 3])
... pd.api.indexers.check_array_indexer(arr, indexer) array([0, 2])
This example is valid syntax, but we were not able to check execution
>>> indexer = pd.array([0, pd.NA], dtype="Int64")
... pd.api.indexers.check_array_indexer(arr, indexer) Traceback (most recent call last): ... ValueError: Cannot index with an integer indexer containing NA values

For non-integer/boolean dtypes, an appropriate error is raised:

This example is valid syntax, but we were not able to check execution
>>> indexer = np.array([0., 2.], dtype="float64")
... pd.api.indexers.check_array_indexer(arr, indexer) Traceback (most recent call last): ... IndexError: arrays used as indices must be of integer or boolean type
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.common.is_bool_indexer

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/indexers/utils.py#457
type: <class 'function'>
Commit: