pola-rs/polars · error · ValueError

negative stride is not supported in conjunction with…

Error message

negative stride is not supported in conjunction with start+stop

What it means

ValueError raised when applying a slice to a LazyFrame: a negative stride (step < 0) is combined with an explicit start+stop, which would require collecting and reversing the data. Since LazyFrame cannot efficiently reverse, the slice translation refuses this pattern up front.

Solutions

  1. Use either a negative stride alone or start+stop with positive stride; not both.

Example fix

s[::-1]  # or s[1:10], not s[1:10:-1]
Defensive patterns

Strategy: validation

When it happens

Trigger: Slice combining negative stride with explicit start/stop.

Common situations: See trigger scenarios.


AI-assisted analysis of pola-rs/polars@9b5d73fd00 (2026-08-19). Data as JSON: /api/errors/4ee306e50b0bd274. Report an issue: GitHub.

Appendix: source

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

    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()

        # ---------------------------------------
        # straight-through mappings for "reverse"
        # and/or "gather_every"
        # ---------------------------------------
        # [:]    => clone()

View on GitHub (pinned to 9b5d73fd00)