polars.Series.list.sample#

Series.list.sample(
n: int | IntoExprColumn | None = None,
*,
fraction: float | IntoExprColumn | None = None,
with_replacement: bool = False,
shuffle: bool | None = None,
seed: int | None = None,
) Series[source]#

Sample from this list.

Parameters:
n

Number of items to return. Cannot be used with fraction. Defaults to 1 if fraction is None.

fraction

Fraction of items to return. Cannot be used with n.

with_replacement

Allow values to be sampled more than once.

shuffle

Determines the order of the sampled values. If True, sampled values are explicitly shuffled. If False, the relative order of the sampled values is preserved. (i.e. they appear in the same order as the original input list). If None (default), no ordering guarantee; uses the most performant algorithm.

seed

Seed for the random number generator. If set to None (default), a random seed is generated for each sample operation.

Examples

>>> s = pl.Series("values", [[1, 2, 3], [4, 5]])
>>> s.list.sample(n=pl.Series("n", [2, 1]), shuffle=False, seed=1)
shape: (2,)
Series: 'values' [list[i64]]
[
    [2, 3]
    [5]
]