toucan_data_sdk/utils/postprocess/math.py
def formula(df, *, new_column: str, formula: str):
    """
    Do mathematic operations on columns (add, subtract, multiply or divide)

    ---

    ### Parameters

    *mandatory:*
    - `new_column` (*str*): name of the output column
    - `formula` (*str*): Operation on column. Use name of column and special character:
        - `+` for addition
        - `-` for subtraction
        - `*` for multiplication
        - `/` for division

    **Note:**
    - your column name can contain spaces.
    - if your column name is a number, you must use a quote mark : `"` or `'` (cf. example)

    ---

    ### Examples

    **Input**

    | variable | valueA | valueB | My rate |
    |:--------:|:--------:|:-----:|:------:|
    |   toto   |    20    |  100  |   10   |
    |   toto   |    30    |  200  |   10   |
    |   toto   |    10    |  300  |   10   |


    ```cson
    formula:
      new_column: 'valueD'
      formula: '(valueB + valueA ) / My rate'
    ```

    **Output**

    | variable | valueA   | valueB |  My rate |  valueD |
    |:--------:|:--------:|:------:|:-------:|:-------:|
    |   toto   |    20    |   100  |    10   |     12  |
    |   toto   |    30    |   200  |    10   |     23  |
    |   toto   |    10    |   300  |    10   |     31  |

    ---

    **Input**

    | variable | 2018 | 2019 |
    |:--------:|:--------:|:-----:|
    |   toto   |    20    |  100  |
    |   toto   |    30    |  200  |
    |   toto   |    10    |  300  |

    ```cson
    formula:
      new_column: 'Evolution'
      formula: "'2019' - '2018'"
    ```

    **Output**

    | variable | 2018 | 2019 | Evolution |
    |:--------:|:--------:|:-----:|:-----:|
    |   toto   |    20    |  100  | 80 |
    |   toto   |    30    |  200  | 170 |
    |   toto   |    10    |  300  | 290 |

    """
    tokens = _parse_formula(formula)
    expression_splitted = []
    for t in tokens:
        # To use a column name with only digits, it has to be quoted!
        # Otherwise it is considered as a regular number
        if not t.quoted and (t in MATH_CHARACTERS or is_float(t)):
            expression_splitted.append(t)
        elif t in df.columns:
            expression_splitted.append(f'df["{t}"]')
        else:
            raise FormulaError(f'"{t}" is not a valid column name')
    expression = ''.join(expression_splitted)
    df[new_column] = eval(expression)
    return df
def formula(df, *, new_column: str, formula: str):
    """
    Do mathematic operations on columns (add, subtract, multiply or divide)

    ---

    ### Parameters

    *mandatory:*
    - `new_column` (*str*): name of the output column
    - `formula` (*str*): Operation on column. Use name of column and special character:
        - `+` for addition
        - `-` for subtraction
        - `*` for multiplication
        - `/` for division

    **Note:**
    - your column name can contain spaces.
    - if your column name is a number, you must use a quote mark : `"` or `'` (cf. example)

    ---

    ### Examples

    **Input**

    | variable | valueA | valueB | My rate |
    |:--------:|:--------:|:-----:|:------:|
    |   toto   |    20    |  100  |   10   |
    |   toto   |    30    |  200  |   10   |
    |   toto   |    10    |  300  |   10   |


    ```cson
    formula:
      new_column: 'valueD'
      formula: '(valueB + valueA ) / My rate'
    ```

    **Output**

    | variable | valueA   | valueB |  My rate |  valueD |
    |:--------:|:--------:|:------:|:-------:|:-------:|
    |   toto   |    20    |   100  |    10   |     12  |
    |   toto   |    30    |   200  |    10   |     23  |
    |   toto   |    10    |   300  |    10   |     31  |

    ---

    **Input**

    | variable | 2018 | 2019 |
    |:--------:|:--------:|:-----:|
    |   toto   |    20    |  100  |
    |   toto   |    30    |  200  |
    |   toto   |    10    |  300  |

    ```cson
    formula:
      new_column: 'Evolution'
      formula: "'2019' - '2018'"
    ```

    **Output**

    | variable | 2018 | 2019 | Evolution |
    |:--------:|:--------:|:-----:|:-----:|
    |   toto   |    20    |  100  | 80 |
    |   toto   |    30    |  200  | 170 |
    |   toto   |    10    |  300  | 290 |

    """
    tokens = _parse_formula(formula)
    expression_splitted = []
    for t in tokens:
        # To use a column name with only digits, it has to be quoted!
        # Otherwise it is considered as a regular number
        if not t.quoted and (t in MATH_CHARACTERS or is_float(t)):
            expression_splitted.append(t)
        elif t in df.columns:
            expression_splitted.append(f'df["{t}"]')
        else:
            raise FormulaError(f'"{t}" is not a valid column name')
    expression = ''.join(expression_splitted)
    df[new_column] = eval(expression)
    return df
def formula ( df , * , new_column : str , formula : str ) : tokens = _parse_formula ( formula ) expression_splitted = [ ] for t in tokens : # To use a column name with only digits, it has to be quoted! # Otherwise it is considered as a regular number if not t . quoted and ( t in MATH_CHARACTERS or is_float ( t ) ) : expression_splitted . append ( t ) elif t in df . columns : expression_splitted . append ( f'df["{t}"]' ) else : raise FormulaError ( f'"{t}" is not a valid column name' ) expression = '' . join ( expression_splitted ) df [ new_column ] = eval ( expression ) return df
Do mathematic operations on columns (add, subtract, multiply or divide)

    ---

    ### Parameters

    *mandatory:*
    - `new_column` (*str*): name of the output column
    - `formula` (*str*): Operation on column. Use name of column and special character:
        - `+` for addition
        - `-` for subtraction
        - `*` for multiplication
        - `/` for division

    **Note:**
    - your column name can contain spaces.
    - if your column name is a number, you must use a quote mark : `"` or `'` (cf. example)

    ---

    ### Examples

    **Input**

    | variable | valueA | valueB | My rate |
    |:--------:|:--------:|:-----:|:------:|
    |   toto   |    20    |  100  |   10   |
    |   toto   |    30    |  200  |   10   |
    |   toto   |    10    |  300  |   10   |


    ```cson
    formula:
      new_column: 'valueD'
      formula: '(valueB + valueA ) / My rate'
    ```

    **Output**

    | variable | valueA   | valueB |  My rate |  valueD |
    |:--------:|:--------:|:------:|:-------:|:-------:|
    |   toto   |    20    |   100  |    10   |     12  |
    |   toto   |    30    |   200  |    10   |     23  |
    |   toto   |    10    |   300  |    10   |     31  |

    ---

    **Input**

    | variable | 2018 | 2019 |
    |:--------:|:--------:|:-----:|
    |   toto   |    20    |  100  |
    |   toto   |    30    |  200  |
    |   toto   |    10    |  300  |

    ```cson
    formula:
      new_column: 'Evolution'
      formula: "'2019' - '2018'"
    ```

    **Output**

    | variable | 2018 | 2019 | Evolution |
    |:--------:|:--------:|:-----:|:-----:|
    |   toto   |    20    |  100  | 80 |
    |   toto   |    30    |  200  | 170 |
    |   toto   |    10    |  300  | 290 |
Do mathematic operations on columns ( add subtract multiply or divide )