Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/operations/aggregation/test_folds.py
6940 views
1
import pytest
2
3
import polars as pl
4
from polars.testing import assert_frame_equal
5
6
7
@pytest.mark.may_fail_cloud # reason: eager execution
8
def test_fold_reduce() -> None:
9
df = pl.DataFrame({"a": [1, 2, 3], "b": [1.0, 2.0, 3.0]})
10
11
out = df.select(
12
pl.fold(acc=pl.lit(0), function=lambda acc, x: acc + x, exprs=pl.all()).alias(
13
"foo"
14
)
15
)
16
assert out["foo"].to_list() == [2, 4, 6]
17
out = df.select(
18
pl.reduce(function=lambda acc, x: acc + x, exprs=pl.all()).alias("foo")
19
)
20
assert out["foo"].to_list() == [2, 4, 6]
21
22
23
@pytest.mark.may_fail_cloud # reason: eager execution
24
def test_cum_fold() -> None:
25
df = pl.DataFrame(
26
{
27
"a": [1, 2, 3, 4],
28
"b": [5, 6, 7, 8],
29
"c": [10, 20, 30, 40],
30
}
31
)
32
result = df.select(pl.cum_fold(pl.lit(0, pl.Int64), lambda a, b: a + b, pl.all()))
33
expected = pl.DataFrame(
34
{
35
"cum_fold": [
36
{"a": 1, "b": 6, "c": 16},
37
{"a": 2, "b": 8, "c": 28},
38
{"a": 3, "b": 10, "c": 40},
39
{"a": 4, "b": 12, "c": 52},
40
]
41
}
42
)
43
assert_frame_equal(result, expected)
44
45
46
def test_cum_reduce() -> None:
47
df = pl.DataFrame(
48
{
49
"a": [1, 2, 3, 4],
50
"b": [5, 6, 7, 8],
51
"c": [10, 20, 30, 40],
52
}
53
)
54
result = df.select(pl.cum_reduce(lambda a, b: a + b, pl.all()))
55
expected = pl.DataFrame(
56
{
57
"cum_reduce": [
58
{"a": 1, "b": 6, "c": 16},
59
{"a": 2, "b": 8, "c": 28},
60
{"a": 3, "b": 10, "c": 40},
61
{"a": 4, "b": 12, "c": 52},
62
]
63
}
64
)
65
assert_frame_equal(result, expected)
66
67
68
def test_alias_prune_in_fold_15438() -> None:
69
df = pl.DataFrame({"x": [1, 2], "expected_result": ["first", "second"]}).select(
70
actual_result=pl.fold(
71
acc=pl.lit("other", dtype=pl.Utf8),
72
function=lambda acc, x: pl.select(
73
pl.when(x).then(pl.lit(x.name)).otherwise(acc)
74
).to_series(),
75
exprs=[
76
(pl.col("x") == 1).alias("first"),
77
(pl.col("x") == 2).alias("second"),
78
],
79
)
80
)
81
expected = pl.DataFrame(
82
{
83
"actual_result": ["first", "second"],
84
}
85
)
86
assert_frame_equal(df, expected)
87
88