{"record":{"id":"5b4a9cfd006f6ae8","repo":"pandas-dev/pandas","slug":"index-key-is-out-of-bounds-for-axis-0-with-size","errorCode":null,"errorMessage":"index {key} is out of bounds for axis 0 with size {n}","messagePattern":"index (.+?) is out of bounds for axis 0 with size (.+?)","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/arrow/array.py","lineNumber":2892,"sourceCode":"        if com.is_null_slice(key):\n            # fast path (GH50248)\n            if (\n                isinstance(value, (pa.Array, pa.ChunkedArray))\n                and value.type == self._pa_array.type\n                and len(value) == len(self)\n            ):\n                data = value\n            else:\n                data = self._if_else(True, value, self._pa_array)\n\n        elif is_integer(key):\n            # fast path\n            key = cast(\"int\", key)\n            n = len(self)\n            if key < 0:\n                key += n\n            if not 0 <= key < n:\n                raise IndexError(\n                    f\"index {key} is out of bounds for axis 0 with size {n}\"\n                )\n            if isinstance(value, pa.Scalar):\n                value = value.as_py()\n            elif is_list_like(value):\n                raise ValueError(\"Length of indexer and values mismatch\")\n            chunks = [\n                *self._pa_array[:key].chunks,\n                pa.array([value], type=self._pa_array.type, from_pandas=is_nan_na()),\n                *self._pa_array[key + 1 :].chunks,\n            ]\n            data = pa.chunked_array(chunks).combine_chunks()\n\n        elif is_bool_dtype(key):\n            key = np.asarray(key, dtype=np.bool_)\n            data = self._replace_with_mask(self._pa_array, key, value)\n\n        elif is_scalar(value) or isinstance(value, pa.Scalar):","sourceCodeStart":2874,"sourceCodeEnd":2910,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/arrow/array.py#L2874-L2910","documentation":"Raised in __setitem__ on the integer-key path when the resolved index is outside [0, n). Negative indices are normalized by adding n, so both overly-negative and too-large indices land here.","triggerScenarios":"Doing `arr[k] = v` where `k` is an int with `abs(k) >= len(arr)` (or `k < -len(arr)`).","commonSituations":"Indexing errors after filtering changes length, using DataFrame row numbers on a Series subset, or stale index variables.","solutions":["Validate `0 <= k < len(arr)` (or `-len(arr) <= k < 0`) before assignment.","Recompute the index from the current array.","Use `.iloc` on the wrapping Series with bounds-checked positions."],"exampleFix":"// before\narr = pd.array([1, 2, 3], dtype=\"int64[pyarrow]\")\narr[5] = 99\n\n// after\narr = pd.array([1, 2, 3, 0, 0, 0], dtype=\"int64[pyarrow]\")\narr[5] = 99","handlingStrategy":"validation","validationCode":"def check_index(arr, k):\n    n = len(arr)\n    kk = k + n if k < 0 else k\n    if not 0 <= kk < n:\n        raise IndexError(f\"index {k} out of bounds for size {n}\")\n    return kk","typeGuard":"def index_in_bounds(arr, k) -> bool:\n    n = len(arr)\n    kk = k + n if k < 0 else k\n    return 0 <= kk < n","tryCatchPattern":"try:\n    arr[k] = v\nexcept IndexError as e:\n    if \"out of bounds for axis 0\" in str(e):\n        # extend array or skip\n        pass\n    else:\n        raise","preventionTips":["Validate integer keys against current array length before assignment.","Recompute indices after filtering/concat.","Prefer .iloc with bounds-checked positions over raw integer assignment."],"tags":["arrow","setitem","out-of-bounds","indexing"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}