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

The following standard python libraries are supported:
  • time

  • dateutil.parser

  • datetime

  • calendar

  • random

  • math

  • re

These libraries don’t have any alias and can be used directly.

Basic Examples for Standard Libraries

Example 1

This code snippet creates three new columns:
  • 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).

Source Table
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

Taget Table
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.

Source Table
first last address date_of_joining

John

Doe

123 St, Rock Valley

24-09-1999

Jane

Doe

456 St, It Valley

23-10-2005

Target Table
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

The Python operator offers support for data manipulation and vector operations via support for the builtins (version 3.11), NumPy (version 1.26.4), and Pandas (version 2.2.2) modules. In order to achieve a safe execution environment, some restrictions are applied.

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:

  • eval

  • exec

  • compile

  • classmethod

  • staticmethod

  • breakpoint

  • memoryview

  • __import__() i.e. import statement

  • input

  • open

  • dir

  • object

  • print

  • id

  • vars

  • hasattr

  • getattr and __getattribute__

  • setattr and __setattr__

  • delattr and __delattr__

All objects and functions in the NumPy module are supported (using the standard np alias) except the following:
  • load

  • save

  • savez

  • savez_compressed

  • loadtxt

  • savetxt

  • genfromtxt

  • fromregex

  • fromfile

  • memmap

  • DataSource

  • ndarray.tofile

  • ndarray.dump

All objects and functions in the Pandas module are supported (using the standard pd alias) except the following:
  • read_pickle

  • read_table

  • read_csv

  • read_fwf

  • read_clipboard

  • read_excel

  • read_json

  • read_html

  • read_hdf

  • read_feather

  • read_parquet

  • read_sas

  • read_spss

  • read_gbq

  • read_stata

  • read_sql_table

  • read_sql_query

  • io.excel.ExcelFile

  • io.excel.ExcelWriter

  • io.stata.StataReader

  • io.sql.SQLDatabase

  • io.sql.SQLiteDatabase

  • io.pytables.HDFStore

  • DataFrame.to_pickle

  • DataFrame.to_csv

  • DataFrame.to_hdf

  • DataFrame.to_sql

  • DataFrame.to_excel

  • DataFrame.to_html

  • DataFrame.to_feather

  • DataFrame.to_stata

  • DataFrame.to_gbq

  • DataFrame.to_clipboard

  • DataFrame.to_json

  • DataFrame.to_latex

  • DataFrame.to_markdown

  • Series.to_pickle

  • Series.to_csv

  • Series.to_excel

  • Series.to_hdf

  • Series.to_sql

  • Series.to_clipboard

  • Series.to_json

  • Series.to_latex

  • Series.to_markdown

Support for builtins Exception Types

The following builtin exception types are supported for use in error handling.
  • 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.

Source table (Example DataFrame stored locally in the script as data):
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

Target table (Example returning DataFrame projection):
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.

Source table (Example DataFrame stored locally in the script as df):
first last

John

Doe

Jane

Doe

Output table (Example returning DataFrame df):
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.

Source table (Example of incoming DataFrame as data):
first last birthday

John

Doe

2002-04-09 12:33:58

Jane

Doe

2005-03-03 12:33:58

Target table (Example returning DataFrame as data):
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.

Source table (Example of incoming DataFrame as data):
dept_no dept_name

dept_101

Audit

dept_102

Advisory

Target table (Example returning DataFrame as data):
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.

Source table (Example of incoming DataFrame as data):
first last salary

John

Doe

Decimal("1099.99")

Jane

Doe

Decimal("1599.50")

Target table (Example returning DataFrame as data):
first last salary

Jane

Doe

Decimal("1599.50")

The example assumes that the source and target tables have the same schema and data types.