{"record":{"id":"6ba4541ec50e7968","repo":"pandas-dev/pandas","slug":"cannot-do-a-non-empty-take-from-an-empty-axes","errorCode":null,"errorMessage":"cannot do a non-empty take from an empty axes.","messagePattern":"cannot do a non-empty take from an empty axes\\.","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/sparse/array.py","lineNumber":1191,"sourceCode":"\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\n        if self.sp_index.npoints == 0 and old_fill_indices.all():\n            # We've looked up all valid points on an all-sparse array.\n            taken = np.full(\n                sp_indexer.shape, fill_value=self.fill_value, dtype=self.dtype.subtype\n            )\n\n        elif self.sp_index.npoints == 0:\n            # Use the old fill_value unless we took for an index of -1\n            _dtype = np.result_type(self.dtype.subtype, type(fill_value))\n            if self.dtype.subtype.kind == \"b\" and _dtype.kind != \"b\":","sourceCodeStart":1173,"sourceCodeEnd":1209,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/sparse/array.py#L1173-L1209","documentation":"IndexError from SparseArray._take_with_fill when the array is empty (len==0) but the caller requested a non-empty take that is not entirely the -1 fill sentinel. Empty source arrays can only honor an all-fill take.","triggerScenarios":"pd.arrays.SparseArray([]).take([0], allow_fill=True); any take on an empty sparse Series where indices contains a real position.","commonSituations":"Branches that forgot to short-circuit on empty input; groupby/reindex paths that produce empty groups but still call take.","solutions":["Guard the empty case: if len(arr) == 0: return empty result.","Only take with all -1 sentinels when allow_fill=True on an empty array.","Use pd.Series(arr).reindex(...).fillna(...) which handles empties."],"exampleFix":"// before\npd.arrays.SparseArray([]).take([0], allow_fill=True)  # raises\n// after\nempty = pd.arrays.SparseArray([])\nout = empty.take([-1, -1], allow_fill=True)  # all-fill is allowed","handlingStrategy":"validation","validationCode":"def safe_take_empty(arr, indices, fill_value=None):\n    if len(arr) == 0:\n        import numpy as np\n        if np.all(np.asarray(indices) == -1):\n            return arr.take(indices, allow_fill=True, fill_value=fill_value)\n        return arr  # empty result\n    return arr.take(indices, allow_fill=True, fill_value=fill_value)","typeGuard":"def empty_source(arr) -> bool:\n    return len(arr) == 0","tryCatchPattern":"try:\n    arr.take(indices, allow_fill=True)\nexcept IndexError as e:\n    if 'empty axes' in str(e):\n        out = arr  # nothing to take\n    else:\n        raise","preventionTips":["Short-circuit take calls on empty arrays.","Only request all -1 sentinels for fill takes on empties.","Use Series.reindex for robustness on empty groups."],"tags":["sparse","take","empty","allow-fill"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}