{"record":{"id":"7cee7c179adb4600","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/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_utils/slice.py#L199-L218","documentation":"apply is the final fallback in lazy slice handling: if the slice pattern is not one of the efficiently recognizable forms (empty slices, head, every-Nth, etc.), lazy computation cannot represent it and ValueError is raised telling the user to build the result explicitly.","triggerScenarios":"Any lazy .slice(...) combination not matched by the optimized patterns in apply — e.g. exotic start/stop/step combos like lf.slice(3, 100, 7) that fall through all fast paths.","commonSituations":"Porting numpy/Python slice semantics to lazy polars frames, or generating slice parameters dynamically and hitting an unhandled combination.","solutions":["Re-express the slice with filter+gather: lf.with_row_index().filter((pl.col(\"index\") >= start) & (pl.col(\"index\") < stop).gather_every-ish logic) or use pl.int_range(start, stop, step).gather on rows.","Collect to an eager DataFrame and slice there if data size allows.","Use supported helpers: head, tail, gather_every, or slice with step == 1 and non-negative stop.","Simplify the slice parameters to one of the optimized patterns (e.g. [:stop], [start:], [::step])."],"exampleFix":"// before\nlf.slice(3, 100, 7)\n// after\nidx = pl.int_range(3, 100, 7)\nlf.with_row_index().filter(pl.col(\"index\").is_in(idx)).drop(\"index\")","handlingStrategy":"try-catch","validationCode":"def is_supported_lazy_slice(start, stop, step=1):\n    if stop is not None and stop < 0:\n        return False\n    if step < 0 and (start > 0 or stop is not None) and start != stop:\n        return not (start > 0 > step and stop is None)\n    return True","typeGuard":null,"tryCatchPattern":"try:\n    result = lf.slice(start, stop, step)\nexcept ValueError as e:\n    if \"not supported by lazy computation\" in str(e):\n        result = lf.with_row_index().filter(\n            (pl.col(\"index\") >= start) & (pl.col(\"index\") < stop)\n        ).drop(\"index\")\n    else:\n        raise","preventionTips":["Centralize slicing through a helper that maps unsupported patterns to filter/gather.","Prefer head/tail/gather_every for the common optimized cases.","Collect eagerly when arbitrary numpy-style slicing is required.","Add tests for every slice-shape your pipeline can generate."],"tags":["polars","lazy","slicing"],"backgroundTag":"unsupported-operation","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}