Scalar UDFs

Scalar user-defined function APIs

scalar

scalar()

Scalar user-defined functions.

The scalar class itself is not a public API, its methods are.

Methods

Name Description
pandas Construct a vectorized scalar user-defined function that accepts pandas Series’ as inputs.
pyarrow Construct a vectorized scalar user-defined function that accepts PyArrow Arrays as input.
python Construct a non-vectorized scalar user-defined function that accepts Python scalar values as inputs.

pandas

pandas(cls, fn=None, *args, name=None, schema=None, **kwargs)

Construct a vectorized scalar user-defined function that accepts pandas Series’ as inputs.

Parameters

Name Type Description Default
fn Callable | None The The function to wrap. None
args Any Configuration arguments for the UDF. ()
name str | None The name of the UDF in the backend if different from the function name. None
schema str | None The schema in which to create the UDF. None
kwargs Any Additional configuration arguments for the UDF. {}

Examples

>>> import ibis
>>> @ibis.udf.scalar.pandas
... def add_one(x: int) -> int:
...     return x + 1
>>> expr = add_one(2)
>>> con = ibis.connect(os.environ["SNOWFLAKE_URL"])  # doctest: +SKIP
>>> con.execute(expr)  # doctest: +SKIP
3

See Also

pyarrow

pyarrow(cls, fn=None, *args, name=None, schema=None, **kwargs)

Construct a vectorized scalar user-defined function that accepts PyArrow Arrays as input.

Parameters

Name Type Description Default
fn Callable | None The The function to wrap. None
args Any Configuration arguments for the UDF. ()
name str | None The name of the UDF in the backend if different from the function name. None
schema str | None The schema in which to create the UDF. None
kwargs Any Additional configuration arguments for the UDF. {}

Examples

>>> import ibis
>>> import pyarrow.compute as pc
>>> @ibis.udf.scalar.pyarrow
... def add_one(x: int) -> int:
...     return pc.add(x, 1)
>>> expr = add_one(2)
>>> con = ibis.connect("duckdb://")
>>> con.execute(expr)
3

See Also

python

python(cls, fn=None, *args, name=None, schema=None, **kwargs)

Construct a non-vectorized scalar user-defined function that accepts Python scalar values as inputs.

python UDFs are not vectorized: they are executed row by row with one Python function call per row

This calling pattern tends to be much slower than pandas or pyarrow-based vectorized UDFs.

Parameters

Name Type Description Default
fn Callable | None The The function to wrap. None
args Any Configuration arguments for the UDF. ()
name str | None The name of the UDF in the backend if different from the function name. None
schema str | None The schema in which to create the UDF. None
kwargs Any Additional configuration arguments for the UDF. {}

Examples

>>> import ibis
>>> @ibis.udf.scalar.python
... def add_one(x: int) -> int:
...     return x + 1
>>> expr = add_one(2)
>>> con = ibis.connect("duckdb://")
>>> con.execute(expr)
3

See Also

Back to top