Path: blob/main/py-polars/tests/unit/lazyframe/test_collect_all.py
8450 views
from pathlib import Path1from typing import cast23import pytest45import polars as pl6from polars.testing import assert_frame_equal789def test_collect_all_type_coercion_21805() -> None:10df = pl.LazyFrame({"A": [1.0, 2.0]})11df = df.with_columns(pl.col("A").shift().fill_null(2))12assert pl.collect_all([df])[0]["A"].to_list() == [2.0, 1.0]131415@pytest.mark.parametrize("optimizations", [pl.QueryOptFlags(), pl.QueryOptFlags.none()])16def test_collect_all(df: pl.DataFrame, optimizations: pl.QueryOptFlags) -> None:17lf1 = df.lazy().select(pl.col("int").sum())18lf2 = df.lazy().select((pl.col("floats") * 2).sum())19out = pl.collect_all([lf1, lf2], optimizations=optimizations)20assert cast("int", out[0].item()) == 621assert cast("float", out[1].item()) == 12.0222324def test_collect_all_issue_26097(tmp_path: Path) -> None:25data = pl.DataFrame({"A": [1]})26tmp_file = tmp_path / "polars-bug-repr.parquet"27data.write_parquet(tmp_file)2829df = pl.scan_parquet(tmp_file).select([pl.col("A")])3031dummy_df = pl.DataFrame({"v": [1]}).lazy().select(pl.len())32results = pl.collect_all([dummy_df, df])3334expected = pl.DataFrame({"A": [1]})35assert_frame_equal(results[1], expected)3637Path(tmp_file).unlink()383940def test_collect_all_groupby_lazy_sink_issue_26296(tmp_path: Path) -> None:41df = pl.DataFrame({"g": ["A"], "v": [1]})42result = df.lazy().group_by("g").agg(pl.col("v").sum())4344tmp_file = tmp_path / "bug.parquet"45sink = result.sink_parquet(tmp_file, lazy=True)46other = result.select(pl.lit(1))4748# Should not raise ColumnNotFoundError: v49results = pl.collect_all([other, sink])5051expected_other = pl.DataFrame({"literal": [1]}, schema={"literal": pl.Int32})52assert_frame_equal(results[0], expected_other)5354expected_sink = pl.DataFrame({"g": ["A"], "v": [1]})55assert_frame_equal(pl.read_parquet(tmp_file), expected_sink)565758