Exemple #1
0
def test_formula_number_columns():
    df = pd.DataFrame({'2017': [3, 2], '2018': [8, -1]})

    res = formula(df, new_column='evo', formula='2018 - 2017')
    assert res['evo'].tolist() == [1, 1]

    res = formula(df, new_column='evo', formula='"2018" - "2017"')
    assert res['evo'].tolist() == [5, -3]
def test_formula():
    df = pd.DataFrame({'a': [1, 3], 'b': [2, 4], 'other col': [3, 5],
                       'yet another1': [2, 2]})

    with pytest.raises(FormulaError) as exc_info:
        formula(df, new_column='c', formula='a, + b')
    assert str(exc_info.value) == '"a," is not valid. Allowed entries are numbers, ' \
                                  'column names and math symbols ' \
                                  '(among "(", ")", "+", "-", "/", "*", "%", ".")'

    with pytest.raises(FormulaError) as exc_info:
        formula(df, new_column='c', formula='import ipdb')
    assert str(exc_info.value) == '"import ipdb" is not a valid column name'

    res = formula(df, new_column='c', formula='a + b')
    assert res['c'].tolist() == [3, 7]

    res = formula(df, new_column='c', formula='.5*a - b')
    assert res['c'].tolist() == [-1.5, -2.5]

    res = formula(df, new_column='c', formula='a + other col')
    assert res['c'].tolist() == [4, 8]

    res = formula(df, new_column='c', formula='a + other col / 2')
    assert res['c'].tolist() == [2.5, 5.5]

    res = formula(df, new_column='c', formula='a + other col // 2')
    assert res['c'].tolist() == [2, 5]

    res = formula(df, new_column='c', formula='(a + other col)/  2.')
    assert res['c'].tolist() == [2, 4]

    res = formula(df, new_column='c', formula='(a + b ) % 3')
    assert res['c'].tolist() == [0, 1]
Exemple #3
0
def test_formula():
    df = pd.DataFrame({
        'a': [1, 3],
        'b': [2, 4],
        'other col': [3, 5],
        'yet_another': [2, 2],
        'and-another': [2, 2],
    })

    with pytest.raises(FormulaError) as exc_info:
        formula(df, new_column='c', formula='`a,` + `b`')
    assert str(exc_info.value) == '"a," is not a valid column name'

    with pytest.raises(FormulaError) as exc_info:
        formula(df, new_column='c', formula='`import ipdb`')
    assert str(exc_info.value) == '"import ipdb" is not a valid column name'

    res = formula(df, new_column='c', formula='`a` + `b`')
    assert res['c'].tolist() == [3, 7]

    res = formula(df, new_column='c', formula='.5*`a` - `b`')
    assert res['c'].tolist() == [-1.5, -2.5]

    res = formula(df, new_column='c', formula='`a` + `other col`')
    assert res['c'].tolist() == [4, 8]

    res = formula(df, new_column='c', formula='`a` + `other col` / 2')
    assert res['c'].tolist() == [2.5, 5.5]

    res = formula(df, new_column='c', formula='`a` + `other col` // 2')
    assert res['c'].tolist() == [2, 5]

    res = formula(df, new_column='c', formula='(`a` + `other col`)/  2.')
    assert res['c'].tolist() == [2, 4]

    res = formula(df, new_column='c', formula='(`a` + `b` ) % 3')
    assert res['c'].tolist() == [0, 1]

    res = formula(df, new_column='c', formula='`yet_another` + `b`')
    assert res['c'].tolist() == [4, 6]

    res = formula(df, new_column='c', formula='(`yet_another` + `b` ) % 3')
    assert res['c'].tolist() == [1, 0]

    with pytest.raises(FormulaError):
        formula(df, new_column='c', formula='`and`-`another` - `yet_another`')

    res = formula(df, new_column='c', formula='`and-another` - `yet_another`')
    assert res['c'].tolist() == [0, 0]
