Generate random numbers and statistical distributions natively in Polars DataFrames
Project description
polars-random
Generate random numbers and statistical distributions natively in Polars DataFrames — a NumPy-style random API exposed as first-class Polars expressions, with reproducible seeds and per-row parameters.
polars-random is a Rust plugin offering four equivalent entry points so it composes naturally with the rest of polars:
| Use case | API |
|---|---|
| "Add a column of random draws to a DataFrame" | df.random.<dist>(...) |
| Same thing, lazy | lf.random.<dist>(...) |
Inside any expression / with_columns / select |
pl.col("x").random.<dist>(...) or polars_random.<dist>(...) |
| Just give me N values as a Series | polars_random.<dist>(..., size=N) |
import polars as pl
import polars_random as pr # registers DataFrame/LazyFrame/Expr namespaces
# 1. eager Series
pr.normal(mean=0.0, std=1.0, size=5, seed=42)
# 2. as a polars expression in any context
df = pl.DataFrame({"id": range(5)})
df.with_columns(noise=pr.normal(mean=0.0, std=1.0, seed=42))
df.with_columns(noise=pl.col("id").random.normal(seed=42))
# 3. as a DataFrame method (returns a new DataFrame with the column appended)
df.random.normal(mean=0.0, std=1.0, seed=42, name="noise")
# 4. inside a lazy pipeline
df.lazy().random.normal(seed=42, name="noise").collect()
Available distributions: rand / uniform, normal, binomial, randint. Every parameter (low, high, mean, std, n, p) accepts a Python scalar, a column name ("my_col"), or any pl.Expr. Nulls in column-valued parameters propagate as null in the output (no panic).
Why polars-random?
- Polars-native — outputs are regular Polars columns, composable with the rest of your pipeline (no NumPy round-trips).
- Per-row parameters —
mean,std,low,high,n,pcan come from other columns, so each row can be drawn from a different distribution. - Reproducible — pass
seed=...for deterministic draws. - Fast — implemented in Rust on top of
rand/rand_distr.
Installation
uv add polars-random
poetry add polars-random
pip install polars-random
How it works (mental model)
Every distribution follows the same shape:
df.random.<distribution>(<params>, seed=None, name=None)
<params>are the distribution's parameters (e.g.low/high,mean/std,n/p).- Each parameter accepts a Python literal, a column name as a string, or a Polars expression (
pl.col(...), arithmetic, etc.). Within a single call, all distribution parameters must be the same kind — either all literals or all expressions/column-names (no mixing). seedmakes the draw reproducible. Omit it for entropy-based randomness.nameis the new column's name. Defaults to the distribution name ("rand","normal","binomial").- The result is a new
pl.DataFramewith the column appended. Calls chain.
Coming from NumPy?
| NumPy | polars-random |
|---|---|
np.random.uniform(low, high, size=n) |
pr.rand(low=low, high=high, size=n) or df.random.rand(low=low, high=high) |
np.random.normal(mean, std, size=n) |
pr.normal(mean=mean, std=std, size=n) |
np.random.binomial(n, p, size=size) |
pr.binomial(n=n, p=p, size=size) |
np.random.randint(low, high, size=n) |
pr.randint(low=low, high=high, size=n) |
np.random.seed(42) (global) |
seed=42 per call |
| Different params per row (loop / vectorize manually) | Pass a column name or pl.col(...) as the parameter |
When used as a DataFrame/LazyFrame method or via the pl.col(...).random namespace, the output length is taken from the parent — no size= needed. Use size=N only with the top-level functions for "give me N values without a frame."
Distributions
df.random.rand (uniform) · also aliased as df.random.uniform
| Parameter | Type | Default | Description |
|---|---|---|---|
low |
float, str, pl.Expr, or None |
0.0 |
Lower bound (inclusive). |
high |
float, str, pl.Expr, or None |
1.0 |
Upper bound (exclusive). |
seed |
int or None |
None |
Reproducible draws. |
name |
str or None |
"rand" |
Output column name. |
import polars as pl
import polars_random
df = pl.DataFrame({
"custom_low": [0.0, 10.0, 100.0],
"custom_high": [1.0, 20.0, 200.0],
})
(
df
# Scalar parameters
.random.rand(low=1_000., high=2_000., seed=42, name="rand_scalar")
# Default range [0, 1)
.random.rand(seed=42, name="rand_default")
# Per-row parameters via expression
.random.rand(low=pl.col("custom_low"), high=pl.col("custom_high"), seed=42, name="rand_expr")
# Per-row parameters via column name
.random.rand(low="custom_low", high="custom_high", seed=42, name="rand_str")
)
df.random.normal
| Parameter | Type | Default | Description |
|---|---|---|---|
mean |
float, str, pl.Expr, or None |
0.0 |
Mean of the normal distribution. |
std |
float, str, pl.Expr, or None |
1.0 |
Standard deviation (must be > 0). |
seed |
int or None |
None |
Reproducible draws. |
name |
str or None |
"normal" |
Output column name. |
import polars as pl
import polars_random
df = pl.DataFrame({
"custom_mean": [0.0, 5.0, -3.0],
"custom_std": [1.0, 2.0, 0.5],
})
(
df
.random.normal(mean=3., std=2., seed=42, name="normal_scalar")
.random.normal(seed=42, name="normal_default") # mean=0, std=1
.random.normal(mean=pl.col("custom_mean"), std=pl.col("custom_std"), seed=42, name="normal_expr")
.random.normal(mean="custom_mean", std="custom_std", seed=42, name="normal_str")
)
df.random.binomial
| Parameter | Type | Default | Description |
|---|---|---|---|
n |
int, str, or pl.Expr |
(required) | Number of trials. |
p |
float, str, or pl.Expr |
(required) | Probability of success on each trial (0 ≤ p ≤ 1). |
seed |
int or None |
None |
Reproducible draws. |
name |
str or None |
"binomial" |
Output column name. |
import polars as pl
import polars_random
df = pl.DataFrame({
"n": [10, 50, 100],
"p": [0.1, 0.5, 0.9],
})
(
df
.random.binomial(n=100, p=.5, seed=42, name="binomial_scalar")
.random.binomial(n=pl.col("n"), p=pl.col("p"), seed=42, name="binomial_expr")
.random.binomial(n="n", p="p", seed=42, name="binomial_str")
)
df.random.randint
Uniform random integers in [low, high) (high is exclusive, matching numpy.random.randint).
| Parameter | Type | Default | Description |
|---|---|---|---|
low |
int, str, or pl.Expr |
0 |
Lower bound (inclusive). |
high |
int, str, or pl.Expr |
2 |
Upper bound (exclusive). |
seed |
int or None |
None |
Reproducible draws. |
name |
str or None |
"randint" |
Output column name. |
df.random.randint(low=0, high=10, seed=42) # one column, scalar bounds
df.random.randint(low="lo", high="hi", seed=42) # per-row bounds via columns
Beyond df.random — same kernel, four entry points
import polars as pl
import polars_random as pr
# 1. Top-level: returns a Series of N random values (NumPy-style).
pr.normal(mean=0, std=1, size=1_000, seed=42)
# 2. Top-level inside any expression (length comes from the surrounding context).
df.with_columns(noise=pr.normal(mean=0, std=1, seed=42))
# 3. Expression namespace — anchor random draws to an existing column.
df.with_columns(noise=pl.col("id").random.normal(mean=0, std=1, seed=42))
# 4. LazyFrame — keep random draws inside a lazy plan.
df.lazy().random.binomial(n=10, p=0.5, seed=42, name="trials").collect()
When a parameter is column-valued (pl.col(...), a column name, or any expression) and contains nulls, the output is null at those rows instead of raising.
Documentation
Full API reference: https://diegoglozano.github.io/polars-random/
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file polars_random-0.4.0.tar.gz.
File metadata
- Download URL: polars_random-0.4.0.tar.gz
- Upload date:
- Size: 139.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0525b4fa289202ab789a47bc1067ff4d3f15b668ce99e548851daeaed8de9d3b
|
|
| MD5 |
2f5e264e95df5e868b115dd747a9c372
|
|
| BLAKE2b-256 |
30b03d2789bf65f66f4c17a971935a88337966779657d58b24eea18665ff6c4d
|
File details
Details for the file polars_random-0.4.0-cp39-abi3-win_amd64.whl.
File metadata
- Download URL: polars_random-0.4.0-cp39-abi3-win_amd64.whl
- Upload date:
- Size: 4.6 MB
- Tags: CPython 3.9+, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f2e68b6b594f194f98148ef7ef04c1ff4fb4ec547cffc3ece199d50bd88a30be
|
|
| MD5 |
d596c169e3a57b7bf2119d2a3a93abb2
|
|
| BLAKE2b-256 |
61b6fd00de44a6f07bf97b5004a487c5fc394b6bc6f92e977c0a46a6bab95926
|
File details
Details for the file polars_random-0.4.0-cp39-abi3-win32.whl.
File metadata
- Download URL: polars_random-0.4.0-cp39-abi3-win32.whl
- Upload date:
- Size: 3.9 MB
- Tags: CPython 3.9+, Windows x86
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4934d5200df3544a7d329a6efe8b465cda0f6d0dcb21e102ab8b3b70dc0e3fee
|
|
| MD5 |
d0054b3f82dabd560d2f5ee785943c8e
|
|
| BLAKE2b-256 |
2bf61308bff9bcf06583dfea0297f627b4e923bfe628e58273183aa33ffc38d8
|
File details
Details for the file polars_random-0.4.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: polars_random-0.4.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 5.2 MB
- Tags: CPython 3.9+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7981b2e1b7c2a76a6b76ad2bd46746bec90b54be0f26df4463ff73a38d7f8c5d
|
|
| MD5 |
83712ebc07613dc0110ff4c65739c052
|
|
| BLAKE2b-256 |
9d8f97603bed42b0ff41b58bd28717796b00f34f8495ec938c1fb5f9710151bc
|
File details
Details for the file polars_random-0.4.0-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.
File metadata
- Download URL: polars_random-0.4.0-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
- Upload date:
- Size: 5.6 MB
- Tags: CPython 3.9+, manylinux: glibc 2.17+ ppc64le
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b71697c8b616db9764277a91ae2a509be99e121678fcef040947ad50277cdc4
|
|
| MD5 |
ba8f1a7501b4cdc500eebce5c1451bcf
|
|
| BLAKE2b-256 |
92b19339b5bbd0dbe78b512e8f48f64fe54f259b0a6f23c126895cb5da77c8cf
|
File details
Details for the file polars_random-0.4.0-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl.
File metadata
- Download URL: polars_random-0.4.0-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
- Upload date:
- Size: 5.5 MB
- Tags: CPython 3.9+, manylinux: glibc 2.17+ i686
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
432c261e2783a95b4d1ffb3090e4c4dcbb905b9d2969f786b7cb2a3035c9c040
|
|
| MD5 |
df36efb6dca97807d359925a98675cc6
|
|
| BLAKE2b-256 |
deb2189f9704b87faf91c8b4015b0b0d9659bf9af9dc3a7eb6ffd48bf0d2a758
|
File details
Details for the file polars_random-0.4.0-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.
File metadata
- Download URL: polars_random-0.4.0-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
- Upload date:
- Size: 4.9 MB
- Tags: CPython 3.9+, manylinux: glibc 2.17+ ARMv7l
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
94933996a439bba1091027f6de36a2101897b51bffd2c269bdacc335859259ff
|
|
| MD5 |
4fd020a41bdca075edddfdb9033f653d
|
|
| BLAKE2b-256 |
c34bd7ccfeb3b25d40b981584a10090b6d66e9156fef7af213785d88e1767c68
|
File details
Details for the file polars_random-0.4.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: polars_random-0.4.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 4.7 MB
- Tags: CPython 3.9+, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e9de5d8324fc2932a8a1d406425a403b9b8ad2718f3eb55946b02144888a5d0e
|
|
| MD5 |
421546c455e2e447288197e92713d2fa
|
|
| BLAKE2b-256 |
0d234cf8fbf3ff80a3f5ec1937f268178e9802e5615c99930d48fa5ad182e202
|
File details
Details for the file polars_random-0.4.0-cp39-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: polars_random-0.4.0-cp39-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 4.3 MB
- Tags: CPython 3.9+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e2769341fa1290ab3eff402572a0c761f6f39e330c1b6db0abd74916818ba00e
|
|
| MD5 |
69cf87cbc272def570aee10fc955fda6
|
|
| BLAKE2b-256 |
6b983b6e7c8157bab0d6355d0cb68df2201a4cd2bb7a8d72a5e1b548fdf286fa
|
File details
Details for the file polars_random-0.4.0-cp39-abi3-macosx_10_12_x86_64.whl.
File metadata
- Download URL: polars_random-0.4.0-cp39-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 4.6 MB
- Tags: CPython 3.9+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f24c84149209a97f07d7a7fe4cfbbfa56feb24d6a03ff210f5d933f655764012
|
|
| MD5 |
2c142e3916db84b72b98adc6fd5e818f
|
|
| BLAKE2b-256 |
9afc5fcee1a51f36bce15ce47f0dfa745a425f44097b8204f0e20345bb6c718c
|