Skip to content

Calculate

math

Evaluates a formula containing basic arithmetic over a dataset's columns.

For example, to multiply column A by two and add column Col B, you would simply write

calculate(ds[["A", "Col B"]], {
    "formula": "2 * A - `Col B`"
}) -> (ds.result)

For more details regarding valids operators etc. see the Pandas eval() documentation, more specifically the supported syntax, and eval() applied to DataFrames.

Note that assignments in the formula are not supported, since the result must always be a new column, i.e. the following kind of formula should be avoided: "c = a + b". The correct way to return the result as a column would simply be "a + b".

If the name of an input column contains spaces, such as in the example above, it should be quoted in single backticks.

Usage


The following are the step's expected inputs and outputs and their specific types.

Step signature
calculate(ds: dataset, {
    "param": value
}) -> (result: number)

where the object {"param": value} is optional in most cases and if present may contain any of the parameters described in the corresponding section below.

Example

Assuming a dataset ds containing the numeric columns num_a, num_b and num_c, and a constant a with value 1.3, the following formula transforms each column numerically before adding and multiplying them together, all in one step:

Example call (in recipe editor)
calculate(ds, {
    "formula": "log(num_a) + num_b**3 * (num_c + @a)",
    "constants": {"a": 1.3}
}) -> (ds.result)

Inputs


ds: dataset

A dataset with columns to be used in the evaluation of the formula. Note, all columns mentioned in the formula must be numeric!

Outputs


result: column:number

A numeric column containing the result of evaluating the formula.

Parameters


formula: string

The formula to execute. A formula containing basic arithmetic operations and references to column names.


constants: object

Constants to be used in the formula. An object of key-value pairs, where keys refer to names of constants whose values will be available in the formula with a @ prefix. E.g. "constants": {"factor": 1.23} makes it possible to refer to @factor in the formula with given value; also see example(s).

Items in constants

*param: number | string

One or more constants. Note that all constant must be of allowed types (number or string).