polars
polars copied to clipboard
Syntactic sugar
Big thanks for the work you are doing maintaining this very nice library!
In the code below two helpers that can improve readability of the code. This helpers make:
- Names of new columns columns will be on the same level. It makes more easier to understand what was added to dataframe.
- Access to the columns three letters shorter. Also removes redundant brackets witch improves readability especially in complex expression with a lot of brackets.
And it seems this improvements can be added without much harm done.
import polars as pl
import numpy as np
class ColHelper:
def __call__(self, key):
return pl.col(key)
def __getattr__(self, key):
return pl.col(key)
col = ColHelper()
def with_sugar(self, *args, **kwargs):
expr_list = list(args)
for key, expr in kwargs.items():
expr_list.append(expr.alias(key))
return self.with_columns(expr_list)
pl.DataFrame.with_sugar = with_sugar
pl.DataFrame([
pl.Series("a", np.arange(100)),
pl.Series("b", np.arange(100, 200)),
pl.Series("c", np.arange(200, 300)),
]).with_sugar( # Suggestion
d = col.a + col.b,
e = ((col.a + col.b) / col.c) + col.a,
).with_columns([ # Current approach
(col("a") + col("b")).alias("d"),
(((col("a") + col("b")) / col("c")) + col("a")).alias("e"),
]).head(5)
a | b | c | d | e |
---|---|---|---|---|
i64 | i64 | i64 | i64 | f64 |
0 | 100 | 200 | 100 | 0.5 |
1 | 101 | 201 | 102 | 1.507463 |
2 | 102 | 202 | 104 | 2.514851 |
3 | 103 | 203 | 106 | 3.522167 |
4 | 104 | 204 | 108 | 4.529412 |