{"record":{"id":"5adfa648ab115f23","repo":"pola-rs/polars","slug":"the-given-slice-s-r-is-not-supported-by-lazy-com","errorCode":null,"errorMessage":"the given slice {s!r} is not supported by lazy computation\\n\\nConsider a more efficient approach, or construct explicitly with other methods.","messagePattern":"the given slice (.+?) is not supported by lazy computation\\\\n\\\\nConsider a more efficient approach, or construct explicitly with other methods\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/slice.py","lineNumber":217,"sourceCode":"            obj = self.obj.tail(abs(start))\n            return obj if (step == 1) else obj.gather_every(step)\n\n        # ---------------------------------------\n        # straight-through mappings for \"slice\"\n        # ---------------------------------------\n        # [i:]     => slice(i)\n        # [i:j]    => slice(i,j-i)\n        # [i:j:k]  => slice(i,j-i).gather_every(k)\n        elif start > 0 and (s.stop is None or s.stop >= 0):\n            slice_length = None if (s.stop is None) else (s.stop - start)\n            obj = self.obj.slice(start, slice_length)\n            return obj if (step == 1) else obj.gather_every(step)\n\n        msg = (\n            f\"the given slice {s!r} is not supported by lazy computation\"\n            \"\\n\\nConsider a more efficient approach, or construct explicitly with other methods.\"\n        )\n        raise ValueError(msg)\n","sourceCodeStart":199,"sourceCodeEnd":218,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/_utils/slice.py#L199-L218","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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:])"],"exampleFix":"# before\nlf = pl.scan_parquet('f.parquet')\nwindow = lf[-3:10]  # ValueError\n\n# after\nwindow = lf.head(10).tail(3)","handlingStrategy":"validation","validationCode":"def lazy_getitem_supported(s: slice) -> bool:\n    start = s.start or 0\n    step = s.step or 1\n    if s.stop is not None and s.stop < 0:\n        return False\n    if step < 0:\n        return (s.start is None and s.stop is None) or (start >= 0 > step and s.stop is None)\n    if start < 0:\n        return s.stop is None\n    return True","typeGuard":null,"tryCatchPattern":"try:\n    out = lf[s]\nexcept ValueError as e:\n    if 'not supported by lazy computation' in str(e):\n        out = lf.collect()[s]\n    else:\n        raise","preventionTips":["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"],"tags":["polars","lazyframe","slicing","valueerror"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}