Examples: How to Use teradataml UDF Functions | Teradata Package for Python - Examples: How to Use teradataml UDF Functions - Teradata Package for Python

Teradata® Package for Python User Guide

Deployment
VantageCloud
VantageCore
Edition
Enterprise
IntelliFlex
VMware
Product
Teradata Package for Python
Release Number
20.00
Published
December 2024
ft:locale
en-US
ft:lastEdition
2025-01-23
dita:mapPath
nvi1706202040305.ditamap
dita:ditavalPath
plt1683835213376.ditaval
dita:id
rkb1531260709148
Product Category
Teradata Vantage

Example 1: Register a UDF in Analytics Database

Register a Python function with name “upper” to get the values upper case.

>>> from teradataml.dataframe.functions import register
>>> def to_upper(s):
...     return s.upper()
>>>
# Register the created Python function with name "upper".
>>> register("upper", to_upper)
>>>

Register a teradataml udf with name “fact” to get factorial of a number and store the result in Integer type column.

>>> from teradataml.dataframe.functions import udf
>>> from teradatasqlalchemy.types import INTEGER
>>> @udf(returns = INTEGER())
... def factorial(n):
...    import math
...    return math.factorial(n)
>>>
# Register the created user defined function with name "fact".
>>> register("fact", factorial)
>>>

Example 2: List all UDFs registered in Analytics Database

>> from teradataml.dataframe.functions import list_udfs
>>> list_udfs(True)
id      name  return_type                                          file_name
0      upper  VARCHAR1024  tdml_udf_name_upper_udf_type_VARCHAR1024_register.py
1       fact      INTEGER       tdml_udf_name_fact_udf_type_INTEGER_register.py
2   add_date         DATE      tdml_udf_name_add_date_udf_type_DATE_register.py
>>>

Example 3: Execute a registered UDF

Execute the UDF registered with name 'upper'.

>>> from teradataml.dataframe.functions import call_udf
# Call the Python function registered with name "upper" and assign the
# ColumnExpression returned to the DataFrame.
>>> res = df.assign(upper_col = call_udf("upper", ('accounts',)))
>>> res
              Feb    Jan    Mar    Apr  datetime  id   upper_col
accounts
Yellow Inc   90.0    NaN    NaN    NaN  17/01/04   4  YELLOW INC
Alpha Co    210.0  200.0  215.0  250.0  17/01/04   2    ALPHA CO
Jones LLC   200.0  150.0  140.0  180.0  17/01/04   5   JONES LLC
Red Inc     200.0  150.0  140.0    NaN  17/01/04   3     RED INC
Blue Inc     90.0   50.0   95.0  101.0  17/01/04   1    BLUE INC
Orange Inc  210.0    NaN    NaN  250.0  17/01/04   4  ORANGE INC
>>>

Execute the UDF registered with name 'fact'.

# Call the user defined function registered with name "fact" and assign the
# ColumnExpression returned to the DataFrame.
>>> res = df.assign(fact_col = call_udf("fact", ('id',)))
>>> res
              Feb    Jan    Mar    Apr  datetime  id  fact_col
accounts
Jones LLC   200.0  150.0  140.0  180.0  17/01/04   5       120
Yellow Inc   90.0    NaN    NaN    NaN  17/01/04   4        24
Red Inc     200.0  150.0  140.0    NaN  17/01/04   3         6
Blue Inc     90.0   50.0   95.0  101.0  17/01/04   1         1
Alpha Co    210.0  200.0  215.0  250.0  17/01/04   2         2
Orange Inc  210.0    NaN    NaN  250.0  17/01/04   4        24

Example 4: Delete registered UDFs from Analytics Database

>>> from teradataml.dataframe.functions import deregister, list_udfs
>>> from teradatasqlalchemy.types import INTEGER
>>> deregister("upper")
>>> deregister("fact", returns=INTEGER()
>>>
>>> list_udfs(True)
id      name  return_type                                         file_name
 0  add_date         DATE     tdml_udf_name_add_date_udf_type_DATE_register.py