Path: blob/main/py-polars/tests/unit/operations/test_format.py
8415 views
from __future__ import annotations12from typing import TYPE_CHECKING34import pytest56import polars as pl7from polars.testing import assert_frame_equal, assert_series_equal89if TYPE_CHECKING:10from tests.conftest import PlMonkeyPatch111213def test_format_expr() -> None:14a = [1, 2, 3]15b = ["a", "b", None]16df = pl.DataFrame({"a": a, "b": b})1718out = df.select(19y=pl.format("{} abc", pl.lit("xyz")),20z=pl.format("{} abc", pl.col.a),21w=pl.format("{} abc {}", pl.col.a, pl.lit("xyz")),22a=pl.format("{} abc {}", pl.lit("xyz"), pl.col.a),23b=pl.format("abc {} {}", pl.lit("xyz"), pl.col.a),24c=pl.format("abc {} {}", pl.lit("xyz"), pl.col.b),25d=pl.format("abc {} {}", pl.col.a, pl.col.b),26e=pl.format("{} abc {}", pl.col.a, pl.col.b),27f=pl.format("{} {} abc", pl.col.a, pl.col.b),28g=pl.format("{}{}", pl.col.a, pl.col.b),29h=pl.format("{}", pl.col.a),30i=pl.format("{}", pl.col.b),31)3233expected = pl.DataFrame(34{35"y": ["xyz abc"] * 3,36"z": [f"{i} abc" for i in a],37"w": [f"{i} abc xyz" for i in a],38"a": [f"xyz abc {i}" for i in a],39"b": [f"abc xyz {i}" for i in a],40"c": [None if i is None else f"abc xyz {i}" for i in b],41"d": [42None if j is None else f"abc {i} {j}" for i, j in zip(a, b, strict=True)43],44"e": [45None if j is None else f"{i} abc {j}" for i, j in zip(a, b, strict=True)46],47"f": [48None if j is None else f"{i} {j} abc" for i, j in zip(a, b, strict=True)49],50"g": [None if j is None else f"{i}{j}" for i, j in zip(a, b, strict=True)],51"h": [f"{i}" for i in a],52"i": [None if i is None else f"{i}" for i in b],53}54)5556assert_frame_equal(out, expected)575859def test_format_fail_on_unequal() -> None:60with pytest.raises(pl.exceptions.ShapeError):61pl.select(pl.format("abc", pl.lit("x")))6263with pytest.raises(pl.exceptions.ShapeError):64pl.select(pl.format("abc {}"))6566with pytest.raises(pl.exceptions.ShapeError):67pl.select(pl.format("abc {} {}", pl.lit("x"), pl.lit("y"), pl.lit("z")))6869with pytest.raises(pl.exceptions.ShapeError):70pl.select(pl.format("abc {}", pl.lit("x"), pl.lit("y")))717273def test_format_group_by_23858() -> None:74df = (75pl.LazyFrame({"x": [0], "y": [0]})76.group_by("x")77.agg(pl.format("'{}'", pl.col("y")).alias("quoted_ys"))78.with_columns(pl.col("quoted_ys").cast(pl.List(pl.String())).list.join(", "))79.collect()80)81assert_frame_equal(df, pl.DataFrame({"x": [0], "quoted_ys": ["'0'"]}))828384# Flaky - requires POLARS_MAX_THREADS=1 to trigger multiple chunks85# Only valid when run in isolation, see also GH issue #2207086def test_format_on_multiple_chunks_25159(plmonkeypatch: PlMonkeyPatch) -> None:87plmonkeypatch.setenv("POLARS_MAX_THREADS", "1")88df = pl.DataFrame({"group": ["A", "B"]})89df = df.with_columns(90pl.date_ranges(pl.date(2025, 1, 1), pl.date(2025, 1, 3))91).explode("date")92out = df.group_by(pl.all()).agg(93pl.format("{}", (pl.col("date").max()).dt.to_string()).alias("label")94)95assert out.shape == (6, 3)969798def test_format_on_multiple_chunks_concat_25159() -> None:99df1 = pl.DataFrame({"a": ["123"]})100df2 = pl.DataFrame({"a": ["456"]})101df = pl.concat([df1, df2])102out = df.select(pl.format("{}", pl.col.a))103assert_frame_equal(df, out)104105106def test_format_with_nulls_25347() -> None:107assert_series_equal(108pl.DataFrame({"a": [None, "a"]})109.select(a=pl.format("prefix: {}", pl.col.a))110.to_series(),111pl.Series("a", [None, "prefix: a"]),112)113114assert_series_equal(115pl.DataFrame({"a": [None, "y", "z"], "b": ["a", "b", None]})116.select(a=pl.format("prefix: {} {}", pl.col.a, pl.col.b))117.to_series(),118pl.Series("a", [None, "prefix: y b", None]),119)120121122