{"record":{"id":"f6f49f56858ebec4","repo":"pandas-dev/pandas","slug":"out-of-bounds-value-in-indices-f6f49f","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/sparse/array.py","lineNumber":1181,"sourceCode":"        else:\n            return self._take_without_fill(indices)\n\n        return type(self)(\n            result, fill_value=self.fill_value, kind=self.kind, dtype=dtype\n        )\n\n    def _take_with_fill(self, indices, fill_value=None) -> np.ndarray:\n        if fill_value is None:\n            fill_value = self.dtype.na_value\n\n        if indices.min() < -1:\n            raise ValueError(\n                \"Invalid value in 'indices'. Must be between -1 \"\n                \"and the length of the array.\"\n            )\n\n        if indices.max() >= len(self):\n            raise IndexError(\"out of bounds value in 'indices'.\")\n\n        if len(self) == 0:\n            # Empty... Allow taking only if all empty\n            if (indices == -1).all():\n                dtype = np.result_type(self.sp_values, type(fill_value))\n                taken = np.empty_like(indices, dtype=dtype)\n                taken.fill(fill_value)\n                return taken\n            else:\n                raise IndexError(\"cannot do a non-empty take from an empty axes.\")\n\n        # sp_indexer may be -1 for two reasons\n        # 1.) we took for an index of -1 (new)\n        # 2.) we took a value that was self.fill_value (old)\n        sp_indexer = self.sp_index.lookup_array(indices)\n        new_fill_indices = indices == -1\n        old_fill_indices = (sp_indexer == -1) & ~new_fill_indices\n","sourceCodeStart":1163,"sourceCodeEnd":1199,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/sparse/array.py#L1163-L1199","documentation":"IndexError from SparseArray._take_with_fill when allow_fill=True and the maximum index is >= len(self). The upper bound for fill-mode take is len(self)-1; -1 is reserved as the fill sentinel.","triggerScenarios":"arr.take([0, len(arr)], allow_fill=True); forwarding computed indices without bounding to the array length.","commonSituations":"Reindexing logic that produces positions equal to the array length; using a positional array from a longer array on a shorter one.","solutions":["Bound-check: indices = indices[(indices >= -1) & (indices < len(arr))].","Use pd.Series(arr).reindex(labels) for label-based reindex instead of manual take.","Drop allow_fill and let negative/positive positions wrap, after confirming they are in range."],"exampleFix":"// before\narr.take([0, len(arr)], allow_fill=True)  # raises\n// after\nimport numpy as np\nidx = np.array([0, len(arr)-1])\narr.take(idx, allow_fill=True)","handlingStrategy":"validation","validationCode":"import numpy as np\ndef bounded_indices(arr, indices):\n    idx = np.asarray(indices)\n    return idx[(idx >= -1) & (idx < len(arr))]","typeGuard":"def indices_in_range(arr, indices) -> bool:\n    import numpy as np\n    idx = np.asarray(indices)\n    return bool(idx.max() < len(arr) and idx.min() >= -1)","tryCatchPattern":"try:\n    arr.take(indices, allow_fill=True)\nexcept IndexError as e:\n    if 'out of bounds' in str(e):\n        out = arr.take(bounded_indices(arr, indices), allow_fill=True)\n    else:\n        raise","preventionTips":["Bound indices to [0, len-1] (or -1 sentinel) before fill-mode take.","Prefer Series.reindex for label-based selection."],"tags":["sparse","take","out-of-bounds","allow-fill"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}