MCPcopy
hub / github.com/pandas-dev/pandas / check_bool_indexer

Function check_bool_indexer

pandas/core/indexing.py:2647–2696  ·  view source on GitHub ↗

Check if key is a valid boolean indexer for an object with such index and perform reindexing or conversion if needed. This function assumes that is_bool_indexer(key) == True. Parameters ---------- index : Index Index of the object on which the indexing is done.

(index: Index, key)

Source from the content-addressed store, hash-verified

2645
2646
2647def check_bool_indexer(index: Index, key) -> np.ndarray:
2648 """
2649 Check if key is a valid boolean indexer for an object with such index and
2650 perform reindexing or conversion if needed.
2651
2652 This function assumes that is_bool_indexer(key) == True.
2653
2654 Parameters
2655 ----------
2656 index : Index
2657 Index of the object on which the indexing is done.
2658 key : list-like
2659 Boolean indexer to check.
2660
2661 Returns
2662 -------
2663 np.array
2664 Resulting key.
2665
2666 Raises
2667 ------
2668 IndexError
2669 If the key does not have the same length as index.
2670 IndexingError
2671 If the index of the key is unalignable to index.
2672 """
2673 result = key
2674 if isinstance(key, ABCSeries) and not key.index.equals(index):
2675 indexer = result.index.get_indexer_for(index)
2676 if -1 in indexer:
2677 raise IndexingError(
2678 "Unalignable boolean Series provided as "
2679 "indexer (index of the boolean Series and of "
2680 "the indexed object do not match)."
2681 )
2682
2683 result = result.take(indexer)
2684
2685 # fall through for boolean
2686 if not isinstance(result.dtype, ExtensionDtype):
2687 return result.astype(bool)._values
2688
2689 if is_object_dtype(key):
2690 # key might be object-dtype bool, check_array_indexer needs bool array
2691 result = np.asarray(result, dtype=bool)
2692 elif not is_array_like(result):
2693 # GH 33924
2694 # key may contain nan elements, check_array_indexer needs bool array
2695 result = pd_array(result, dtype=bool)
2696 return check_array_indexer(index, result)
2697
2698
2699def convert_missing_indexer(indexer):

Callers 6

__getitem__Method · 0.90
__setitem__Method · 0.90
_getitem_bool_arrayMethod · 0.90
_setitem_arrayMethod · 0.90
_getbool_axisMethod · 0.85
_convert_to_indexerMethod · 0.85

Calls 8

IndexingErrorClass · 0.90
is_object_dtypeFunction · 0.90
check_array_indexerFunction · 0.90
is_array_likeFunction · 0.85
get_indexer_forMethod · 0.80
equalsMethod · 0.45
takeMethod · 0.45
astypeMethod · 0.45

Tested by

no test coverage detected