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)
| 2645 | |
| 2646 | |
| 2647 | def 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 | |
| 2699 | def convert_missing_indexer(indexer): |
no test coverage detected