-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
name.py
348 lines (291 loc) · 11.7 KB
/
name.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
from __future__ import annotations
from typing import TYPE_CHECKING, Callable
if TYPE_CHECKING:
from polars import Expr
class ExprNameNameSpace:
"""Namespace for expressions that operate on expression names."""
_accessor = "name"
def __init__(self, expr: Expr):
self._from_pyexpr = expr._from_pyexpr
self._pyexpr = expr._pyexpr
def keep(self) -> Expr:
"""
Keep the original root name of the expression.
Notes
-----
This will undo any previous renaming operations on the expression.
Due to implementation constraints, this method can only be called as the last
expression in a chain. Only one name operation per expression will work.
Consider using `.name.map` for advanced renaming.
See Also
--------
Expr.alias
map
Examples
--------
Prevent errors due to potential duplicate column names.
>>> df = pl.DataFrame(
... {
... "a": [1, 2],
... "b": [3, 4],
... }
... )
>>> df.select((pl.lit(10) / pl.all()).name.keep())
shape: (2, 2)
┌──────┬──────────┐
│ a ┆ b │
│ --- ┆ --- │
│ f64 ┆ f64 │
╞══════╪══════════╡
│ 10.0 ┆ 3.333333 │
│ 5.0 ┆ 2.5 │
└──────┴──────────┘
Undo an alias operation.
>>> df.with_columns((pl.col("a") * 9).alias("c").name.keep())
shape: (2, 2)
┌─────┬─────┐
│ a ┆ b │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════╡
│ 9 ┆ 3 │
│ 18 ┆ 4 │
└─────┴─────┘
"""
return self._from_pyexpr(self._pyexpr.name_keep())
def map(self, function: Callable[[str], str]) -> Expr:
"""
Rename the output of an expression by mapping a function over the root name.
Notes
-----
This will undo any previous renaming operations on the expression.
Due to implementation constraints, this method can only be called as the last
expression in a chain. Only one name operation per expression will work.
Parameters
----------
function
Function that maps a root name to a new name.
See Also
--------
keep
prefix
suffix
Examples
--------
Remove a common suffix and convert to lower case.
>>> df = pl.DataFrame(
... {
... "A_reverse": [3, 2, 1],
... "B_reverse": ["z", "y", "x"],
... }
... )
>>> df.with_columns(
... pl.all().reverse().name.map(lambda c: c.rstrip("_reverse").lower())
... )
shape: (3, 4)
┌───────────┬───────────┬─────┬─────┐
│ A_reverse ┆ B_reverse ┆ a ┆ b │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ i64 ┆ str │
╞═══════════╪═══════════╪═════╪═════╡
│ 3 ┆ z ┆ 1 ┆ x │
│ 2 ┆ y ┆ 2 ┆ y │
│ 1 ┆ x ┆ 3 ┆ z │
└───────────┴───────────┴─────┴─────┘
"""
return self._from_pyexpr(self._pyexpr.name_map(function))
def prefix(self, prefix: str) -> Expr:
"""
Add a prefix to the root column name of the expression.
Parameters
----------
prefix
Prefix to add to the root column name.
Notes
-----
This will undo any previous renaming operations on the expression.
Due to implementation constraints, this method can only be called as the last
expression in a chain. Only one name operation per expression will work.
Consider using `.name.map` for advanced renaming.
See Also
--------
suffix
Examples
--------
>>> df = pl.DataFrame(
... {
... "a": [1, 2, 3],
... "b": ["x", "y", "z"],
... }
... )
>>> df.with_columns(pl.all().reverse().name.prefix("reverse_"))
shape: (3, 4)
┌─────┬─────┬───────────┬───────────┐
│ a ┆ b ┆ reverse_a ┆ reverse_b │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ i64 ┆ str │
╞═════╪═════╪═══════════╪═══════════╡
│ 1 ┆ x ┆ 3 ┆ z │
│ 2 ┆ y ┆ 2 ┆ y │
│ 3 ┆ z ┆ 1 ┆ x │
└─────┴─────┴───────────┴───────────┘
"""
return self._from_pyexpr(self._pyexpr.name_prefix(prefix))
def suffix(self, suffix: str) -> Expr:
"""
Add a suffix to the root column name of the expression.
Parameters
----------
suffix
Suffix to add to the root column name.
Notes
-----
This will undo any previous renaming operations on the expression.
Due to implementation constraints, this method can only be called as the last
expression in a chain. Only one name operation per expression will work.
Consider using `.name.map` for advanced renaming.
See Also
--------
prefix
Examples
--------
>>> df = pl.DataFrame(
... {
... "a": [1, 2, 3],
... "b": ["x", "y", "z"],
... }
... )
>>> df.with_columns(pl.all().reverse().name.suffix("_reverse"))
shape: (3, 4)
┌─────┬─────┬───────────┬───────────┐
│ a ┆ b ┆ a_reverse ┆ b_reverse │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ i64 ┆ str │
╞═════╪═════╪═══════════╪═══════════╡
│ 1 ┆ x ┆ 3 ┆ z │
│ 2 ┆ y ┆ 2 ┆ y │
│ 3 ┆ z ┆ 1 ┆ x │
└─────┴─────┴───────────┴───────────┘
"""
return self._from_pyexpr(self._pyexpr.name_suffix(suffix))
def to_lowercase(self) -> Expr:
"""
Make the root column name lowercase.
Notes
-----
This will undo any previous renaming operations on the expression.
Due to implementation constraints, this method can only be called as the last
expression in a chain. Only one name operation per expression will work.
Consider using `.name.map` for advanced renaming.
See Also
--------
prefix
suffix
to_uppercase
Examples
--------
>>> df = pl.DataFrame(
... {
... "ColX": [1, 2, 3],
... "ColY": ["x", "y", "z"],
... }
... )
>>> df.with_columns(pl.all().name.to_lowercase())
shape: (3, 4)
┌──────┬──────┬──────┬──────┐
│ ColX ┆ ColY ┆ colx ┆ coly │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ i64 ┆ str │
╞══════╪══════╪══════╪══════╡
│ 1 ┆ x ┆ 1 ┆ x │
│ 2 ┆ y ┆ 2 ┆ y │
│ 3 ┆ z ┆ 3 ┆ z │
└──────┴──────┴──────┴──────┘
"""
return self._from_pyexpr(self._pyexpr.name_to_lowercase())
def to_uppercase(self) -> Expr:
"""
Make the root column name uppercase.
Notes
-----
This will undo any previous renaming operations on the expression.
Due to implementation constraints, this method can only be called as the last
expression in a chain. Only one name operation per expression will work.
Consider using `.name.map` for advanced renaming.
See Also
--------
prefix
suffix
to_lowercase
Examples
--------
>>> df = pl.DataFrame(
... {
... "ColX": [1, 2, 3],
... "ColY": ["x", "y", "z"],
... }
... )
>>> df.with_columns(pl.all().name.to_uppercase())
shape: (3, 4)
┌──────┬──────┬──────┬──────┐
│ ColX ┆ ColY ┆ COLX ┆ COLY │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ i64 ┆ str │
╞══════╪══════╪══════╪══════╡
│ 1 ┆ x ┆ 1 ┆ x │
│ 2 ┆ y ┆ 2 ┆ y │
│ 3 ┆ z ┆ 3 ┆ z │
└──────┴──────┴──────┴──────┘
"""
return self._from_pyexpr(self._pyexpr.name_to_uppercase())
def map_fields(self, function: Callable[[str], str]) -> Expr:
"""
Rename fields of a struct by mapping a function over the field name.
Notes
-----
This only take effects for struct.
Parameters
----------
function
Function that maps a field name to a new name.
Examples
--------
>>> df = pl.DataFrame({"x": {"a": 1, "b": 2}})
>>> df.select(pl.col("x").name.map_fields(lambda x: x.upper())).schema
Schema({'x': Struct({'A': Int64, 'B': Int64})})
"""
return self._from_pyexpr(self._pyexpr.name_map_fields(function))
def prefix_fields(self, prefix: str) -> Expr:
"""
Add a prefix to all fields name of a struct.
Notes
-----
This only take effects for struct.
Parameters
----------
prefix
Prefix to add to the filed name
Examples
--------
>>> df = pl.DataFrame({"x": {"a": 1, "b": 2}})
>>> df.select(pl.col("x").name.prefix_fields("prefix_")).schema
Schema({'x': Struct({'prefix_a': Int64, 'prefix_b': Int64})})
"""
return self._from_pyexpr(self._pyexpr.name_prefix_fields(prefix))
def suffix_fields(self, suffix: str) -> Expr:
"""
Add a suffix to all fields name of a struct.
Notes
-----
This only take effects for struct.
Parameters
----------
suffix
Suffix to add to the filed name
Examples
--------
>>> df = pl.DataFrame({"x": {"a": 1, "b": 2}})
>>> df.select(pl.col("x").name.suffix_fields("_suffix")).schema
Schema({'x': Struct({'a_suffix': Int64, 'b_suffix': Int64})})
"""
return self._from_pyexpr(self._pyexpr.name_suffix_fields(suffix))