{"record":{"id":"533c8de2dd77acf1","repo":"pandas-dev/pandas","slug":"out-of-bounds-value-in-indices","errorCode":null,"errorMessage":"out of bounds value in 'indices'.","messagePattern":"out of bounds value in 'indices'\\.","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/arrow/array.py","lineNumber":2093,"sourceCode":"\n        See Also\n        --------\n        numpy.take\n        api.extensions.take\n\n        Notes\n        -----\n        ExtensionArray.take is called by ``Series.__getitem__``, ``.loc``,\n        ``iloc``, when `indices` is a sequence of values. Additionally,\n        it's called by :meth:`Series.reindex`, or any other method\n        that causes realignment, with a `fill_value`.\n        \"\"\"\n        indices_array = np.asanyarray(indices)\n\n        if len(self._pa_array) == 0 and (indices_array >= 0).any():\n            raise IndexError(\"cannot do a non-empty take\")\n        if indices_array.size > 0 and indices_array.max() >= len(self._pa_array):\n            raise IndexError(\"out of bounds value in 'indices'.\")\n\n        if allow_fill:\n            fill_mask = indices_array < 0\n            if fill_mask.any():\n                validate_indices(indices_array, len(self._pa_array))\n                # TODO(ARROW-9433): Treat negative indices as NULL\n                indices_array = pa.array(indices_array, mask=fill_mask)\n                result = self._pa_array.take(indices_array)\n                if isna(fill_value):\n                    return self._from_pyarrow_array(result)\n                # TODO: ArrowNotImplementedError: Function fill_null has no\n                # kernel matching input types (array[string], scalar[string])\n                result = self._from_pyarrow_array(result)\n                result[fill_mask] = fill_value\n                return result\n                # return type(self)(pc.fill_null(result, pa.scalar(fill_value)))\n            else:\n                # Nothing to fill","sourceCodeStart":2075,"sourceCodeEnd":2111,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/arrow/array.py#L2075-L2111","documentation":"The second guard in take(): after the empty-array check, if the largest requested index exceeds (or equals) the length of self._pa_array, the position is out of range. validate_indices is called only inside the allow_fill branch, so this explicit check protects the non-fill path.","triggerScenarios":"Calling .take([n]) or fancy indexing where an index >= len(arr) on a pyarrow-backed ExtensionArray, e.g. pd.array([1,2], dtype='int64[pyarrow]').take([5]).","commonSituations":"Off-by-one bugs in computed indices; reusing indices from a larger frame on a filtered subset; iloc with stale positions after row removal.","solutions":["Clip or recompute indices against the current array length: indices = indices[indices < len(arr)].","Switch to allow_fill=True with negative sentinel indices and a fill_value for out-of-range positions.","Re-derive indices after any filter/sort that changes the array length."],"exampleFix":"// before\narr = pd.array([1, 2, 3], dtype=\"int64[pyarrow]\")\narr.take([5])\n// after\narr = pd.array([1, 2, 3], dtype=\"int64[pyarrow]\")\narr.take([5], allow_fill=True, fill_value=-1)","handlingStrategy":"validation","validationCode":"import numpy as np\n\ndef safe_take(arr, indices):\n    idx = np.asanyarray(indices)\n    if idx.size and idx.max() >= len(arr):\n        raise IndexError(\"requested index exceeds array length\")\n    return arr.take(indices)","typeGuard":"import numpy as np\n\ndef indices_in_bounds(arr, indices) -> bool:\n    idx = np.asanyarray(indices)\n    return idx.size == 0 or idx.max() < len(arr)","tryCatchPattern":"try:\n    arr.take(indices)\nexcept IndexError as e:\n    if \"out of bounds\" in str(e):\n        arr.take(indices, allow_fill=True, fill_value=-1)\n    else:\n        raise","preventionTips":["Recompute indices after filtering or sorting the source.","Use allow_fill=True with negative sentinels for optional positions."],"tags":["pyarrow","take","out-of-bounds","indexerror"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}