-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvectorization07.py
More file actions
134 lines (108 loc) · 4.29 KB
/
vectorization07.py
File metadata and controls
134 lines (108 loc) · 4.29 KB
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
import pandas as pd
import polars as pl
import sqlalchemy as sa
import ibis
from ibis import _ as col
import pydiverse.transform as pdt
from pydiverse.pipedag import Flow, Stage, Table, materialize
from pydiverse.transform import λ
from pydiverse.transform.core.verbs import (
mutate, alias, group_by, summarise, arrange, build_query,
)
from pydiverse.transform.eager import PandasTableImpl
from pydiverse.transform.lazy import SQLTableImpl
@materialize(version="1.0.0")
def read_input_data():
titanic = pd.read_csv(
'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/titanic.csv'
)
return Table(titanic, name="titanic")
@materialize(input_type=pd.DataFrame, version="1.0.0")
def task_pandas(titanic: pd.DataFrame):
return (
titanic
.assign(age_bucket=(titanic.age + 4.999).round(-1))
.groupby("age_bucket")
.agg(samples=("age_bucket", "count"), survival_likelyhood=("survived", "mean"))
.sort_values("age_bucket")
)
@materialize(input_type=pl.DataFrame, version="1.0.0")
def task_polars(titanic: pl.DataFrame):
return (
titanic
.with_columns(age_bucket=(((pl.col("age") + 4.999) / 10).round() * 10))
.group_by("age_bucket")
.agg(samples=pl.col("age_bucket").count(),
survival_likelyhood=pl.col("survived").mean())
.sort("age_bucket")
)
@materialize(input_type=PandasTableImpl, version="1.0.0")
def task_transform_df(titanic: pdt.Table):
return (
titanic
>> mutate(age_bucket = round(λ.age + 4.999, -1))
>> group_by(λ.age_bucket)
>> summarise(samples=λ.age_bucket.count(),
survival_likelyhood=λ.survived.mean())
>> arrange(λ.age_bucket)
>> alias("transform_df")
)
@materialize(input_type=SQLTableImpl, lazy=True)
def task_transform_sql(titanic: pdt.Table):
return (
titanic
>> mutate(age_bucket = round(λ.age + 4.999, -1))
>> group_by(λ.age_bucket)
>> summarise(samples=λ.age_bucket.count(),
survival_likelyhood=λ.survived.mean())
>> arrange(λ.age_bucket)
>> alias("transform_sql")
)
@materialize(input_type=ibis.api.Table, lazy=True)
def task_ibis(titanic: ibis.api.Table):
return (
titanic
.mutate(age_bucket = (col.age + ibis.literal(4.999, "decimal")).round(-1))
.group_by(col.age_bucket)
.aggregate(samples=col.age_bucket.count(), survival_likelyhood=col.survived.mean())
.order_by(col.age_bucket)
)
@materialize(input_type=sa.Table, lazy=True)
def task_sqlalchemy(titanic: sa.Table):
age_bucket = sa.func.round(titanic.c.age + 4.999, -1).label("age_bucket")
return sa.select(
age_bucket,
sa.func.count(age_bucket).label("samples"),
sa.func.avg(titanic.c.survived).label("survival_likelyhood")
).select_from(titanic).group_by(age_bucket).order_by(age_bucket)
@materialize(input_type=sa.Table, lazy=True)
def task_sql(titanic: sa.Table):
return sa.text(f"""
SELECT round((titanic.age + 4.999) / CAST(10 AS NUMERIC)) * 10 AS age_bucket,
count(round((titanic.age + 4.999) / CAST(10 AS NUMERIC)) * 10) AS samples,
AVG(titanic.survived) AS survival_likelyhood
FROM {titanic.original.schema}.{titanic.name} AS titanic
GROUP BY round((titanic.age + 4.999) / CAST(10 AS NUMERIC)) * 10
ORDER BY round((titanic.age + 4.999) / CAST(10 AS NUMERIC)) * 10 ASC NULLS LAST
""")
@materialize(input_type=pd.DataFrame, version="1.0.0")
def print_tables(tbls: list[pd.DataFrame]):
for tbl in tbls:
print(f"\n\n{tbl}")
def get_pipeline():
tasks = [task_pandas, task_polars, task_transform_df, task_transform_sql,
task_ibis, task_sqlalchemy, task_sql]
with Flow("flow") as flow:
with Stage("t1_raw_input"):
titanic = read_input_data()
with Stage("t2_transformed_data"):
out_tbls = [task(titanic) for task in tasks]
print_tables(out_tbls)
return flow
if __name__ == "__main__":
import logging
from pydiverse.pipedag.util.structlog import setup_logging
setup_logging(log_level=logging.INFO)
flow = get_pipeline()
result = flow.run()
assert result.successful