Path: blob/main/py-polars/tests/unit/operations/test_format.py
7884 views
from typing import Any12import pytest34import polars as pl5from polars.testing import assert_frame_equal, assert_series_equal678def test_format_expr() -> None:9a = [1, 2, 3]10b = ["a", "b", None]11df = pl.DataFrame({"a": a, "b": b})1213out = df.select(14y=pl.format("{} abc", pl.lit("xyz")),15z=pl.format("{} abc", pl.col.a),16w=pl.format("{} abc {}", pl.col.a, pl.lit("xyz")),17a=pl.format("{} abc {}", pl.lit("xyz"), pl.col.a),18b=pl.format("abc {} {}", pl.lit("xyz"), pl.col.a),19c=pl.format("abc {} {}", pl.lit("xyz"), pl.col.b),20d=pl.format("abc {} {}", pl.col.a, pl.col.b),21e=pl.format("{} abc {}", pl.col.a, pl.col.b),22f=pl.format("{} {} abc", pl.col.a, pl.col.b),23g=pl.format("{}{}", pl.col.a, pl.col.b),24h=pl.format("{}", pl.col.a),25i=pl.format("{}", pl.col.b),26)2728expected = pl.DataFrame(29{30"y": ["xyz abc"] * 3,31"z": [f"{i} abc" for i in a],32"w": [f"{i} abc xyz" for i in a],33"a": [f"xyz abc {i}" for i in a],34"b": [f"abc xyz {i}" for i in a],35"c": [None if i is None else f"abc xyz {i}" for i in b],36"d": [None if j is None else f"abc {i} {j}" for i, j in zip(a, b)],37"e": [None if j is None else f"{i} abc {j}" for i, j in zip(a, b)],38"f": [None if j is None else f"{i} {j} abc" for i, j in zip(a, b)],39"g": [None if j is None else f"{i}{j}" for i, j in zip(a, b)],40"h": [f"{i}" for i in a],41"i": [None if i is None else f"{i}" for i in b],42}43)4445assert_frame_equal(out, expected)464748def test_format_fail_on_unequal() -> None:49with pytest.raises(pl.exceptions.ShapeError):50pl.select(pl.format("abc", pl.lit("x")))5152with pytest.raises(pl.exceptions.ShapeError):53pl.select(pl.format("abc {}"))5455with pytest.raises(pl.exceptions.ShapeError):56pl.select(pl.format("abc {} {}", pl.lit("x"), pl.lit("y"), pl.lit("z")))5758with pytest.raises(pl.exceptions.ShapeError):59pl.select(pl.format("abc {}", pl.lit("x"), pl.lit("y")))606162def test_format_group_by_23858() -> None:63df = (64pl.LazyFrame({"x": [0], "y": [0]})65.group_by("x")66.agg(pl.format("'{}'", pl.col("y")).alias("quoted_ys"))67.with_columns(pl.col("quoted_ys").cast(pl.List(pl.String())).list.join(", "))68.collect()69)70assert_frame_equal(df, pl.DataFrame({"x": [0], "quoted_ys": ["'0'"]}))717273# Flaky - requires POLARS_MAX_THREADS=1 to trigger multiple chunks74# Only valid when run in isolation, see also GH issue #2207075def test_format_on_multiple_chunks_25159(monkeypatch: Any) -> None:76monkeypatch.setenv("POLARS_MAX_THREADS", "1")77df = pl.DataFrame({"group": ["A", "B"]})78df = df.with_columns(79pl.date_ranges(pl.date(2025, 1, 1), pl.date(2025, 1, 3))80).explode("date")81out = df.group_by(pl.all()).agg(82pl.format("{}", (pl.col("date").max()).dt.to_string()).alias("label")83)84assert out.shape == (6, 3)858687def test_format_on_multiple_chunks_concat_25159() -> None:88df1 = pl.DataFrame({"a": ["123"]})89df2 = pl.DataFrame({"a": ["456"]})90df = pl.concat([df1, df2])91out = df.select(pl.format("{}", pl.col.a))92assert_frame_equal(df, out)939495def test_format_with_nulls_25347() -> None:96assert_series_equal(97pl.DataFrame({"a": [None, "a"]})98.select(a=pl.format("prefix: {}", pl.col.a))99.to_series(),100pl.Series("a", [None, "prefix: a"]),101)102103assert_series_equal(104pl.DataFrame({"a": [None, "y", "z"], "b": ["a", "b", None]})105.select(a=pl.format("prefix: {} {}", pl.col.a, pl.col.b))106.to_series(),107pl.Series("a", [None, "prefix: y b", None]),108)109110111