Path: blob/main/py-polars/tests/unit/functions/test_cumulative_eval.py
8420 views
import pytest12import polars as pl3from polars.testing import assert_frame_equal, assert_series_equal456def test_cumulative_eval_sum() -> None:7assert_frame_equal(8pl.DataFrame({"a": [1, 2, 3]}).with_columns(9b=pl.col.a.cumulative_eval(pl.element().sum())10),11pl.DataFrame({"a": [1, 2, 3], "b": [1, 3, 6]}),12)131415def test_cumulative_eval_group_by() -> None:16assert_frame_equal(17pl.DataFrame({"a": [1, 2, 3, 2, 3, 4, 3], "g": [1, 1, 1, 2, 2, 2, 3]})18.group_by("g")19.agg(b=pl.col.a.cumulative_eval(pl.element().sum())),20pl.DataFrame({"g": [1, 2, 3], "b": [[1, 3, 6], [2, 5, 9], [3]]}),21check_row_order=False,22)232425def test_cumulative_eval_deny_non_scalar() -> None:26with pytest.raises(pl.exceptions.InvalidOperationError, match="scalar"):27(28pl.DataFrame({"a": [1, 2, 3]}).with_columns(29b=pl.col.a.cumulative_eval(pl.element() + 1)30),31)323334def test_cumulative_eval_empty() -> None:35s = pl.Series("a", [], pl.Int64)36assert_series_equal(s.cumulative_eval(pl.element().first()), s)373839def test_cumulative_eval_samples() -> None:40assert_series_equal(41pl.Series("a", [None, None, 1, 2, 3, None, None], pl.Int64).cumulative_eval(42pl.element().first(), min_samples=343),44pl.Series("a", [None, None, None, None, None, None, None], pl.Int64),45)4647assert_series_equal(48pl.Series("a", [None, None, 1, 2, 3, None, None], pl.Int64).cumulative_eval(49pl.element().min(), min_samples=350),51pl.Series("a", [None, None, None, None, 1, 1, 1], pl.Int64),52)535455def test_cumulative_eval_length_preserving_streaming_25293() -> None:56df = pl.DataFrame({"a": [1, 2, 3]})57q = df.lazy().with_columns(58pl.col("a")59.first()60.cumulative_eval(61pl.element().map_batches(lambda x: x.max(), pl.Int64, returns_scalar=True)62)63)64expected = pl.DataFrame({"a": [1, 1, 1]})65assert_frame_equal(q.collect(engine="in-memory"), expected)66assert_frame_equal(q.collect(engine="streaming"), expected)676869