Using expressions

For your calculations in Analytics Service, you can type or paste a valid Pandas expression as input to a function. You must understand how to form the expression before you add it.

When to use expressions

Typically, if the calculation you define involves a single line of code you can use an expression. Instead of writing your own custom function to perform the calculation, you can type or paste your expression into an input field of a function on the UI.

Python expressions are used in some functions. For example, the PythonExpression built-in function uses expressions. An expression is formed using existing data items. The PythonExpression function creates a new data item (KPI).

Writing expressions

Calculations in Analytics Service use the Pandas library (see Working with calculations). Before you write expressions for Analytics Service, familiarize yourself with data structures, operations, and syntax in Pandas.

When you enter an expression on the UI, you are not writing a new Python function. Instead, the expression is an input parameter to an existing Python function. You must tailor the syntax of your expression accordingly.

Example

The operations manager in company Acme wants to calculate the distance traveled by a newly developed robotic arm, robot A. The data scientist defines distance as:

distance = speed * travel_time

The calculation uses two of the data items from robot A; speed and travel_time.

An analyst writes the following Python expression for the calculation:

df['distance'] = df['speed']*df['travel_time']
print(df)

To paste the expression into an input field in PythonExpression on the UI, the analyst removes df['distance'] =.

The analyst enters:

df['speed']*df['travel_time']

The analyst labels the output parameter as distance.

Testing expressions

Test your expression in your own environment before you use them in Analytics Service .

You do not have to connect to Analytics Service or install IoT Functions package to test the expression.

Write a Python script to create some samples data and evaluate the expression against the data. In your script, you must add a function to call the expression, call the expression, and display the output.

See Tutorial: Creating expressions for a step-by-step example of how to test an expression.

Tip: You can use the same test script to test any of your expressions. To make your test results more meaningful, you can update the data item names or add new data items to the code that generates the random data values to suit your expressions.

More information