module pixeltable.functions
General Pixeltable UDFs. This parent module contains general-purpose UDFs that apply to multiple data types.func filter()
Signature
filter() is used like a UDF, for example in select() or add_computed_column().
Parameters:
expr(pixeltable.exprs.expr.Expr): The array to filter; an expression of typepxt.Jsonthat resolves to a JSON array. Its element type is preserved in the result.predicate(typing.Callable[[pixeltable.exprs.expr.Expr], typing.Any]): A Python function (typically a lambda) that decides which elements to keep. It receivesx, a stand-in for a single array element, and returns a boolean condition; the element is kept when it is true. Operate onxexactly as you would on a column: comparisons, indexing (x[0]), field access (x.field), and JSON methods all work. Combine multiple conditions with&and|, and negate with~(not Pythonand/or/not). Example:lambda x: (x > 0) & (x < 10).
pixeltable.exprs.expr.Expr: A new array containing the elements ofexprfor whichpredicateis true, unchanged. Ifexprisnullor does not resolve to a JSON array, the result isnull.
tbl with a pxt.Json column data holding lists of numbers, add a column that keeps only
the positive numbers:
data holds lists of objects such as {'score': 0.9, 'label': 'cat'}, keep only the high-confidence
ones:
func map()
Signature
map() is used like a UDF, for example in select() or add_computed_column().
Parameters:
expr(pixeltable.exprs.expr.Expr): The array to map over; an expression of typepxt.Jsonthat resolves to a JSON array. If its elements have a known type (e.g. the column is declaredpxt.Json[[int]]), that type is available tofnand carries through to the result.fn(typing.Callable[[pixeltable.exprs.expr.Expr], typing.Any]): A Python function (typically a lambda) applied to each element to produce its replacement. It receivesx, a stand-in for a single array element, and returns the value to store in its place. Operate onxexactly as you would on a column: arithmetic, indexing (x[0]), field access (x.field), and JSON methods (x.len(),x.sum(), etc.) all work.
pixeltable.exprs.expr.Expr: A new array holdingfn(x)for each elementxofexpr. Ifexprisnullor does not resolve to a JSON array, the result isnull.
tbl with a pxt.Json column data holding lists of numbers, add a column that doubles
each number:
data holds lists of objects such as {'score': 0.9, 'label': 'cat'}, extract each score:
func sort()
Signature
sort() is used like a UDF, for example in select() or add_computed_column().
Parameters:
expr(pixeltable.exprs.expr.Expr): The array to sort; an expression of typepxt.Jsonthat resolves to a JSON array. Its element type is preserved in the result.key(typing.Optional[typing.Callable[[pixeltable.exprs.expr.Expr], typing.Any]]): An optional Python function (typically a lambda) that produces the value each element is ordered by. It receivesx, a stand-in for a single array element, and returns the sort key. Operate onxexactly as you would on a column: arithmetic, indexing (x[0]), field access (x.field), and JSON methods all work. Whenkeyis omitted, the elements are ordered by their own natural ordering.asc(bool, default:True): Whether to sort in ascending (the default) or descending order.
pixeltable.exprs.expr.Expr: A new array with the elements ofexprin sorted order. Ifexprisnullor does not resolve to a JSON array, the result isnull. Sorting a list of scalars without akey, or by non-orderable keys, raises if the values are not mutually comparable (matching Python’ssorted()).
tbl with a pxt.Json column data holding lists of numbers, add a column that sorts each
list in descending order:
data holds lists of objects such as {'score': 0.9, 'label': 'cat'}, sort each list by score:
uda count()
Signatures
val(String | None): The value to count.
pxt.Int: The count of non-null values.
value column of the table tbl:
category column and compute the count of non-null values in the value column
for each category, assigning the name 'category_count' to the new column:
uda max()
Signatures
val(String | None): The value to compare.
pxt.String | None: The maximum value, orNoneif there are no non-null values.
value column of the table tbl:
category column and compute the maximum value in the value column for each category,
assigning the name 'category_max' to the new column:
uda mean()
Signatures
val(Int | None): The numeric value to include in the mean.
pxt.Float | None: The mean of the non-null values, orNoneif there are no non-null values.
value column of the table tbl:
category column and compute the mean of the value column for each category,
assigning the name 'category_mean' to the new column:
uda min()
Signatures
val(String | None): The value to compare.
pxt.String | None: The minimum value, orNoneif there are no non-null values.
value column of the table tbl:
category column and compute the minimum value in the value column for each category,
assigning the name 'category_min' to the new column:
uda sum()
Signatures
val(Int | None): The numeric value to add to the sum.
pxt.Int | None: The sum of the non-null values, orNoneif there are no non-null values.
value column of the table tbl:
category column and compute the sum of the value column for each category,
assigning the name 'category_total' to the new column: