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
8420 views
1
import pytest
2
3
import polars as pl
4
from polars.testing import assert_frame_equal, assert_series_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
35
def test_cumulative_eval_empty() -> None:
36
s = pl.Series("a", [], pl.Int64)
37
assert_series_equal(s.cumulative_eval(pl.element().first()), s)
38
39
40
def test_cumulative_eval_samples() -> None:
41
assert_series_equal(
42
pl.Series("a", [None, None, 1, 2, 3, None, None], pl.Int64).cumulative_eval(
43
pl.element().first(), min_samples=3
44
),
45
pl.Series("a", [None, None, None, None, None, None, None], pl.Int64),
46
)
47
48
assert_series_equal(
49
pl.Series("a", [None, None, 1, 2, 3, None, None], pl.Int64).cumulative_eval(
50
pl.element().min(), min_samples=3
51
),
52
pl.Series("a", [None, None, None, None, 1, 1, 1], pl.Int64),
53
)
54
55
56
def test_cumulative_eval_length_preserving_streaming_25293() -> None:
57
df = pl.DataFrame({"a": [1, 2, 3]})
58
q = df.lazy().with_columns(
59
pl.col("a")
60
.first()
61
.cumulative_eval(
62
pl.element().map_batches(lambda x: x.max(), pl.Int64, returns_scalar=True)
63
)
64
)
65
expected = pl.DataFrame({"a": [1, 1, 1]})
66
assert_frame_equal(q.collect(engine="in-memory"), expected)
67
assert_frame_equal(q.collect(engine="streaming"), expected)
68
69