Exemple #1
0
def test_caching_not_confused():

    df = tibble(x=[1, 2, 3])
    res = df >> mutate(
        # evaluating in Python space because the '+' operator is not supported
        # by numexpr for the bool dtype, use '|' instead
        any=if_any(f.x, lambda x: x >= 2) | if_any(f.x, lambda x: x >= 3),
        all=if_all(f.x, lambda x: x >= 2) | if_all(f.x, lambda x: x >= 3),
    )
    # dtypes not changed
    assert res["any"].eq([False, True, True]).all()
    assert res["all"].eq([False, True, True]).all()
Exemple #2
0
def test_if_any_all_enforce_bool():
    d = tibble(x=10, y=10)
    out = d >> filter(if_all(f[f.x:f.y], identity))
    assert_frame_equal(out, d)

    out = d >> filter(if_any(f[f.x:f.y], identity))
    assert_frame_equal(out, d)

    out = d >> mutate(ok=if_all(f[f.x:f.y], identity))
    assert_frame_equal(out, mutate(d, ok=True))

    out = d >> mutate(ok=if_any(f[f.x:f.y], identity))
    assert_frame_equal(out, mutate(d, ok=True))
Exemple #3
0
def test_if_any_if_all_single_arg():
    df = tibble(x=[True, False], y=[True, True])
    out = df >> filter(if_any(c(f.x, f.y)))
    assert_frame_equal(out, df)

    out = df >> filter(if_all(c(f.x, f.y)))
    assert_frame_equal(out, df.iloc[[0], :])
Exemple #4
0
def test_if_any_all_in_mutate():
    d = tibble(x=c(1, 5, 10, 10), y=c(0, 0, 0, 10), z=c(10, 5, 1, 10))
    res = d >> mutate(
        any=if_any(f[f.x:], lambda x: x > 8),
        all=if_all(f[f.x:f.any], lambda x: x > 8),
    )
    assert_iterable_equal(res["any"], [True, False, True, True])
    assert_iterable_equal(res["all"], [False, False, False, True])
Exemple #5
0
def test_works_with_if_any_if_all():
    df = tibble(x1=range(1, 11), x2=c(range(1, 6), 10, 9, 8, 7, 6))
    df1 = df >> filter(if_all(starts_with("x"), lambda x: x > 6))
    df2 = df >> filter((f.x1 > 6) & (f.x2 > 6))
    assert df1.equals(df2)

    df1 = df >> filter(if_any(starts_with("x"), lambda x: x > 6))
    df2 = df >> filter((f.x1 > 6) | (f.x2 > 6))
    assert df1.equals(df2)
Exemple #6
0
def test_if_any_all_na_handling():
    df = expandgrid(x=c(True, False, NA), y=c(True, False, NA))

    out = df >> filter(if_all(c(f.x, f.y), identity))
    expect = df >> filter(f.x & f.y)
    assert_frame_equal(out, expect)

    out = df >> filter(if_any(c(f.x, f.y), identity))
    expect = df >> filter(f.x | f.y)
    assert_frame_equal(out, expect)
Exemple #7
0
def test_if_any_if_all_no_args():
    out = tibble(x=1, y=0) >> mutate(if_any())
    assert_iterable_equal(out.iloc[:, 2], [True])

    out = tibble(x=1, y=0) >> mutate(if_all())
    assert_iterable_equal(out.iloc[:, 2], [False])