Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/functions/test_cumulative_eval.py
6939 views
1
import pytest
2
3
import polars as pl
4
from polars.testing import assert_frame_equal
5
6
7
def test_cumulative_eval_sum() -> None:
8
assert_frame_equal(
9
pl.DataFrame({"a": [1, 2, 3]}).with_columns(
10
b=pl.col.a.cumulative_eval(pl.element().sum())
11
),
12
pl.DataFrame({"a": [1, 2, 3], "b": [1, 3, 6]}),
13
)
14
15
16
def test_cumulative_eval_group_by() -> None:
17
assert_frame_equal(
18
pl.DataFrame({"a": [1, 2, 3, 2, 3, 4, 3], "g": [1, 1, 1, 2, 2, 2, 3]})
19
.group_by("g")
20
.agg(b=pl.col.a.cumulative_eval(pl.element().sum())),
21
pl.DataFrame({"g": [1, 2, 3], "b": [[1, 3, 6], [2, 5, 9], [3]]}),
22
check_row_order=False,
23
)
24
25
26
def test_cumulative_eval_deny_non_scalar() -> None:
27
with pytest.raises(pl.exceptions.InvalidOperationError, match="scalar"):
28
(
29
pl.DataFrame({"a": [1, 2, 3]}).with_columns(
30
b=pl.col.a.cumulative_eval(pl.element() + 1)
31
),
32
)
33
34