{"record":{"id":"b12498609cecc8da","repo":"pandas-dev/pandas","slug":"indices-must-be-an-array-not-a-scalar-indices","errorCode":null,"errorMessage":"'indices' must be an array, not a scalar '{indices}'.","messagePattern":"'indices' must be an array, not a scalar '(.+?)'\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/sparse/array.py","lineNumber":1154,"sourceCode":"        if loc < 0:\n            loc += n\n\n        if loc >= n or loc < 0:\n            raise IndexError(\n                f\"index is out of bounds: must be an integer between -{n} and {n - 1}\"\n            )\n\n        sp_loc = self.sp_index.lookup(loc)\n        if sp_loc == -1:\n            return self.fill_value\n        else:\n            val = self.sp_values[sp_loc]\n            val = maybe_box_datetimelike(val, self.sp_values.dtype)\n            return val\n\n    def take(self, indices, *, allow_fill: bool = False, fill_value=None) -> Self:\n        if is_scalar(indices):\n            raise ValueError(f\"'indices' must be an array, not a scalar '{indices}'.\")\n        indices = np.asarray(indices, dtype=np.int32)\n\n        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","sourceCodeStart":1136,"sourceCodeEnd":1172,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/sparse/array.py#L1136-L1172","documentation":"ValueError from SparseArray.take when the indices argument is a scalar rather than array-like. take requires an iterable of integer positions because it builds a new SparseArray; scalar lookup should use __getitem__.","triggerScenarios":"arr.take(3) (scalar) instead of arr.take([3]); passing a python int where an array/list is expected.","commonSituations":"Generic code calling take with whatever value a caller supplied without wrapping; refactoring from arr[i] to .take.","solutions":["Wrap scalar indices in a list: arr.take([3]).","Use arr[3] directly for single-element access.","Validate: indices = np.atleast_1d(indices) before calling take."],"exampleFix":"// before\narr.take(3)  # raises\n// after\narr.take([3])","handlingStrategy":"validation","validationCode":"import numpy as np\ndef take_safe(arr, indices):\n    return arr.take(np.atleast_1d(indices))","typeGuard":"def is_scalar_index(indices) -> bool:\n    import numbers\n    return isinstance(indices, numbers.Integral)","tryCatchPattern":"try:\n    arr.take(indices)\nexcept ValueError as e:\n    if 'must be an array' in str(e):\n        out = arr.take([indices])\n    else:\n        raise","preventionTips":["Always pass a list/array of positions to take.","Use __getitem__ for single-element access.","Wrap with np.atleast_1d in generic helpers."],"tags":["sparse","take","scalar","validation"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}