{"record":{"id":"4c59940c4ed35bb2","repo":"pandas-dev/pandas","slug":"cannot-do-a-non-empty-take","errorCode":null,"errorMessage":"cannot do a non-empty take","messagePattern":"cannot do a non-empty take","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/arrow/array.py","lineNumber":2066,"sourceCode":"            When `indices` contains negative values other than ``-1``\n            and `allow_fill` is True.\n\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)))","sourceCodeStart":2048,"sourceCodeEnd":2084,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/arrow/array.py#L2048-L2084","documentation":"Raised by ExtensionArray.take when the source array is empty (length 0) but the requested indices contain at least one non-negative value. Taking elements from an empty array is logically impossible, so pandas rejects it as an IndexError.","triggerScenarios":"Calling `take(indices, allow_fill=...)` on an empty ArrowExtensionArray where `indices` contains any index >= 0; commonly reached via `Series.reindex`, `.iloc`, or `.take` on an empty Series.","commonSituations":"Operating on a filtered DataFrame that became empty, reindexing against a target index that no rows match, or generic code that doesn't short-circuit on empty input.","solutions":["Guard for empty input before calling take: `if len(arr) == 0: return arr`.","Check that indices are all negative/sentinel-only when the array is empty (use allow_fill=True with -1 sentinels).","Filter or skip the operation when the source frame has zero rows."],"exampleFix":"// before\narr = pd.array([], dtype=\"int64[pyarrow]\")\narr.take([0])\n\n// after\nif len(arr):\n    arr.take([0])\nelse:\n    arr  # nothing to take","handlingStrategy":"validation","validationCode":"def safe_take(arr, indices, allow_fill=False, fill_value=None):\n    if len(arr) == 0:\n        return arr\n    return arr.take(indices, allow_fill=allow_fill, fill_value=fill_value)","typeGuard":"def take_is_safe(arr, indices) -> bool:\n    import numpy as np\n    idx = np.asanyarray(indices)\n    return len(arr) > 0 or not bool((idx >= 0).any())","tryCatchPattern":"try:\n    arr.take(indices)\nexcept IndexError as e:\n    if \"cannot do a non-empty take\" in str(e):\n        result = arr  # empty source -> empty result\n    else:\n        raise","preventionTips":["Short-circuit take/reindex on empty inputs.","In ETL pipelines, guard `if len(df) == 0: return df` before positional slicing.","Use allow_fill=True when missing positions are expected."],"tags":["arrow","take","empty-array","indexing"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}