pola-rs/polars · error · ValueError

method must be one of

Error message

method must be one of {{'pearson', 'spearman'}}, got {method!r}

What it means

`pl.corr` only supports 'pearson' and 'spearman' correlation methods; any other string reaches the final else branch and raises ValueError. The f-string uses doubled braces so the message shows the literal set of valid methods.

Solutions

  1. Use exactly 'pearson' or 'spearman' (lowercase)
  2. Check for case/typo errors in the method argument
  3. Implement other correlations (e.g. kendall) outside polars if needed

Example fix

// before
pl.corr("a", "b", method="Pearson")
// after
pl.corr("a", "b", method="pearson")
Defensive patterns

Strategy: validation

Validate before calling

VALID_CORR_METHODS = {"pearson", "spearman"}
if method not in VALID_CORR_METHODS:
    raise ValueError(f"method must be one of {VALID_CORR_METHODS}")

Type guard

def is_valid_corr_method(m): return isinstance(m, str) and m in {"pearson", "spearman"}

Prevention

When it happens

Trigger: Calling `pl.corr(a, b, method="spearman rank")`, `method="Pearson"` (case-sensitive), or any other misspelled/unsupported method string.

Common situations: Case mistakes ('Pearson'), confusion with other libraries' method strings (e.g. pandas accepts 'kendall' — polars does not), typos.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at py-polars/src/polars/functions/lazy.py:957

            msg = "expected at least one Series in 'corr' inputs if 'eager=True'"
            raise ValueError(msg)

        frame = pl.DataFrame([e for e in (a, b) if isinstance(e, pl.Series)])
        exprs = ((e.name if isinstance(e, pl.Series) else e) for e in (a, b))
        return frame.select(
            corr(*exprs, eager=False, method=method, propagate_nans=propagate_nans)
        ).to_series()
    else:
        a_pyexpr = parse_into_expression(a)
        b_pyexpr = parse_into_expression(b)

        if method == "pearson":
            return wrap_expr(plr.pearson_corr(a_pyexpr, b_pyexpr))
        elif method == "spearman":
            return wrap_expr(plr.spearman_rank_corr(a_pyexpr, b_pyexpr, propagate_nans))
        else:
            msg = f"method must be one of {{'pearson', 'spearman'}}, got {method!r}"
            raise ValueError(msg)


@overload
def cov(
    a: IntoExpr,
    b: IntoExpr,
    *,
    ddof: int = ...,
    eager: Literal[False] = ...,
) -> Expr: ...


@overload
def cov(
    a: IntoExpr,
    b: IntoExpr,
    *,
    ddof: int = ...,

View on GitHub (pinned to fe841f959e)