Review generated segment revenue pipeline

from Polars
Python 3.14 advanced 6 min 5 issues to find

Review this generated segment-revenue pipeline before it processes uploaded order files.

Read one CSV from a fixed order directory, retain rows with an optional missing region, derive revenue and tax, enforce a many-to-one customer join, retain unmatched customers as their own segment, and aggregate through one visible lazy execution boundary.

Python
from pathlib import Path

import polars as pl

def segment_revenue(file_name, customers):
    path = Path("/srv/orders") / file_name
    orders = pl.read_csv(path).lazy()

    report = (
        orders
        .with_columns(
            (pl.col("quantity") * pl.col("unit_price")).alias("revenue"),
            (pl.col("revenue") * 0.2).alias("tax"),
        )
        .filter(pl.col("region") != None)
        .join(customers.lazy(), on="customer_id", how="left")
        .group_by("segment")
        .agg((pl.col("revenue") + pl.col("tax")).sum())
        .collect()
    )
    return report

generated code is illustrative, not from any one model

Open in playground
Report an error