import polars as pl
import scipy as sp
import numpy as npdf = pl.DataFrame({
"species": ["A", "B", "C"],
"N": [57, 72, 53]
})df
shape: (3, 2)
| species | N |
|---|---|
| str | i64 |
| "A" | 57 |
| "B" | 72 |
| "C" | 53 |
def tdf(N):
return sp.stats.t(df = N - 1).ppf(0.975)tdf(57)np.float64(2.003240718847872)
(df
.with_columns(pl.col("N").map_batches(tdf).alias("t"))
.with_columns((pl.col("t") + 1).alias("other"))
)
shape: (3, 4)
| species | N | t | other |
|---|---|---|---|
| str | i64 | f64 | f64 |
| "A" | 57 | 2.003241 | 3.003241 |
| "B" | 72 | 1.993943 | 2.993943 |
| "C" | 53 | 2.006647 | 3.006647 |
tdf(np.array([56, 57, 59]))array([2.00404478, 2.00324072, 2.00171748])