pola-rs/polars · error

index positions should be greater than or equal to -2^32

Error message

index positions should be greater than or equal to -2^32

What it means

Symmetric to the upper-bound check: with UInt32 indices, an Int64 index Series whose minimum is below -2^32 cannot be safely converted to the unsigned absolute-position representation, so `_convert_series_to_indices` raises ValueError.

Solutions

  1. Clamp or rebase positions so they stay >= -2^32 (and ideally >= -len(s)).
  2. Compute absolute positions yourself and clip: `idx = (idx % s.len())`.
  3. Use `.gather()` after sanitizing the index Series.

Example fix

// before
idx = positions - 2**33
sub = s[idx]  # ValueError
// after
idx = (positions - 2**33) % s.len()
sub = s[idx]
Defensive patterns

Strategy: validation

Validate before calling

if idx.dtype == pl.Int64 and idx.len() and idx.min() < -(2**32 - 1):
    idx = idx % s.len()

Type guard

def negatives_in_range(idx: pl.Series, length: int) -> bool:
    return idx.min() >= -length if idx.len() else True

Try / catch

try:
    sub = s[idx]
except ValueError as e:
    if "greater than or equal to -2^32" in str(e):
        sub = s[idx % s.len()]
    else:
        raise

Prevention

When it happens

Trigger: Indexing with a pl.Series of Int64 containing very negative values (< -4294967295), e.g. `s[pl.Series([-2**32])]`. Small negatives (-1, -2) are fine because they are treated as from-the-end positions.

Common situations: Computing positions with signed arithmetic that can underflow, or offsetting valid positions by large negative deltas before indexing.

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/dc5ab597bf25fba8. Report an issue: GitHub.

Appendix: source

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

        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))
                    .cast(idx_type)
                )
                .to_series(0)

View on GitHub (pinned to fe841f959e)