Exemple #4
0
def test_formula():
    df = pd.DataFrame({
        'a': [1, 3],
        'b': [2, 4],
        'other col': [3, 5],
        'yet_another': [2, 2],
        'and-another': [2, 2]
    })

    with pytest.raises(FormulaError) as exc_info:
        formula(df, new_column='c', formula='a, + b')
    assert str(exc_info.value) == '"a," is not a valid column name'

    with pytest.raises(FormulaError) as exc_info:
        formula(df, new_column='c', formula='import ipdb')
    assert str(exc_info.value) == '"import ipdb" is not a valid column name'

    res = formula(df, new_column='c', formula='a + b')
    assert res['c'].tolist() == [3, 7]

    res = formula(df, new_column='c', formula='.5*a - b')
    assert res['c'].tolist() == [-1.5, -2.5]

    res = formula(df, new_column='c', formula='a + other col')
    assert res['c'].tolist() == [4, 8]

    res = formula(df, new_column='c', formula='a + other col / 2')
    assert res['c'].tolist() == [2.5, 5.5]

    res = formula(df, new_column='c', formula='a + other col // 2')
    assert res['c'].tolist() == [2, 5]

    res = formula(df, new_column='c', formula='(a + other col)/  2.')
    assert res['c'].tolist() == [2, 4]

    res = formula(df, new_column='c', formula='(a + b ) % 3')
    assert res['c'].tolist() == [0, 1]

    res = formula(df, new_column='c', formula='yet_another + b')
    assert res['c'].tolist() == [4, 6]

    res = formula(df, new_column='c', formula='(yet_another + b ) % 3')
    assert res['c'].tolist() == [1, 0]

    with pytest.raises(FormulaError):
        formula(df, new_column='c', formula='and-another - yet_another')

    res = formula(df, new_column='c', formula='"and-another" - yet_another')
    assert res['c'].tolist() == [0, 0]
Exemple #5
0
def test_formula_old_syntax(mocker):
    df = pd.DataFrame({
        'a': [1, 3],
        'b': [2, 4],
        'other col': [3, 5],
        'yet_another': [2, 2],
        'and-another': [2, 2],
    })

    with pytest.raises(FormulaError) as exc_info:
        formula(df, new_column='c', formula='a, + b')
    assert str(exc_info.value) == '"a," is not a valid column name'

    with pytest.raises(FormulaError) as exc_info:
        formula(df, new_column='c', formula='import ipdb')
    assert str(exc_info.value) == '"import ipdb" is not a valid column name'

    log_warning = mocker.patch.object(LOGGER, 'warning')
    res = formula(df, new_column='c', formula='a + b')
    assert res['c'].tolist() == [3, 7]
    log_warning.assert_called_once_with(
        "DEPRECATED: You should always use ` for your columns. Old syntax: 'a + b', new syntax: '`a`+`b`'"
    )

    res = formula(df, new_column='c', formula='.5*a - b')
    assert res['c'].tolist() == [-1.5, -2.5]

    res = formula(df, new_column='c', formula='a + other col')
    assert res['c'].tolist() == [4, 8]

    res = formula(df, new_column='c', formula='a + other col / 2')
    assert res['c'].tolist() == [2.5, 5.5]

    res = formula(df, new_column='c', formula='a + other col // 2')
    assert res['c'].tolist() == [2, 5]

    res = formula(df, new_column='c', formula='(a + other col)/  2.')
    assert res['c'].tolist() == [2, 4]

    res = formula(df, new_column='c', formula='(a + b ) % 3')
    assert res['c'].tolist() == [0, 1]

    res = formula(df, new_column='c', formula='yet_another + b')
    assert res['c'].tolist() == [4, 6]

    res = formula(df, new_column='c', formula='(yet_another + b ) % 3')
    assert res['c'].tolist() == [1, 0]

    with pytest.raises(FormulaError):
        formula(df, new_column='c', formula='and-another - yet_another')

    res = formula(df, new_column='c', formula='"and-another" - yet_another')
    assert res['c'].tolist() == [0, 0]