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_implode.py
8406 views
1
import polars as pl
2
from polars.testing import assert_frame_equal
3
4
5
def test_implode_22192_22191() -> None:
6
df = pl.DataFrame({"x": [5, 6, 7, 8, 9], "g": [1, 2, 3, 3, 3]})
7
assert df.group_by("g").agg(pl.col.x.implode()).sort("x").to_dict(
8
as_series=False
9
) == {"g": [1, 2, 3], "x": [[5], [6], [7, 8, 9]]}
10
assert df.select(pl.col.x.implode().over("g")).to_dict(as_series=False) == {
11
"x": [[5], [6], [7, 8, 9], [7, 8, 9], [7, 8, 9]]
12
}
13
14
15
def test_implode_agg_lit() -> None:
16
assert_frame_equal(
17
pl.DataFrame()
18
.group_by(pl.lit(1, pl.Int64))
19
.agg(x=pl.lit([3]).list.set_union(pl.lit(1).implode())),
20
pl.DataFrame({"literal": [1], "x": [[3, 1]]}),
21
)
22
23
24
def test_implode_explode_agg() -> None:
25
assert_frame_equal(
26
pl.DataFrame({"a": [1, 2]})
27
.group_by(pl.lit(1, pl.Int64))
28
.agg(pl.col.a.implode().explode().sum()),
29
pl.DataFrame({"literal": [1], "a": [3]}),
30
)
31
32