votes up 7

arrays used as indices must be of integer or boolean type

github stars 30911
Exception Class:

Raise code

            indexer = np.asarray(indexer, dtype=np.intp)
        except ValueError as err:
            raise ValueError(
                "Cannot index with an integer indexer containing NA values"
            ) from err
        raise IndexError("arrays used as indices must be of integer or boolean type")

    return indexer

Ways to fix

votes up 1 votes down

Error code:

import pandas as pd 

indexer = pd.array(['one','two'])
arr = pd.array([123])
pd.api.indexers.check_array_indexer(arr, indexer)

check_array_indexer checks if indexer is a valid array indexer for array. Take 2 arguments: an array that is being indexed and an indexer that is used to index. The error comes when the indexer has a different data type than integer and boolean.

Fix code:

import pandas as pd 

indexer = pd.array([0,2])
arr = pd.array([123])
pd.api.indexers.check_array_indexer(arr, indexer)
Jul 20, 2021 nurlan.taghi answer
nurlan.taghi 10.8k

Add a possible fix

Please authorize to post fix