Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/operations/namespaces/test_name.py
8422 views
1
from __future__ import annotations
2
3
from collections import OrderedDict
4
5
import pytest
6
7
import polars as pl
8
from polars.exceptions import ComputeError
9
from polars.testing import assert_frame_equal
10
11
12
def test_name_change_case() -> None:
13
df = pl.DataFrame(
14
schema={"ColX": pl.Int32, "ColY": pl.String},
15
).with_columns(
16
pl.all().name.to_uppercase(),
17
pl.all().name.to_lowercase(),
18
)
19
assert df.schema == OrderedDict(
20
[
21
("ColX", pl.Int32),
22
("ColY", pl.String),
23
("COLX", pl.Int32),
24
("COLY", pl.String),
25
("colx", pl.Int32),
26
("coly", pl.String),
27
]
28
)
29
30
31
def test_name_prefix_suffix() -> None:
32
df = pl.DataFrame(
33
schema={"ColX": pl.Int32, "ColY": pl.String},
34
).with_columns(
35
pl.all().name.prefix("#"),
36
pl.all().name.suffix("!!"),
37
)
38
assert df.schema == OrderedDict(
39
[
40
("ColX", pl.Int32),
41
("ColY", pl.String),
42
("#ColX", pl.Int32),
43
("#ColY", pl.String),
44
("ColX!!", pl.Int32),
45
("ColY!!", pl.String),
46
]
47
)
48
49
50
def test_name_replace() -> None:
51
df = pl.DataFrame(
52
schema={"n_foo": pl.Int32, "n_bar": pl.String, "misc?": pl.Float64},
53
)
54
55
assert df.select(
56
pl.all().name.replace("^n_", "col_"),
57
).schema == {
58
"col_foo": pl.Int32,
59
"col_bar": pl.String,
60
"misc?": pl.Float64,
61
}
62
63
assert df.select(
64
pl.all().name.replace("(a|e|i|o|u)", "#"),
65
).schema == {
66
"n_f##": pl.Int32,
67
"n_b#r": pl.String,
68
"m#sc?": pl.Float64,
69
}
70
71
with pytest.raises(ComputeError, match="repetition operator missing expression"):
72
df.select(
73
pl.all().name.replace("?", "!!"),
74
)
75
76
assert df.select(
77
pl.all().name.replace("?", "!!", literal=True),
78
).schema == {
79
"n_foo": pl.Int32,
80
"n_bar": pl.String,
81
"misc!!": pl.Float64,
82
}
83
84
85
def test_name_update_all() -> None:
86
df = pl.DataFrame(
87
schema={
88
"col1": pl.UInt32,
89
"col2": pl.Float64,
90
"other": pl.UInt64,
91
}
92
)
93
assert (
94
df.select(
95
pl.col("col2").append(pl.col("other")),
96
pl.col("col1").append(pl.col("other")).name.keep(),
97
pl.col("col1").append(pl.col("other")).name.prefix("prefix_"),
98
pl.col("col1").append(pl.col("other")).name.suffix("_suffix"),
99
)
100
).schema == OrderedDict(
101
[
102
("col2", pl.Float64),
103
("col1", pl.UInt64),
104
("prefix_col1", pl.UInt64),
105
("col1_suffix", pl.UInt64),
106
]
107
)
108
109
110
def test_name_map_chain_21164() -> None:
111
df = pl.DataFrame({"MyCol": [0, 1, 2]})
112
assert_frame_equal(
113
df.select(pl.all().name.to_lowercase().name.suffix("_suffix")),
114
df.select(mycol_suffix=pl.col("MyCol")),
115
)
116
117
118
def test_when_then_keep_map_13858() -> None:
119
df = pl.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
120
121
assert_frame_equal(
122
df.with_columns(
123
pl.when(True)
124
.then(pl.int_range(3))
125
.otherwise(pl.all())
126
.name.keep()
127
.name.suffix("_other")
128
),
129
df.with_columns(a_other=pl.int_range(3), b_other=pl.int_range(3)),
130
)
131
132
133
def test_keep_name_struct_field_23669() -> None:
134
df = pl.DataFrame(
135
[
136
pl.Series("foo", [{"x": 1}], pl.Struct({"x": pl.Int64})),
137
pl.Series("bar", [{"x": 2}], pl.Struct({"x": pl.Int64})),
138
]
139
)
140
assert_frame_equal(
141
df.select(pl.all().struct.field("x").name.keep()),
142
pl.DataFrame(
143
[
144
pl.Series("foo", [1], pl.Int64),
145
pl.Series("bar", [2], pl.Int64),
146
]
147
),
148
)
149
150