{"record":{"id":"17ae9763550654af","repo":"pandas-dev/pandas","slug":"only-integers-slices-and-integer-or-boolean-array","errorCode":null,"errorMessage":"Only integers, slices and integer or boolean arrays are valid indices.","messagePattern":"Only integers, slices and integer or boolean arrays are valid indices\\.","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/arrow/array.py","lineNumber":919,"sourceCode":"            if not len(item):\n                # Removable once we migrate StringDtype[pyarrow] to ArrowDtype[string]\n                if (\n                    isinstance(self._dtype, StringDtype)\n                    and self._dtype.storage == \"pyarrow\"\n                ):\n                    # TODO(infer_string) should this be large_string?\n                    pa_dtype = pa.string()\n                else:\n                    pa_dtype = self._dtype.pyarrow_dtype\n                result = pa.chunked_array([], type=pa_dtype)\n                return self._from_pyarrow_array(result)\n\n            elif item.dtype.kind in \"iu\":\n                return self.take(item)\n            elif item.dtype.kind == \"b\":\n                return self._from_pyarrow_array(self._pa_array.filter(item))\n            else:\n                raise IndexError(\n                    \"Only integers, slices and integer or \"\n                    \"boolean arrays are valid indices.\"\n                )\n        elif isinstance(item, tuple):\n            item = unpack_tuple_and_ellipses(item)\n\n        if item is Ellipsis:\n            # TODO: should be handled by pyarrow?\n            item = slice(None)\n\n        if is_scalar(item) and not is_integer(item):\n            # e.g. \"foo\" or 2.5\n            # exception message copied from numpy\n            raise IndexError(\n                r\"only integers, slices (`:`), ellipsis (`...`), numpy.newaxis \"\n                r\"(`None`) and integer or boolean arrays are valid indices\"\n            )\n        # We are not an array indexer, so maybe e.g. a slice or integer","sourceCodeStart":901,"sourceCodeEnd":937,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/arrow/array.py#L901-L937","documentation":"Raised by ArrowExtensionArray.__getitem__ when the indexer is a numpy array whose dtype kind is neither integer ('i'/'u') nor boolean ('b'). The __getitem__ override dispatches integer arrays to self.take and boolean arrays to pa.ChunkedArray.filter; any other ndarray kind (float, object, string, datetime) is invalid as a positional indexer.","triggerScenarios":"Indexing arr[np_float_array] where the indexer is a float ndarray (e.g. np.array([0.0, 1.0])); indexing with an ndarray of Python objects or strings; using the result of a computation that returned float indices without casting to intp.","commonSituations":"Computed indexers from division or math that yield float arrays; numpy boolean masks accidentally upcast to int8/int16 then to non-integer; passing a pandas Series of floats as a positional indexer; mixing positional and label-based indexing.","solutions":["Cast the indexer to intp before indexing: arr[idx.astype(np.intp)].","If the floats are coordinates, round and cast explicitly: arr[np.round(idx).astype(np.intp)].","Use a boolean mask instead of integer positions when possible.","If you intended label-based indexing, use a Series/Index accessor rather than the raw extension array __getitem__."],"exampleFix":"# before\nimport numpy as np\narr = pd.array([10, 20, 30], dtype=\"int64[pyarrow]\")\nidx = np.array([0.0, 2.0])\narr[idx]  # raises IndexError\n\n# after\narr[idx.astype(np.intp)]","handlingStrategy":"validation","validationCode":"import numpy as np\n\ndef coerce_indexer(idx):\n    arr = np.asarray(idx)\n    if arr.dtype.kind == 'f':\n        if not np.all(arr == arr.astype(np.int64)):\n            raise ValueError('non-integer float indexer')\n        return arr.astype(np.intp)\n    if arr.dtype.kind == 'b':\n        return arr\n    if arr.dtype.kind in 'iu':\n        return arr.astype(np.intp)\n    raise TypeError(f'unsupported indexer dtype {arr.dtype}')","typeGuard":"import numpy as np\n\ndef is_valid_positional_indexer(idx: np.ndarray) -> bool:\n    return idx.ndim == 1 and idx.dtype.kind in 'iub'","tryCatchPattern":null,"preventionTips":["Always cast computed float indexers to np.intp before indexing.","Prefer boolean masks over integer indexers when feasible.","Validate indexer dtype kind before __getitem__."],"tags":["indexing","numpy","type-validation","getitem"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}