Skip to main content
Open in Kaggle  Open in Colab  Download Notebook
This documentation page is also available as an interactive notebook. You can launch the notebook in Kaggle or Colab, or download it for use with an IDE or local Jupyter installation, by clicking one of the above links.
Build reusable aggregation logic for group-by queries and analytics.

Problem

You need aggregations beyond the built-in sum, count, mean, min, max — such as collecting values into a list, concatenating strings, or computing custom statistics.

Solution

What’s in this recipe:
  • Define a UDA (User-Defined Aggregate) with the @pxt.uda decorator
  • Use UDAs in group_by queries
  • Create UDAs with multiple inputs

Setup

Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/pjlb/.pixeltable/pgdata
Created directory ‘uda_demo’.
<pixeltable.catalog.dir.Dir at 0x16a80d480>

Create sample data

Created table ‘sales’.Inserting rows into `sales`: 0 rows [00:00, ? rows/s]
Inserting rows into `sales`: 6 rows [00:00, 609.56 rows/s]
Inserted 6 rows with 0 errors.

Variance UDA (not built-in)

String concatenation UDA

Collect values into a list

Weighted average UDA

Mode UDA (most frequent value)

Explanation

UDA structure:
Key points:
  • Always handle None values in update()
  • Multiple parameters in update() enable multi-column aggregations (like weighted_avg)
  • Return type annotation on value() determines output column type

See also

Last modified on June 24, 2026