{"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":2068,"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":2050,"sourceCodeEnd":2086,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/arrow/array.py#L2050-L2086","documentation":"Raised by ArrowExtensionArray.take when any index is greater than or equal to the array length. This is the standard out-of-bounds guard for positional indexing and is reached through Series.iloc, .loc, reindex, and explicit .take() calls.","triggerScenarios":"Calling `.take(indices)`, `.iloc[indices]`, or `.reindex` (with allow_fill=False) where `max(indices) >= len(arr)`.","commonSituations":"Stale index lists computed against an older/droppedna version of the data, off-by-one loop errors, or passing DataFrame row positions to a Series subset.","solutions":["Clip or filter indices to valid range: `indices = indices[indices < len(arr)]`.","Use `allow_fill=True` with a fill_value to permit missing positions.","Recompute indices from the current array's positions, not from a cached/filtered copy."],"exampleFix":"// before\narr = pd.array([10, 20], dtype=\"int64[pyarrow]\")\narr.take([0, 5])\n\n// after\narr.take([0, 1])  # or arr.take([0, 5], allow_fill=True, fill_value=0)","handlingStrategy":"validation","validationCode":"import numpy as np\n\ndef bounded_take(arr, indices, fill_value=None):\n    idx = np.asanyarray(indices)\n    if idx.size and idx.max() >= len(arr):\n        if fill_value is not None:\n            return arr.take(idx, allow_fill=True, fill_value=fill_value)\n        idx = idx[idx < len(arr)]\n    return arr.take(idx)","typeGuard":"def indices_in_bounds(arr, indices) -> bool:\n    import numpy as np\n    idx = np.asanyarray(indices)\n    return idx.size == 0 or int(idx.max()) < len(arr)","tryCatchPattern":"try:\n    arr.take(indices)\nexcept IndexError as e:\n    if \"out of bounds value in 'indices'\" in str(e):\n        arr.take(indices, allow_fill=True, fill_value=0)\n    else:\n        raise","preventionTips":["Recompute index lists from the current array length, not a cached copy.","Use allow_fill=True with a sentinel when indices may exceed bounds.","Clip or filter indices before passing to take/iloc."],"tags":["arrow","take","out-of-bounds","indexing"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}