pola-rs/polars · error

index positions should be smaller than 2^32

Error message

index positions should be smaller than 2^32

What it means

When the default index type is UInt32 (polars' default), `_convert_series_to_indices` checks the max of an Int64/UInt64 index Series against 2^32-1 and raises ValueError if any position exceeds it, because positions cannot be represented in UInt32.

Solutions

  1. Ensure index positions are < 2^32; select a subrange of the frame first, then index.
  2. Run polars with `POLARS_MAX_THREADS`/index type as u64 if available (`polars.Config` / `pl.enable_string_cache`-style env `POLARS_USING_U64_INDEX` is not public — prefer keeping positions in range).
  3. Convert the selection to a boolean mask or `.take()` after shrinking data instead of passing huge positions.

Example fix

// before
big = pl.int_range(0, n).filter(ids > 2**32)
sub = s[big]  # ValueError: index positions should be smaller than 2^32
// after
sub_small = s.filter(pl.int_range(0, s.len()) < 2**32)
sub = sub_small.take(big.filter(big < 2**32))
Defensive patterns

Strategy: validation

Validate before calling

if idx.dtype in (pl.Int64, pl.UInt64) and idx.len() and idx.max() >= 2**32 - 1:
    raise ValueError("index positions must be < 2^32")

Type guard

def fits_u32(idx: pl.Series) -> bool:
    return idx.dtype not in (pl.Int64, pl.UInt64) or idx.max() < 2**32 - 1

Try / catch

try:
    sub = s[idx]
except ValueError as e:
    if "smaller than 2^32" in str(e):
        sub = s[idx.filter(idx < 2**32 - 1)]
    else:
        raise

Prevention

When it happens

Trigger: Indexing with a pl.Series of dtype Int64 or UInt64 whose max value is >= 4294967295, e.g. `s[pl.Series([2**32, 5])]`.

Common situations: Building index Series from large computed values (counters, hashes, offsets) on a DataFrame with billions of rows' worth of address space, or reusing ID columns (like user IDs) as positional indices.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/ce6101e1c0cbf38b. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/_utils/getitem.py:371

    idx_type = get_index_type()

    if s.dtype == idx_type:
        return s

    if not s.dtype.is_integer():
        if s.dtype == Boolean:
            _raise_on_boolean_mask()
        else:
            msg = f"cannot treat Series of type {s.dtype} as indices"
            raise TypeError(msg)

    if s.len() == 0:
        return pl.Series(s.name, [], dtype=idx_type)

    if idx_type == UInt32:
        if s.dtype in {Int64, UInt64} and s.max() >= U32_MAX:  # type: ignore[operator]
            msg = "index positions should be smaller than 2^32"
            raise ValueError(msg)
        if s.dtype == Int64 and s.min() < -U32_MAX:  # type: ignore[operator]
            msg = "index positions should be greater than or equal to -2^32"
            raise ValueError(msg)

    if s.dtype.is_signed_integer():
        if s.min() < 0:  # type: ignore[operator]
            if idx_type == UInt32:
                idxs = s.cast(Int32) if s.dtype in {Int8, Int16} else s
            else:
                idxs = s.cast(Int64) if s.dtype in {Int8, Int16, Int32} else s

            # Update negative indexes to absolute indexes.
            return (
                idxs.to_frame()
                .select(
                    F.when(F.col(idxs.name) < 0)
                    .then(size + F.col(idxs.name))
                    .otherwise(F.col(idxs.name))

View on GitHub (pinned to fe841f959e)