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
- Collect first and slice eagerly: lf.collect()[-3:10]
- Replace negative start with tail(): lf.tail(3) reproduces lf[-3:]
- Recompose the window from supported ops, e.g. lf.head(10).tail(3) for lf[-3:10] semantics
- 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
- Never combine a negative start with an explicit stop on a LazyFrame
- Use tail(n) instead of [-n:] and head(j) instead of [:j]
- Collect before arbitrary slicing when the data fits in memory
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
- negative stop is not supported for lazy slices
- negative stride is not supported in conjunction with start+s
- LazyFrame `how` must be one of {{{allowed}}}, got {how!r}
- `format` must be one of {'binary', 'json'}, got {format!r}
- invalid input for `aggregate_function` argument: {aggregate_
AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16).
Data as JSON: /api/errors/5adfa648ab115f23.
Report an issue: GitHub.