polars.cov#

polars.cov(a: IntoExpr, b: IntoExpr, *, ddof: int = 1, eager: bool = False) Expr | Series[source]#

Compute the covariance between two columns/ expressions.

Parameters:
a

Column name or Expression.

b

Column name or Expression.

ddof

“Delta Degrees of Freedom”: the divisor used in the calculation is N - ddof, where N represents the number of elements. By default ddof is 1.

eager

Evaluate immediately and return a Series; this requires that at least one of the given arguments is a Series. If set to False (default), return an expression instead.

Examples

>>> df = pl.DataFrame(
...     {
...         "a": [1, 8, 3],
...         "b": [4, 5, 2],
...         "c": ["foo", "bar", "foo"],
...     },
... )
>>> df.select(
...     x=pl.cov("a", "b"),
...     y=pl.cov("a", "b", ddof=2),
... )
shape: (1, 2)
┌─────┬─────┐
│ x   ┆ y   │
│ --- ┆ --- │
│ f64 ┆ f64 │
╞═════╪═════╡
│ 3.0 ┆ 6.0 │
└─────┴─────┘

Eager evaluation:

>>> s1 = pl.Series("a", [1, 8, 3])
>>> s2 = pl.Series("b", [4, 5, 2])
>>> pl.cov(s1, s2, eager=True)
shape: (1,)
Series: 'a' [f64]
[
    3.0
]