/
col.py
331 lines (284 loc) · 11 KB
/
col.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
from __future__ import annotations
import contextlib
from typing import TYPE_CHECKING, Any, Iterable, Protocol, cast
from polars._utils.wrap import wrap_expr
from polars.datatypes import is_polars_dtype
plr: Any = None
with contextlib.suppress(ImportError): # Module not available when building docs
import polars.polars as plr # type: ignore[no-redef]
if TYPE_CHECKING:
from polars.expr.expr import Expr
from polars.type_aliases import PolarsDataType
__all__ = ["col"]
def _create_col(
name: str | PolarsDataType | Iterable[str] | Iterable[PolarsDataType],
*more_names: str | PolarsDataType,
) -> Expr:
"""Create one or more column expressions representing column(s) in a DataFrame."""
if more_names:
if isinstance(name, str):
names_str = [name]
names_str.extend(more_names) # type: ignore[arg-type]
return wrap_expr(plr.cols(names_str))
elif is_polars_dtype(name):
dtypes = [name]
dtypes.extend(more_names)
return wrap_expr(plr.dtype_cols(dtypes))
else:
msg = (
"invalid input for `col`"
f"\n\nExpected `str` or `DataType`, got {type(name).__name__!r}."
)
raise TypeError(msg)
if isinstance(name, str):
return wrap_expr(plr.col(name))
elif is_polars_dtype(name):
return wrap_expr(plr.dtype_cols([name]))
elif isinstance(name, Iterable):
names = list(name)
if not names:
return wrap_expr(plr.cols(names))
item = names[0]
if isinstance(item, str):
return wrap_expr(plr.cols(names))
elif is_polars_dtype(item):
return wrap_expr(plr.dtype_cols(names))
else:
msg = (
"invalid input for `col`"
"\n\nExpected iterable of type `str` or `DataType`,"
f" got iterable of type {type(item).__name__!r}."
)
raise TypeError(msg)
else:
msg = (
"invalid input for `col`"
f"\n\nExpected `str` or `DataType`, got {type(name).__name__!r}."
)
raise TypeError(msg)
# appease lint by casting `col` with a protocol that conforms to the factory interface
class Column(Protocol):
def __call__(
self,
name: str | PolarsDataType | Iterable[str] | Iterable[PolarsDataType],
*more_names: str | PolarsDataType,
) -> Expr: ...
def __getattr__(self, name: str) -> Expr: ...
# handle attribute lookup on the metaclass (we use the factory uninstantiated)
class ColumnFactoryMeta(type):
def __getattr__(self, name: str) -> Expr:
return _create_col(name)
# factory that creates columns using `col("name")` or `col.name` syntax
class ColumnFactory(metaclass=ColumnFactoryMeta):
"""
Create Polars column expressions.
Notes
-----
An instance of this class is exported under the name `col`. It can be used as
though it were a function by calling, for example, `pl.col("foo")`.
See the :func:`__call__` method for further documentation.
This helper class enables an alternative syntax for creating a column expression
through attribute lookup. For example `col.foo` creates an expression equal to
`col("foo")`.
See the :func:`__getattr__` method for further documentation.
The function call syntax is considered the idiomatic way of constructing a column
expression. The alternative attribute syntax can be useful for quick prototyping as
it can save some keystrokes, but has drawbacks in both expressiveness and
readability.
Examples
--------
>>> from polars import col
>>> df = pl.DataFrame(
... {
... "foo": [1, 2],
... "bar": [3, 4],
... }
... )
Create a new column expression using the standard syntax:
>>> df.with_columns(baz=(col("foo") * col("bar")) / 2)
shape: (2, 3)
┌─────┬─────┬─────┐
│ foo ┆ bar ┆ baz │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ f64 │
╞═════╪═════╪═════╡
│ 1 ┆ 3 ┆ 1.5 │
│ 2 ┆ 4 ┆ 4.0 │
└─────┴─────┴─────┘
Use attribute lookup to create a new column expression:
>>> df.with_columns(baz=(col.foo + col.bar))
shape: (2, 3)
┌─────┬─────┬─────┐
│ foo ┆ bar ┆ baz │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╡
│ 1 ┆ 3 ┆ 4 │
│ 2 ┆ 4 ┆ 6 │
└─────┴─────┴─────┘
"""
def __new__( # type: ignore[misc]
cls,
name: str | PolarsDataType | Iterable[str] | Iterable[PolarsDataType],
*more_names: str | PolarsDataType,
) -> Expr:
"""
Create one or more column expressions representing column(s) in a DataFrame.
Parameters
----------
name
The name or datatype of the column(s) to represent.
Accepts regular expression input.
Regular expressions should start with `^` and end with `$`.
*more_names
Additional names or datatypes of columns to represent,
specified as positional arguments.
Examples
--------
Pass a single column name to represent that column.
>>> df = pl.DataFrame(
... {
... "ham": [1, 2],
... "hamburger": [11, 22],
... "foo": [2, 1],
... "bar": ["a", "b"],
... }
... )
>>> df.select(pl.col("foo"))
shape: (2, 1)
┌─────┐
│ foo │
│ --- │
│ i64 │
╞═════╡
│ 2 │
│ 1 │
└─────┘
Use dot syntax to save keystrokes for quick prototyping.
>>> from polars import col as c
>>> df.select(c.foo + c.ham)
shape: (2, 1)
┌─────┐
│ foo │
│ --- │
│ i64 │
╞═════╡
│ 3 │
│ 3 │
└─────┘
Use the wildcard `*` to represent all columns.
>>> df.select(pl.col("*"))
shape: (2, 4)
┌─────┬───────────┬─────┬─────┐
│ ham ┆ hamburger ┆ foo ┆ bar │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 ┆ str │
╞═════╪═══════════╪═════╪═════╡
│ 1 ┆ 11 ┆ 2 ┆ a │
│ 2 ┆ 22 ┆ 1 ┆ b │
└─────┴───────────┴─────┴─────┘
>>> df.select(pl.col("*").exclude("ham"))
shape: (2, 3)
┌───────────┬─────┬─────┐
│ hamburger ┆ foo ┆ bar │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ str │
╞═══════════╪═════╪═════╡
│ 11 ┆ 2 ┆ a │
│ 22 ┆ 1 ┆ b │
└───────────┴─────┴─────┘
Regular expression input is supported.
>>> df.select(pl.col("^ham.*$"))
shape: (2, 2)
┌─────┬───────────┐
│ ham ┆ hamburger │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═══════════╡
│ 1 ┆ 11 │
│ 2 ┆ 22 │
└─────┴───────────┘
Multiple columns can be represented by passing a list of names.
>>> df.select(pl.col(["hamburger", "foo"]))
shape: (2, 2)
┌───────────┬─────┐
│ hamburger ┆ foo │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═══════════╪═════╡
│ 11 ┆ 2 │
│ 22 ┆ 1 │
└───────────┴─────┘
Or use positional arguments to represent multiple columns in the same way.
>>> df.select(pl.col("hamburger", "foo"))
shape: (2, 2)
┌───────────┬─────┐
│ hamburger ┆ foo │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═══════════╪═════╡
│ 11 ┆ 2 │
│ 22 ┆ 1 │
└───────────┴─────┘
Easily select all columns that match a certain data type by passing that
datatype.
>>> df.select(pl.col(pl.String))
shape: (2, 1)
┌─────┐
│ bar │
│ --- │
│ str │
╞═════╡
│ a │
│ b │
└─────┘
>>> df.select(pl.col(pl.Int64, pl.Float64))
shape: (2, 3)
┌─────┬───────────┬─────┐
│ ham ┆ hamburger ┆ foo │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 │
╞═════╪═══════════╪═════╡
│ 1 ┆ 11 ┆ 2 │
│ 2 ┆ 22 ┆ 1 │
└─────┴───────────┴─────┘
"""
return _create_col(name, *more_names)
# appease sphinx; we actually use '__new__'
def __call__(
self,
name: str | PolarsDataType | Iterable[str] | Iterable[PolarsDataType],
*more_names: str | PolarsDataType,
) -> Expr:
return _create_col(name, *more_names)
def __getattr__(self, name: str) -> Expr:
"""
Create a column expression using attribute syntax.
Note that this syntax does not support passing data
types or multiple column names.
Parameters
----------
name
The name of the column to represent.
Examples
--------
>>> from polars import col as c
>>> df = pl.DataFrame(
... {
... "foo": [1, 2],
... "bar": [3, 4],
... }
... )
>>> df.select(c.foo + c.bar)
shape: (2, 1)
┌─────┐
│ foo │
│ --- │
│ i64 │
╞═════╡
│ 4 │
│ 6 │
└─────┘
"""
return getattr(type(self), name)
col = cast(Column, ColumnFactory)