pola-rs/polars · error · ValueError

negative stop is not supported for lazy slices

Error message

negative stop is not supported for lazy slices

What it means

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.

Solutions

  1. Compute the stop from the length first: lf.select(pl.len()).collect().item() - 5, then slice.
  2. Use negative-aware operations that support length: lf.tail(5) or lf.slice(start, pl.len() - 5) inside select.
  3. Collect (or use .lazy() on materialized data) if you truly need Python-style negative slicing and the data is small.
  4. Express the intent differently, e.g. filter by row index: lf.with_row_index().filter(pl.col("index") < n - 5).

Example fix

// before
lf.slice(0, -5)
// after
n = lf.select(pl.len()).collect().item()
lf.slice(0, n - 5)
Defensive patterns

Strategy: validation

Validate before calling

def validate_lazy_slice(start, stop, step=1):
    if stop is not None and stop < 0:
        raise ValueError("negative stop unsupported in lazy slice; compute from length")

Try / catch

try:
    result = lf.slice(start, stop)
except ValueError as e:
    if "negative stop" in str(e):
        n = lf.select(pl.len()).collect().item()
        result = lf.slice(start, n + stop)
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: Translating Python list-slicing habits (a[:-5]) directly to polars lazy slices.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/b679faabdd7ccafb. Report an issue: GitHub.

Appendix: source

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

    def __init__(self, obj: LazyFrame) -> None:
        self.obj = obj

    def apply(self, s: slice) -> LazyFrame:
        """
        Apply a slice operation.

        Note that LazyFrame is designed primarily for efficient computation and does not
        know its own length so, unlike DataFrame, certain slice patterns (such as those
        requiring negative stop/step) may not be supported.
        """
        start = s.start or 0
        step = s.step or 1

        # fail on operations that require length to do efficiently
        if s.stop and s.stop < 0:
            msg = "negative stop is not supported for lazy slices"
            raise ValueError(msg)
        if step < 0 and (start > 0 or s.stop is not None) and (start != s.stop):
            if not (start > 0 > step and s.stop is None):
                msg = "negative stride is not supported in conjunction with start+stop"
                raise ValueError(msg)

        # ---------------------------------------
        # empty slice patterns
        # ---------------------------------------
        # [:0]
        # [i:<=i]
        # [i:>=i:-k]
        if (step > 0 and (s.stop is not None and start >= s.stop)) or (
            step < 0
            and (s.start is not None and s.stop is not None and s.stop >= s.start >= 0)
        ):
            return self.obj.clear()

        # ---------------------------------------

View on GitHub (pinned to fe841f959e)