Python Operator Reference
Learn about the Python support offered by the Python operator in a transformation flow in a file space.
Support for Python Standard Libraries
-
time
-
dateutil.parser
-
datetime
-
calendar
-
random
-
math
-
re
Basic Examples for Standard Libraries
Example 1
-
street_number [Integer]: extracts the digits from the address column (string type) in the source table.
-
bonus_rounded [Integer]: rounds off the column bonus (float type) to nearest integer.
-
service_years [Integer]: calculates the difference between the current year and date_of_joining (date type).
| first | last | address | date_of_joining | bonus |
|---|---|---|---|---|
|
John |
Doe |
123 St, Rock Valley |
Feb 26, 2022 |
67.5 |
|
Jane |
Doe |
456 St, It Valley |
Dec 9, 2005 |
90.75 |
| first | last | address | date_of_joining | bonus | service_year | bonus_rounded | street_number |
|---|---|---|---|---|---|---|---|
|
John |
Doe |
123 St, Rock Valley |
Feb 26, 2022 |
6700 |
3 |
68 |
123 |
|
Jane |
Doe |
456 St, It Valley |
Dec 9, 2005 |
9000 |
20 |
91 |
456 |
Example 2
This code snippet expects a column date_of_joining of type string which has a date in string format. It uses the calendar library and fetches the day_name of the week by parsing it using the dateutil.parser library, and stores in the new column day_of_the_week.
The current timestamp is fetched with the help of the timestamp column and stored in string format using the time library and stored in the new column as current_timestamp.
The employee rating is assigned randomly in the range of 1 to 5 using the random library and stored in the new column employee_rating.
| first | last | address | date_of_joining |
|---|---|---|---|
|
John |
Doe |
123 St, Rock Valley |
24-09-1999 |
|
Jane |
Doe |
456 St, It Valley |
23-10-2005 |
| first | last | address | date_of_joining | current_timestamp | day_of_the_week | employee_rating |
|---|---|---|---|---|---|---|
|
John |
Doe |
123 St, Rock Valley |
24-09-1999 |
2025-03-11 07:58:19 |
Friday |
4 |
|
Jane |
Doe |
456 St, It Valley |
23-10-2005 |
2025-03-11 07:58:19 |
Sunday |
2 |
Support for builtins, Numpy, and Pandas Modules
|
Builtin Module (version 3.11) |
NumPy Module (version 1.26.4) |
Pandas Module (version 2.2.2) |
|---|---|---|
|
All constants and functions in the Python builtins module are supported except the following:
|
All objects and functions in the NumPy module are supported (using the standard np alias) except the following:
|
All objects and functions in the Pandas module are supported (using the standard pd alias) except the following:
|
Support for builtins Exception Types
-
ArithmeticError
-
AssertionError
-
AttributeError
-
BufferError
-
FloatingPointError
-
IndexError
-
KeyError
-
LookupError
-
NameError
-
NotImplementedError
-
OverflowError
-
RuntimeError
-
StopIteration
-
TypeError
-
UnboundLocalError
-
UnicodeDecodeError
-
UnicodeEncodeError
-
UnicodeError
-
UnicodeTranslateError
-
ValueError
-
ZeroDivisionError
Basic Examples for Exception Types
Type of incoming data is restricted to Pandas DataFrame.
Example 1
This script creates a projection from an incoming DataFrame. It assumes that there would be at least these three columns in the incoming DataFrame: first, customer_name, and office_location. That is an alternative way to say that the source table has at least these three attributes. The projection script then extracts only these three columns and returns them as a DataFrame.
| first | last | birthday | customer_name | office_location |
|---|---|---|---|---|
|
John |
Doe |
2002-04-09 12:33:58 |
RedBull |
Walldorf |
|
Jane |
Doe |
2005-03-03 12:33:58 |
GoPro |
São Leopoldo |
| first | customer_name | office_location |
|---|---|---|
|
John |
RedBull |
Walldorf |
|
Jane |
GoPro |
São Leopoldo |
It expects that the target table has only these three attributes, first, customer_name, and office_location. If the target table schema is different in any ways from this, the transformation flow execution fails.
Example 2
This script extracts the 'first' and 'last' columns of the incoming DataFrame, assuming that they exist in the source table, and stores these two columns as a separate DataFrame in the local variable 'df'. It then computes a third column email from the extracted columns, updates the df, and returns it.
| first | last |
|---|---|
|
John |
Doe |
|
Jane |
Doe |
| first | last | full |
|---|---|---|
|
John |
Doe |
John.Doe |
|
Jane |
Doe |
Jane.Doe |
The transformation flow would fail if the source table has neither the first nor the last attribute, among others. Similarly, it would also fail if the target table schema does not consist of just these three attributes, first, last, and full.
Example 3
This script assumes that there is a birthday column among others, with the string values in the incoming DataFrame. It first attempts to convert the string values of this column to pandas Timestamp and store as a Series. Finally, it makes a specific string representation of the birth days in the Series, using the methods and attributes from the Timestamp class, and updates the same in the original DataFrame, before returning the same.
| first | last | birthday |
|---|---|---|
|
John |
Doe |
2002-04-09 12:33:58 |
|
Jane |
Doe |
2005-03-03 12:33:58 |
| first | last | birthday |
|---|---|---|
|
John |
Doe |
April 9, 2002, Tuesday |
|
Jane |
Doe |
March 3, 2005, Thursday |
In this case, it assumes that the source and target tables have the same schema and data types.
Handling Decimal Types
To work with decimal types in the script operator, the built-in Python Decimal class should be used. There is no way or need to import it, as it is available for the user inside the body of the transform function. Few examples of using the Decimal type in the script operator are provided below.
Basic Examples for Decimal Types
Example 1
This script assumes that the source table has a column name dept_no and dept_name and it wants to add decimal type arbitrary column start_date_utc_long to it. The actual data type precision of this column is assumed to be decimal(21,0) in the target table definition.
| dept_no | dept_name |
|---|---|
|
dept_101 |
Audit |
|
dept_102 |
Advisory |
| dept_no | dept_name | start_date_utc_long |
|---|---|---|
|
dept_101 |
Audit |
Decimal("123456789012345678901") |
|
dept_102 |
Advisory |
Decimal("123456789012345678901") |
In this case, it assumes that the source and target tables have the same schema and data types.
Example 2
This script assumes that the source table has a column name salary and this column is of data type Decimal. The script applies a filter on the column salary comparing the value of its contents to a python Decimal object and returns the filtered table.
| first | last | salary |
|---|---|---|
|
John |
Doe |
Decimal("1099.99") |
|
Jane |
Doe |
Decimal("1599.50") |
| first | last | salary |
|---|---|---|
|
Jane |
Doe |
Decimal("1599.50") |
The example assumes that the source and target tables have the same schema and data types.