Skip to content

Blog

Problems polars-stats ran into, and what solved them. Each post tells the story and links to the reference pages for the detail.

Subscribe through the RSS feed.

Where the plan stops: scipy.stats inside a LazyFrame

scipy.stats already handles per-row distributions. Pass arrays as the parameters and a frozen distribution broadcasts them against the evaluation points, so scoring each sensor reading against its own sensor's baseline is one call:

import numpy as np
from scipy import stats

reading = np.array([9.8, 10.4, 135.0])
mu = np.array([10.0, 10.0, 100.0])
sigma = np.array([0.5, 0.5, 2.0])

print(stats.norm(loc=mu, scale=sigma).sf(reading))
[6.55421742e-01 2.11855399e-01 7.16345877e-69]

For the arithmetic, a Polars user needs nothing more. The trouble starts with where the result lands, which is a NumPy array, outside both the query plan and the expression API.