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
6940 views
1
import pytest
2
3
import polars as pl
4
from polars.testing import assert_frame_equal
5
6
7
def test_implode_22192_22191() -> None:
8
df = pl.DataFrame({"x": [5, 6, 7, 8, 9], "g": [1, 2, 3, 3, 3]})
9
assert df.group_by("g").agg(pl.col.x.implode()).sort("x").to_dict(
10
as_series=False
11
) == {"g": [1, 2, 3], "x": [[5], [6], [7, 8, 9]]}
12
assert df.select(pl.col.x.implode().over("g")).to_dict(as_series=False) == {
13
"x": [[5], [6], [7, 8, 9], [7, 8, 9], [7, 8, 9]]
14
}
15
16
17
@pytest.mark.parametrize("maintain_order", [False, True])
18
def test_implode_agg_lit(maintain_order: bool) -> None:
19
assert_frame_equal(
20
pl.DataFrame()
21
.group_by(pl.lit(1, pl.Int64))
22
.agg(x=pl.lit([3]).list.set_union(pl.lit(1).implode())),
23
pl.DataFrame({"literal": [1], "x": [[3, 1]]}),
24
)
25
26