pola-rs/polars · error · ValueError

`percentiles` must all be in the range [0, 1]

Error message

`percentiles` must all be in the range [0, 1]

What it means

parse_percentiles normalizes percentile inputs for describe and related APIs and requires every percentile to be within [0, 1]. If any value is outside that range, it raises ValueError with a fixed message. Percentiles are fractions, not percentages, so 50 is invalid while 0.5 is valid.

Solutions

  1. Convert percentages to fractions: [25, 50, 75] -> [0.25, 0.5, 0.75].
  2. Validate before calling: assert all(0 <= p <= 1 for p in percentiles).
  3. Remember the median may be auto-injected (inject_median); only pass sub/super-median percentiles you need.

Example fix

// before
df.describe(percentiles=[25, 50, 75])
// after
df.describe(percentiles=[0.25, 0.5, 0.75])
Defensive patterns

Strategy: validation

Validate before calling

assert percentiles is None or all(0 <= p <= 1 for p in percentiles), 'percentiles must be fractions in [0,1]'

Type guard

def are_valid_percentiles(ps) -> bool:
    return ps is None or all(isinstance(p, (int, float)) and 0 <= p <= 1 for p in ps)

Try / catch

try:
    summary = df.describe(percentiles=percentiles)
except ValueError as e:
    if 'percentiles' in str(e):
        summary = df.describe(percentiles=[p / 100 for p in percentiles])

Prevention

When it happens

Trigger: Calling df.describe(percentiles=[0.25, 0.75, 90]) or passing a float like 1.5 / -0.1; also passing a list containing non-comparable values that make the range check fail.

Common situations: Migrating from libraries that take 0-100 percentages; hardcoding 95 for a '95th percentile'; copy-paste of percentile values as integers.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

Thrown at py-polars/src/polars/_utils/various.py:600

    return False


def parse_percentiles(
    percentiles: Sequence[float] | float | None, *, inject_median: bool = False
) -> Sequence[float]:
    """
    Transforms raw percentiles into our preferred format, adding the 50th percentile.

    Raises a ValueError if the percentile sequence is invalid
    (e.g. outside the range [0, 1])
    """
    if isinstance(percentiles, float):
        percentiles = [percentiles]
    elif percentiles is None:
        percentiles = []
    if not all((0 <= p <= 1) for p in percentiles):
        msg = "`percentiles` must all be in the range [0, 1]"
        raise ValueError(msg)

    sub_50_percentiles = sorted(p for p in percentiles if p < 0.5)
    at_or_above_50_percentiles = sorted(p for p in percentiles if p >= 0.5)

    if inject_median and (
        not at_or_above_50_percentiles or at_or_above_50_percentiles[0] != 0.5
    ):
        at_or_above_50_percentiles = [0.5, *at_or_above_50_percentiles]

    return [*sub_50_percentiles, *at_or_above_50_percentiles]


def re_escape(s: str) -> str:
    """Escape a string for use in a Polars (Rust) regex."""
    # note: almost the same as the standard python 're.escape' function, but
    # escapes _only_ those metachars with meaning to the rust regex crate
    re_rust_metachars = r"\\?()|\[\]{}^$#&~.+*-"
    return re.sub(f"([{re_rust_metachars}])", r"\\\1", s)

View on GitHub (pinned to fe841f959e)