Path: blob/main/py-polars/tests/unit/operations/aggregation/test_implode.py
8406 views
import polars as pl1from polars.testing import assert_frame_equal234def test_implode_22192_22191() -> None:5df = pl.DataFrame({"x": [5, 6, 7, 8, 9], "g": [1, 2, 3, 3, 3]})6assert df.group_by("g").agg(pl.col.x.implode()).sort("x").to_dict(7as_series=False8) == {"g": [1, 2, 3], "x": [[5], [6], [7, 8, 9]]}9assert df.select(pl.col.x.implode().over("g")).to_dict(as_series=False) == {10"x": [[5], [6], [7, 8, 9], [7, 8, 9], [7, 8, 9]]11}121314def test_implode_agg_lit() -> None:15assert_frame_equal(16pl.DataFrame()17.group_by(pl.lit(1, pl.Int64))18.agg(x=pl.lit([3]).list.set_union(pl.lit(1).implode())),19pl.DataFrame({"literal": [1], "x": [[3, 1]]}),20)212223def test_implode_explode_agg() -> None:24assert_frame_equal(25pl.DataFrame({"a": [1, 2]})26.group_by(pl.lit(1, pl.Int64))27.agg(pl.col.a.implode().explode().sum()),28pl.DataFrame({"literal": [1], "a": [3]}),29)303132