/
api.py
377 lines (340 loc) · 12.9 KB
/
api.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
from __future__ import annotations
from functools import reduce
from operator import or_
from typing import TYPE_CHECKING, Callable, TypeVar
from warnings import warn
import polars._reexport as pl
from polars._utils.various import find_stacklevel
if TYPE_CHECKING:
from polars import DataFrame, Expr, LazyFrame, Series
__all__ = [
"register_expr_namespace",
"register_dataframe_namespace",
"register_lazyframe_namespace",
"register_series_namespace",
]
# do not allow override of polars' own namespaces (as registered by '_accessors')
_reserved_namespaces: set[str] = reduce(
or_,
(
cls._accessors # type: ignore[attr-defined]
for cls in (pl.DataFrame, pl.Expr, pl.LazyFrame, pl.Series)
),
)
NS = TypeVar("NS")
class NameSpace:
"""Establish property-like namespace object for user-defined functionality."""
def __init__(self, name: str, namespace: type[NS]) -> None:
self._accessor = name
self._ns = namespace
def __get__(self, instance: NS | None, cls: type[NS]) -> NS | type[NS]:
if instance is None:
return self._ns
ns_instance = self._ns(instance) # type: ignore[call-arg]
setattr(instance, self._accessor, ns_instance)
return ns_instance
def _create_namespace(
name: str, cls: type[Expr | DataFrame | LazyFrame | Series]
) -> Callable[[type[NS]], type[NS]]:
"""Register custom namespace against the underlying Polars class."""
def namespace(ns_class: type[NS]) -> type[NS]:
if name in _reserved_namespaces:
msg = f"cannot override reserved namespace {name!r}"
raise AttributeError(msg)
elif hasattr(cls, name):
warn(
f"Overriding existing custom namespace {name!r} (on {cls.__name__!r})",
UserWarning,
stacklevel=find_stacklevel(),
)
setattr(cls, name, NameSpace(name, ns_class))
cls._accessors.add(name)
return ns_class
return namespace
def register_expr_namespace(name: str) -> Callable[[type[NS]], type[NS]]:
"""
Decorator for registering custom functionality with a Polars Expr.
Parameters
----------
name
Name under which the functionality will be accessed.
See Also
--------
register_dataframe_namespace: Register functionality on a DataFrame.
register_lazyframe_namespace: Register functionality on a LazyFrame.
register_series_namespace: Register functionality on a Series.
Examples
--------
>>> @pl.api.register_expr_namespace("pow_n")
... class PowersOfN:
... def __init__(self, expr: pl.Expr):
... self._expr = expr
...
... def next(self, p: int) -> pl.Expr:
... return (p ** (self._expr.log(p).ceil()).cast(pl.Int64)).cast(pl.Int64)
...
... def previous(self, p: int) -> pl.Expr:
... return (p ** (self._expr.log(p).floor()).cast(pl.Int64)).cast(pl.Int64)
...
... def nearest(self, p: int) -> pl.Expr:
... return (p ** (self._expr.log(p)).round(0).cast(pl.Int64)).cast(pl.Int64)
>>>
>>> df = pl.DataFrame([1.4, 24.3, 55.0, 64.001], schema=["n"])
>>> df.select(
... pl.col("n"),
... pl.col("n").pow_n.next(p=2).alias("next_pow2"),
... pl.col("n").pow_n.previous(p=2).alias("prev_pow2"),
... pl.col("n").pow_n.nearest(p=2).alias("nearest_pow2"),
... )
shape: (4, 4)
┌────────┬───────────┬───────────┬──────────────┐
│ n ┆ next_pow2 ┆ prev_pow2 ┆ nearest_pow2 │
│ --- ┆ --- ┆ --- ┆ --- │
│ f64 ┆ i64 ┆ i64 ┆ i64 │
╞════════╪═══════════╪═══════════╪══════════════╡
│ 1.4 ┆ 2 ┆ 1 ┆ 1 │
│ 24.3 ┆ 32 ┆ 16 ┆ 32 │
│ 55.0 ┆ 64 ┆ 32 ┆ 64 │
│ 64.001 ┆ 128 ┆ 64 ┆ 64 │
└────────┴───────────┴───────────┴──────────────┘
"""
return _create_namespace(name, pl.Expr)
def register_dataframe_namespace(name: str) -> Callable[[type[NS]], type[NS]]:
"""
Decorator for registering custom functionality with a Polars DataFrame.
Parameters
----------
name
Name under which the functionality will be accessed.
See Also
--------
register_expr_namespace: Register functionality on an Expr.
register_lazyframe_namespace: Register functionality on a LazyFrame.
register_series_namespace: Register functionality on a Series.
Examples
--------
>>> @pl.api.register_dataframe_namespace("split")
... class SplitFrame:
... def __init__(self, df: pl.DataFrame):
... self._df = df
...
... def by_first_letter_of_column_names(self) -> list[pl.DataFrame]:
... return [
... self._df.select([col for col in self._df.columns if col[0] == f])
... for f in dict.fromkeys(col[0] for col in self._df.columns)
... ]
...
... def by_first_letter_of_column_values(self, col: str) -> list[pl.DataFrame]:
... return [
... self._df.filter(pl.col(col).str.starts_with(c))
... for c in sorted(
... set(df.select(pl.col(col).str.slice(0, 1)).to_series())
... )
... ]
>>>
>>> df = pl.DataFrame(
... data=[["xx", 2, 3, 4], ["xy", 4, 5, 6], ["yy", 5, 6, 7], ["yz", 6, 7, 8]],
... schema=["a1", "a2", "b1", "b2"],
... orient="row",
... )
>>> df
shape: (4, 4)
┌─────┬─────┬─────┬─────┐
│ a1 ┆ a2 ┆ b1 ┆ b2 │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╪═════╡
│ xx ┆ 2 ┆ 3 ┆ 4 │
│ xy ┆ 4 ┆ 5 ┆ 6 │
│ yy ┆ 5 ┆ 6 ┆ 7 │
│ yz ┆ 6 ┆ 7 ┆ 8 │
└─────┴─────┴─────┴─────┘
>>> df.split.by_first_letter_of_column_names()
[shape: (4, 2)
┌─────┬─────┐
│ a1 ┆ a2 │
│ --- ┆ --- │
│ str ┆ i64 │
╞═════╪═════╡
│ xx ┆ 2 │
│ xy ┆ 4 │
│ yy ┆ 5 │
│ yz ┆ 6 │
└─────┴─────┘,
shape: (4, 2)
┌─────┬─────┐
│ b1 ┆ b2 │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════╡
│ 3 ┆ 4 │
│ 5 ┆ 6 │
│ 6 ┆ 7 │
│ 7 ┆ 8 │
└─────┴─────┘]
>>> df.split.by_first_letter_of_column_values("a1")
[shape: (2, 4)
┌─────┬─────┬─────┬─────┐
│ a1 ┆ a2 ┆ b1 ┆ b2 │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╪═════╡
│ xx ┆ 2 ┆ 3 ┆ 4 │
│ xy ┆ 4 ┆ 5 ┆ 6 │
└─────┴─────┴─────┴─────┘, shape: (2, 4)
┌─────┬─────┬─────┬─────┐
│ a1 ┆ a2 ┆ b1 ┆ b2 │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╪═════╡
│ yy ┆ 5 ┆ 6 ┆ 7 │
│ yz ┆ 6 ┆ 7 ┆ 8 │
└─────┴─────┴─────┴─────┘]
"""
return _create_namespace(name, pl.DataFrame)
def register_lazyframe_namespace(name: str) -> Callable[[type[NS]], type[NS]]:
"""
Decorator for registering custom functionality with a Polars LazyFrame.
Parameters
----------
name
Name under which the functionality will be accessed.
See Also
--------
register_expr_namespace: Register functionality on an Expr.
register_dataframe_namespace: Register functionality on a DataFrame.
register_series_namespace: Register functionality on a Series.
Examples
--------
>>> @pl.api.register_lazyframe_namespace("types")
... class DTypeOperations:
... def __init__(self, ldf: pl.LazyFrame):
... self._ldf = ldf
...
... def split_by_column_dtypes(self) -> list[pl.LazyFrame]:
... return [
... self._ldf.select(pl.col(tp))
... for tp in dict.fromkeys(self._ldf.dtypes)
... ]
...
... def upcast_integer_types(self) -> pl.LazyFrame:
... return self._ldf.with_columns(
... pl.col(tp).cast(pl.Int64) for tp in (pl.Int8, pl.Int16, pl.Int32)
... )
>>>
>>> ldf = pl.DataFrame(
... data={"a": [1, 2], "b": [3, 4], "c": [5.6, 6.7]},
... schema=[("a", pl.Int16), ("b", pl.Int32), ("c", pl.Float32)],
... ).lazy()
>>>
>>> ldf.collect()
shape: (2, 3)
┌─────┬─────┬─────┐
│ a ┆ b ┆ c │
│ --- ┆ --- ┆ --- │
│ i16 ┆ i32 ┆ f32 │
╞═════╪═════╪═════╡
│ 1 ┆ 3 ┆ 5.6 │
│ 2 ┆ 4 ┆ 6.7 │
└─────┴─────┴─────┘
>>> ldf.types.upcast_integer_types().collect()
shape: (2, 3)
┌─────┬─────┬─────┐
│ a ┆ b ┆ c │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ f32 │
╞═════╪═════╪═════╡
│ 1 ┆ 3 ┆ 5.6 │
│ 2 ┆ 4 ┆ 6.7 │
└─────┴─────┴─────┘
>>>
>>> ldf = pl.DataFrame(
... data=[["xx", 2, 3, 4], ["xy", 4, 5, 6], ["yy", 5, 6, 7], ["yz", 6, 7, 8]],
... schema=["a1", "a2", "b1", "b2"],
... orient="row",
... ).lazy()
>>>
>>> ldf.collect()
shape: (4, 4)
┌─────┬─────┬─────┬─────┐
│ a1 ┆ a2 ┆ b1 ┆ b2 │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╪═════╡
│ xx ┆ 2 ┆ 3 ┆ 4 │
│ xy ┆ 4 ┆ 5 ┆ 6 │
│ yy ┆ 5 ┆ 6 ┆ 7 │
│ yz ┆ 6 ┆ 7 ┆ 8 │
└─────┴─────┴─────┴─────┘
>>> pl.collect_all(ldf.types.split_by_column_dtypes())
[shape: (4, 1)
┌─────┐
│ a1 │
│ --- │
│ str │
╞═════╡
│ xx │
│ xy │
│ yy │
│ yz │
└─────┘, shape: (4, 3)
┌─────┬─────┬─────┐
│ a2 ┆ b1 ┆ b2 │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╡
│ 2 ┆ 3 ┆ 4 │
│ 4 ┆ 5 ┆ 6 │
│ 5 ┆ 6 ┆ 7 │
│ 6 ┆ 7 ┆ 8 │
└─────┴─────┴─────┘]
"""
return _create_namespace(name, pl.LazyFrame)
def register_series_namespace(name: str) -> Callable[[type[NS]], type[NS]]:
"""
Decorator for registering custom functionality with a polars Series.
Parameters
----------
name
Name under which the functionality will be accessed.
See Also
--------
register_expr_namespace: Register functionality on an Expr.
register_dataframe_namespace: Register functionality on a DataFrame.
register_lazyframe_namespace: Register functionality on a LazyFrame.
Examples
--------
>>> @pl.api.register_series_namespace("math")
... class MathShortcuts:
... def __init__(self, s: pl.Series):
... self._s = s
...
... def square(self) -> pl.Series:
... return self._s * self._s
...
... def cube(self) -> pl.Series:
... return self._s * self._s * self._s
>>>
>>> s = pl.Series("n", [1.5, 31.0, 42.0, 64.5])
>>> s.math.square().alias("s^2")
shape: (4,)
Series: 's^2' [f64]
[
2.25
961.0
1764.0
4160.25
]
>>> s = pl.Series("n", [1, 2, 3, 4, 5])
>>> s.math.cube().alias("s^3")
shape: (5,)
Series: 's^3' [i64]
[
1
8
27
64
125
]
"""
return _create_namespace(name, pl.Series)