pola-rs/polars · error · ValueError

the given slice {s!r} is not supported by lazy computation\n

Error message

the given slice {s!r} is not supported by lazy computation\n\nConsider a more efficient approach, or construct explicitly with other methods.

What it means

Catch-all ValueError from LazyPolarsSlice.apply (py-polars/src/polars/_utils/slice.py:213-217): the given Python slice matches none of the patterns LazyFrame can compute efficiently (clone/gather_every/reverse/head/tail/slice mappings listed in the source). Typical unreachable patterns are a negative start combined with an explicit stop, e.g. lf[-3:10], because a lazy frame cannot resolve negative offsets without knowing its length.

Source

Thrown at py-polars/src/polars/_utils/slice.py:217

            obj = self.obj.tail(abs(start))
            return obj if (step == 1) else obj.gather_every(step)

        # ---------------------------------------
        # straight-through mappings for "slice"
        # ---------------------------------------
        # [i:]     => slice(i)
        # [i:j]    => slice(i,j-i)
        # [i:j:k]  => slice(i,j-i).gather_every(k)
        elif start > 0 and (s.stop is None or s.stop >= 0):
            slice_length = None if (s.stop is None) else (s.stop - start)
            obj = self.obj.slice(start, slice_length)
            return obj if (step == 1) else obj.gather_every(step)

        msg = (
            f"the given slice {s!r} is not supported by lazy computation"
            "\n\nConsider a more efficient approach, or construct explicitly with other methods."
        )
        raise ValueError(msg)

View on GitHub (pinned to df599052da)

Solutions

  1. Collect first and slice eagerly: lf.collect()[-3:10]
  2. Replace negative start with tail(): lf.tail(3) reproduces lf[-3:]
  3. Recompose the window from supported ops, e.g. lf.head(10).tail(3) for lf[-3:10] semantics
  4. Restrict generic helpers to supported patterns ([i:], [:j], [::k], [i:j], [::-1], [-i:])

Example fix

# before
lf = pl.scan_parquet('f.parquet')
window = lf[-3:10]  # ValueError

# after
window = lf.head(10).tail(3)
Defensive patterns

Strategy: validation

Validate before calling

def lazy_getitem_supported(s: slice) -> bool:
    start = s.start or 0
    step = s.step or 1
    if s.stop is not None and s.stop < 0:
        return False
    if step < 0:
        return (s.start is None and s.stop is None) or (start >= 0 > step and s.stop is None)
    if start < 0:
        return s.stop is None
    return True

Try / catch

try:
    out = lf[s]
except ValueError as e:
    if 'not supported by lazy computation' in str(e):
        out = lf.collect()[s]
    else:
        raise

Prevention

When it happens

Trigger: lf[-3:10], lf[-5:2], lf[-2:10:2], or any slice with start < 0 and an explicit stop; also any residual pattern not covered by the elif chain in LazyPolarsSlice.apply.

Common situations: Reusable slicing utilities that receive arbitrary slice objects; code translated from pandas/numpy conventions; trimming a known number of tail rows while keeping a head window in a lazy pipeline.

Related errors


AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16). Data as JSON: /api/errors/5adfa648ab115f23. Report an issue: GitHub.