pola-rs/polars · error · ValueError

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

Error message

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

What it means

pl.corr accepts exactly two methods, 'pearson' and 'spearman'; anything else raises ValueError listing the allowed pair. Kendall correlation is not implemented natively, and old/alternate spellings like 'spearman_rank' or wrong-case 'Pearson' are rejected.

Source

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

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

Solutions

  1. Use method='spearman' for rank correlation or 'pearson' for linear
  2. For Kendall tau, drop to scipy.stats.kendalltau on the Series' .to_numpy() output
  3. Normalize config strings (strip + lower) and whitelist them at load time

Example fix

# before
pl.corr(s1, s2, method='kendall')  # ValueError

# after (spearman is the supported rank correlation)
pl.corr(s1, s2, method='spearman')
Defensive patterns

Strategy: validation

Validate before calling

method = method.strip().lower()
if method not in ('pearson', 'spearman'):
    raise ValueError(f"method must be 'pearson' or 'spearman', got {method!r}")
result = pl.corr(a, b, method=method, eager=eager)

Prevention

When it happens

Trigger: pl.corr(a, b, method='kendall'); method='spearman_rank'; method='Pearson'; a method string read from config or CLI and forwarded unvalidated.

Common situations: Porting scipy.stats vocabulary; stale option names after upgrades; case-sensitive config values.

Related errors


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