Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/operations/test_format.py
7884 views
1
from typing import Any
2
3
import pytest
4
5
import polars as pl
6
from polars.testing import assert_frame_equal, assert_series_equal
7
8
9
def test_format_expr() -> None:
10
a = [1, 2, 3]
11
b = ["a", "b", None]
12
df = pl.DataFrame({"a": a, "b": b})
13
14
out = df.select(
15
y=pl.format("{} abc", pl.lit("xyz")),
16
z=pl.format("{} abc", pl.col.a),
17
w=pl.format("{} abc {}", pl.col.a, pl.lit("xyz")),
18
a=pl.format("{} abc {}", pl.lit("xyz"), pl.col.a),
19
b=pl.format("abc {} {}", pl.lit("xyz"), pl.col.a),
20
c=pl.format("abc {} {}", pl.lit("xyz"), pl.col.b),
21
d=pl.format("abc {} {}", pl.col.a, pl.col.b),
22
e=pl.format("{} abc {}", pl.col.a, pl.col.b),
23
f=pl.format("{} {} abc", pl.col.a, pl.col.b),
24
g=pl.format("{}{}", pl.col.a, pl.col.b),
25
h=pl.format("{}", pl.col.a),
26
i=pl.format("{}", pl.col.b),
27
)
28
29
expected = pl.DataFrame(
30
{
31
"y": ["xyz abc"] * 3,
32
"z": [f"{i} abc" for i in a],
33
"w": [f"{i} abc xyz" for i in a],
34
"a": [f"xyz abc {i}" for i in a],
35
"b": [f"abc xyz {i}" for i in a],
36
"c": [None if i is None else f"abc xyz {i}" for i in b],
37
"d": [None if j is None else f"abc {i} {j}" for i, j in zip(a, b)],
38
"e": [None if j is None else f"{i} abc {j}" for i, j in zip(a, b)],
39
"f": [None if j is None else f"{i} {j} abc" for i, j in zip(a, b)],
40
"g": [None if j is None else f"{i}{j}" for i, j in zip(a, b)],
41
"h": [f"{i}" for i in a],
42
"i": [None if i is None else f"{i}" for i in b],
43
}
44
)
45
46
assert_frame_equal(out, expected)
47
48
49
def test_format_fail_on_unequal() -> None:
50
with pytest.raises(pl.exceptions.ShapeError):
51
pl.select(pl.format("abc", pl.lit("x")))
52
53
with pytest.raises(pl.exceptions.ShapeError):
54
pl.select(pl.format("abc {}"))
55
56
with pytest.raises(pl.exceptions.ShapeError):
57
pl.select(pl.format("abc {} {}", pl.lit("x"), pl.lit("y"), pl.lit("z")))
58
59
with pytest.raises(pl.exceptions.ShapeError):
60
pl.select(pl.format("abc {}", pl.lit("x"), pl.lit("y")))
61
62
63
def test_format_group_by_23858() -> None:
64
df = (
65
pl.LazyFrame({"x": [0], "y": [0]})
66
.group_by("x")
67
.agg(pl.format("'{}'", pl.col("y")).alias("quoted_ys"))
68
.with_columns(pl.col("quoted_ys").cast(pl.List(pl.String())).list.join(", "))
69
.collect()
70
)
71
assert_frame_equal(df, pl.DataFrame({"x": [0], "quoted_ys": ["'0'"]}))
72
73
74
# Flaky - requires POLARS_MAX_THREADS=1 to trigger multiple chunks
75
# Only valid when run in isolation, see also GH issue #22070
76
def test_format_on_multiple_chunks_25159(monkeypatch: Any) -> None:
77
monkeypatch.setenv("POLARS_MAX_THREADS", "1")
78
df = pl.DataFrame({"group": ["A", "B"]})
79
df = df.with_columns(
80
pl.date_ranges(pl.date(2025, 1, 1), pl.date(2025, 1, 3))
81
).explode("date")
82
out = df.group_by(pl.all()).agg(
83
pl.format("{}", (pl.col("date").max()).dt.to_string()).alias("label")
84
)
85
assert out.shape == (6, 3)
86
87
88
def test_format_on_multiple_chunks_concat_25159() -> None:
89
df1 = pl.DataFrame({"a": ["123"]})
90
df2 = pl.DataFrame({"a": ["456"]})
91
df = pl.concat([df1, df2])
92
out = df.select(pl.format("{}", pl.col.a))
93
assert_frame_equal(df, out)
94
95
96
def test_format_with_nulls_25347() -> None:
97
assert_series_equal(
98
pl.DataFrame({"a": [None, "a"]})
99
.select(a=pl.format("prefix: {}", pl.col.a))
100
.to_series(),
101
pl.Series("a", [None, "prefix: a"]),
102
)
103
104
assert_series_equal(
105
pl.DataFrame({"a": [None, "y", "z"], "b": ["a", "b", None]})
106
.select(a=pl.format("prefix: {} {}", pl.col.a, pl.col.b))
107
.to_series(),
108
pl.Series("a", [None, "prefix: y b", None]),
109
)
110
111