Window functions¶
API Documentation
ts.Window mirrors pyspark.sql.Window, but takes typed columns. .over() keeps
the column type, and qualify() filters on a window expression in one call:
w = ts.Window.partitionBy(orders.customer_id).orderBy(
orders.amount.desc()
)
# latest order per customer
latest = orders.qualify(tsf.row_number().over(w) == 1)
class Ranked(ts.DataFrame):
order_id: ts.String
rank: ts.Int
customer_total: ts.Double
Ranked(
orders,
order_id=orders.order_id,
rank=tsf.rank().over(w),
customer_total=tsf.sum(orders.amount).over(
ts.Window.partitionBy(orders.customer_id)
),
)
An aggregate with .over() is a regular column, so it does not group the DataFrame.
Frames work as in PySpark: w.rowsBetween(ts.Window.unboundedPreceding, ts.Window.currentRow).