timeflux.nodes.expression


expression

class timeflux.nodes.expression.Expression(expr, eval_on, **kwargs)[source]

Bases: timeflux.core.node.Node

Evaluate a Python expression as a string.

This nodes uses eval method from pandas to evaluate a Python expression as a string on the data. The expression can be evaluated either on the input ports (eval_on = ports) or on the input columns (eval_on = columns).

The following arithmetic operations are supported: +, -, , /, *, %, // (python engine only) along with the following boolean operations: | (or), & (and), and ~ (not).

Variables:
  • i (Port) – default data input, expects DataFrame.

  • i_* (Port, optional) – data inputs when eval_on is ports.

  • o (Port) – default output, provides DataFrame.

Parameters:
  • expr (str) – Expression of the function to apply to each column or row.

  • eval_on (columns | ports) – Variable on which the expression is evaluated. Default: ports If columns, the variables passed to the expression are the columns of the data in default input port. If ports, the variables passed to the expression are the data of the input ports.

  • kwargs – Additional keyword arguments to pass as keywords arguments to pandas.eval: {‘parser’: ‘pandas’, ‘engine’: None, ‘resolvers’: None,’level’: None, ‘target’: None }

Example

In this example, we eval arithmetic expression on the input ports : o = i_1 + i_2. Hence, the variables on which the expression is applied are the data of the ports.

  • expr = i_1 + i_2

  • eval_on = ports

The nodes expects two data inputs :

On port i_1:

                      0  1
2018-01-01 00:00:00   5  8
2018-01-01 00:00:01   9  5
2018-01-01 00:00:02  10  4
2018-01-01 00:00:03   5  5

On port i_2:

                      0  1
2018-01-01 00:00:00   1  3
2018-01-01 00:00:01   1  5
2018-01-01 00:00:02  10  1
2018-01-01 00:00:03   2  1

It returns one data output that is i_1.data + i_2.data :

On port o:

                      0   1
2018-01-01 00:00:00   6  11
2018-01-01 00:00:01  10  10
2018-01-01 00:00:02  20   5
2018-01-01 00:00:03   7   6

Example

In this example, we eval an arithmetic expression on columns : col3 = col2 + col1 Hence, the variables on which the expression is applied are the columns of the data from default input port. We set:

  • expr = col2 = col1 + col0

  • eval_on = columns

The node expects data with columns col0 and col1 on the default port i:

                     col0  col1
2018-01-01 00:00:00     5     8
2018-01-01 00:00:01     9     5

It returns data with an appended column col2 on port o:

                     col0  col1  col2
2018-01-01 00:00:00     5     8    13
2018-01-01 00:00:01     9     5    14

References

See the documentation of pandas.eval .

Instantiate the node.

update()[source]

Update the input and output ports.