pandas-dev/pandas · error · IndexError

out of bounds value in 'indices'.

Error message

out of bounds value in 'indices'.

What it means

Raised by ArrowExtensionArray.take when any index is greater than or equal to the array length. This is the standard out-of-bounds guard for positional indexing and is reached through Series.iloc, .loc, reindex, and explicit .take() calls.

Source

Thrown at pandas/core/arrays/arrow/array.py:2068

        See Also
        --------
        numpy.take
        api.extensions.take

        Notes
        -----
        ExtensionArray.take is called by ``Series.__getitem__``, ``.loc``,
        ``iloc``, when `indices` is a sequence of values. Additionally,
        it's called by :meth:`Series.reindex`, or any other method
        that causes realignment, with a `fill_value`.
        """
        indices_array = np.asanyarray(indices)

        if len(self._pa_array) == 0 and (indices_array >= 0).any():
            raise IndexError("cannot do a non-empty take")
        if indices_array.size > 0 and indices_array.max() >= len(self._pa_array):
            raise IndexError("out of bounds value in 'indices'.")

        if allow_fill:
            fill_mask = indices_array < 0
            if fill_mask.any():
                validate_indices(indices_array, len(self._pa_array))
                # TODO(ARROW-9433): Treat negative indices as NULL
                indices_array = pa.array(indices_array, mask=fill_mask)
                result = self._pa_array.take(indices_array)
                if isna(fill_value):
                    return self._from_pyarrow_array(result)
                # TODO: ArrowNotImplementedError: Function fill_null has no
                # kernel matching input types (array[string], scalar[string])
                result = self._from_pyarrow_array(result)
                result[fill_mask] = fill_value
                return result
                # return type(self)(pc.fill_null(result, pa.scalar(fill_value)))
            else:
                # Nothing to fill

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Clip or filter indices to valid range: `indices = indices[indices < len(arr)]`.
  2. Use `allow_fill=True` with a fill_value to permit missing positions.
  3. Recompute indices from the current array's positions, not from a cached/filtered copy.

Example fix

// before
arr = pd.array([10, 20], dtype="int64[pyarrow]")
arr.take([0, 5])

// after
arr.take([0, 1])  # or arr.take([0, 5], allow_fill=True, fill_value=0)
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np

def bounded_take(arr, indices, fill_value=None):
    idx = np.asanyarray(indices)
    if idx.size and idx.max() >= len(arr):
        if fill_value is not None:
            return arr.take(idx, allow_fill=True, fill_value=fill_value)
        idx = idx[idx < len(arr)]
    return arr.take(idx)

Type guard

def indices_in_bounds(arr, indices) -> bool:
    import numpy as np
    idx = np.asanyarray(indices)
    return idx.size == 0 or int(idx.max()) < len(arr)

Try / catch

try:
    arr.take(indices)
except IndexError as e:
    if "out of bounds value in 'indices'" in str(e):
        arr.take(indices, allow_fill=True, fill_value=0)
    else:
        raise

Prevention

When it happens

Trigger: Calling `.take(indices)`, `.iloc[indices]`, or `.reindex` (with allow_fill=False) where `max(indices) >= len(arr)`.

Common situations: Stale index lists computed against an older/droppedna version of the data, off-by-one loop errors, or passing DataFrame row positions to a Series subset.

Related errors


AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07). Data as JSON: /api/errors/533c8de2dd77acf1. Report an issue: GitHub.