{"record":{"id":"b5a961ba3e3251bf","repo":"pandas-dev/pandas","slug":"invalid-value-in-indices-must-be-between-1-and","errorCode":null,"errorMessage":"Invalid value in 'indices'. Must be between -1 and the length of the array.","messagePattern":"Invalid value in 'indices'\\. Must be between -1 and the length of the array\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/sparse/array.py","lineNumber":1175,"sourceCode":"        dtype = None\n        if indices.size == 0:\n            result = np.array([], dtype=\"object\")\n            dtype = self.dtype\n        elif allow_fill:\n            result = self._take_with_fill(indices, fill_value=fill_value)\n        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","sourceCodeStart":1157,"sourceCodeEnd":1193,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/sparse/array.py#L1157-L1193","documentation":"ValueError from SparseArray._take_with_fill when allow_fill=True and the minimum index is less than -1. With allow_fill, -1 is the only legal sentinel (it means 'use fill_value'); anything smaller is invalid.","triggerScenarios":"arr.take([-2, 0, 3], allow_fill=True); computing indices with an off-by-one negative shift and forwarding them.","commonSituations":"Mixing allow_fill=True semantics (which use -1 as sentinel) with normal negative indexing (which uses -n..-1).","solutions":["Clamp negatives: indices = np.where(indices < -1, -1, indices) if you want fill, or drop allow_fill for true negative indexing.","Use allow_fill=False (default) to use standard negative positions.","Validate indices.min() >= -1 before calling take with allow_fill=True."],"exampleFix":"// before\narr.take([-2, 0], allow_fill=True)  # raises\n// after\narr.take([0, 0], allow_fill=True)  # or use allow_fill=False with [-2, 0]","handlingStrategy":"validation","validationCode":"import numpy as np\ndef clamped_fill_indices(indices):\n    arr_idx = np.asarray(indices)\n    arr_idx[arr_idx < -1] = -1\n    return arr_idx","typeGuard":"def fill_indices_valid(indices) -> bool:\n    import numpy as np\n    return bool(np.asarray(indices).min() >= -1)","tryCatchPattern":"try:\n    arr.take(indices, allow_fill=True)\nexcept ValueError as e:\n    if 'Must be between -1' in str(e):\n        out = arr.take(clamped_fill_indices(indices), allow_fill=True)\n    else:\n        raise","preventionTips":["Remember -1 is the only legal sentinel with allow_fill=True.","Drop allow_fill for standard negative indexing.","Validate min(indices) >= -1 before fill-mode take."],"tags":["sparse","take","allow-fill","validation"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}