{"record":{"id":"b679faabdd7ccafb","repo":"pola-rs/polars","slug":"negative-stop-is-not-supported-for-lazy-slices","errorCode":null,"errorMessage":"negative stop is not supported for lazy slices","messagePattern":"negative stop is not supported for lazy slices","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/slice.py","lineNumber":138,"sourceCode":"\n    def __init__(self, obj: LazyFrame) -> None:\n        self.obj = obj\n\n    def apply(self, s: slice) -> LazyFrame:\n        \"\"\"\n        Apply a slice operation.\n\n        Note that LazyFrame is designed primarily for efficient computation and does not\n        know its own length so, unlike DataFrame, certain slice patterns (such as those\n        requiring negative stop/step) may not be supported.\n        \"\"\"\n        start = s.start or 0\n        step = s.step or 1\n\n        # fail on operations that require length to do efficiently\n        if s.stop and s.stop < 0:\n            msg = \"negative stop is not supported for lazy slices\"\n            raise ValueError(msg)\n        if step < 0 and (start > 0 or s.stop is not None) and (start != s.stop):\n            if not (start > 0 > step and s.stop is None):\n                msg = \"negative stride is not supported in conjunction with start+stop\"\n                raise ValueError(msg)\n\n        # ---------------------------------------\n        # empty slice patterns\n        # ---------------------------------------\n        # [:0]\n        # [i:<=i]\n        # [i:>=i:-k]\n        if (step > 0 and (s.stop is not None and start >= s.stop)) or (\n            step < 0\n            and (s.start is not None and s.stop is not None and s.stop >= s.start >= 0)\n        ):\n            return self.obj.clear()\n\n        # ---------------------------------------","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_utils/slice.py#L120-L156","documentation":"LazyFrame slicing cannot efficiently apply slices with a negative stop because a lazy frame does not know its own length; the LazyFrameSlicer raises ValueError when s.stop < 0 rather than materializing the whole frame to resolve the index.","triggerScenarios":"Calling .slice(start, stop) / head/tail-like lazy slicing on a LazyFrame or expression with a negative stop, e.g. lf.slice(0, -5) or slicing with a stop < 0 in lazy context.","commonSituations":"Translating Python list-slicing habits (a[:-5]) directly to polars lazy slices.","solutions":["Compute the stop from the length first: lf.select(pl.len()).collect().item() - 5, then slice.","Use negative-aware operations that support length: lf.tail(5) or lf.slice(start, pl.len() - 5) inside select.","Collect (or use .lazy() on materialized data) if you truly need Python-style negative slicing and the data is small.","Express the intent differently, e.g. filter by row index: lf.with_row_index().filter(pl.col(\"index\") < n - 5)."],"exampleFix":"// before\nlf.slice(0, -5)\n// after\nn = lf.select(pl.len()).collect().item()\nlf.slice(0, n - 5)","handlingStrategy":"validation","validationCode":"def validate_lazy_slice(start, stop, step=1):\n    if stop is not None and stop < 0:\n        raise ValueError(\"negative stop unsupported in lazy slice; compute from length\")","typeGuard":null,"tryCatchPattern":"try:\n    result = lf.slice(start, stop)\nexcept ValueError as e:\n    if \"negative stop\" in str(e):\n        n = lf.select(pl.len()).collect().item()\n        result = lf.slice(start, n + stop)\n    else:\n        raise","preventionTips":["Never port Python negative-index slicing directly to lazy slices.","Compute lengths explicitly with lf.select(pl.len()).collect() when needed.","Prefer head/tail helpers over manual slices for edge trimming.","Add a helper wrapper that resolves negative stops before slicing."],"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"}