udf() accepts several arguments as input so you can tune a Python function and notify teradataml when to use the function.
| Argument | Required/Optional | Type | Description |
|---|---|---|---|
| returns | Optional | teradatasqlalchemy types object | Specifies the output column type. When not specified, default is VARCHAR(1024). |
| env_name | Optional | string or object of class UserEnv | (Applicable for use with Apply table operator) Specifies the name of the remote user environment or an object of class UserEnv for VantageCloud Lake. |
| delimiter | Optional | One-character string | Specifies a delimiter to use when reading columns from a row and writing result columns. Default is comma (,). If data being processed contains a comma, use a different delimiter. |
| quotechar | Optional | One-character string | Specifies a character that forces input of the user function to be quoted using this specified character. |
| debug | Optional | bool | Specifies whether to remove the temporary script generated during execution and display the file path or not. This argument is useful for debugging if there are any failures when executing the function. When set to True, function displays the path of the script and does not remove the file from local file system. Otherwise, file is removed from the local file system. |
Example: Create the udf to analyze the sentiment for the data in column 'review' using VADER Sentiment Analysis tool
Python package vaderSentiment should be installed in the user environment in VantageCloud Lake.
Example setup - Create a teradataml DataFrame
>>> import pandas as pd
>>> from teradataml import copy_to_sql, DataFrame
>>> pdf = pd.DataFrame({'review': ["study is going on as usual", "I am very sad today.", "great job. big display , good backlight"]})
>>> copy_to_sql(df = pdf, table_name = "review_data", if_exists="replace")
>>> df = DataFrame("review_data")
>>> df
review 0 great job. big display,good backlight 1 I am very sad today. 2 study is going on as usual
Convert a Python function to udf using @udf as decorator and pass the arguments to it
>>> from teradatasqlalchemy.types import VARCHAR
>>> from teradataml.dataframe.functions import udf
>>> @udf(returns = VARCHAR(80), env_name = env, delimiter='|')
... def sentiment_analysis(txt):
... from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
... # Create a SentimentIntensityAnalyzer object.
... sid_obj = SentimentIntensityAnalyzer()
...
... # polarity_scores method of SentimentIntensityAnalyzer
... # object gives a sentiment dictionary.
... # which contains pos, neg, neu, and compound scores.
... sentiment_dict = sid_obj.polarity_scores(txt)
...
... if sentiment_dict['compound'] >= 0.05 :
... sentiment = "Positive"
... elif sentiment_dict['compound'] <= - 0.05 :
... sentiment = "Negative"
... else :
... sentiment = "Neutral"
... return sentiment
>>> res = df1.assign(sentiment = sentiment_analysis('review'))
>>> res
review sentiment 0 great job. big display,good backlight Positive 1 study is going on as usual Neutral 2 I am very sad today. Negative >>>