datajoely
02/23/2024, 1:54 PMrename_payments
node
https://datajoely.github.io/jaffle-shop-lineage/Nok Lam Chan
02/23/2024, 3:16 PMdatajoely
02/23/2024, 3:18 PMdatajoely
02/23/2024, 3:18 PMdatajoely
02/23/2024, 3:18 PMdef rename_payments(source: ir.Table) -> ir.Table:
# `amount` is currently stored in cents, so we convert it to dollars
amount_in_dollars = (source.amount / 100).name("amount")
renamed = source[
source.id.name("payment_id"),
"order_id",
"payment_method",
amount_in_dollars,
]
return renamed
datajoely
02/23/2024, 3:19 PMdatajoely
02/23/2024, 3:21 PMdef process_orders(
orders: ir.Table, payments: ir.Table, payment_methods: list[str]
) -> Tuple[ir.Table, ir.Table]:
total_amount_by_payment_method = {}
for payment_method in payment_methods:
total_amount_by_payment_method[f"{payment_method}_amount"] = ibis.coalesce(
payments.amount.sum(where=payments.payment_method == payment_method), 0
)
order_payments = payments.group_by("order_id").aggregate(
**total_amount_by_payment_method, total_amount=payments.amount.sum()
)
final = orders.left_join(order_payments, "order_id")[
[
orders.order_id,
orders.customer_id,
orders.order_date,
orders.status,
*[
order_payments[f"{payment_method}_amount"]
for payment_method in payment_methods
],
order_payments.total_amount.name("amount"),
]
]
return final, order_payments.sample(0.5)
datajoely
02/23/2024, 3:21 PMdatajoely
02/23/2024, 3:29 PMDeepyaman Datta
02/23/2024, 3:43 PMdatajoely
02/23/2024, 3:48 PMdatajoely
02/23/2024, 3:49 PMIñigo Hidalgo
02/23/2024, 5:35 PMIñigo Hidalgo
02/23/2024, 5:35 PMdatajoely
02/23/2024, 5:36 PMIñigo Hidalgo
02/23/2024, 6:30 PM