示例#1
0
def test_slice_any_checks_for_constant_n_and_prop():
    df = tibble(x=range(1, 11))
    with pytest.raises(TypeError):
        slice_head(df, n=f.x)  # ok with n()
    with pytest.raises(TypeError):
        slice_head(df, prop=f.x)

    with pytest.raises(TypeError):
        slice_tail(df, n=f.x)
    with pytest.raises(TypeError):
        slice_tail(df, prop=f.x)

    with pytest.raises(TypeError):
        slice_min(df, f.x, n=f.x)
    with pytest.raises(TypeError):
        slice_min(df, f.x, prop=f.x)

    with pytest.raises(TypeError):
        slice_max(df, f.x, n=f.x)
    with pytest.raises(TypeError):
        slice_max(df, f.x, prop=f.x)

    with pytest.raises(TypeError):
        slice_sample(df, n=f.x)
    with pytest.raises(TypeError):
        slice_sample(df, prop=f.x)
示例#2
0
def test_min_and_max_ignore_nas():
    df = tibble(id=range(1, 5), x=c(2, NA, 1, 2), y=[NA] * 4)
    out = df >> slice_min(f.x, n=2)
    assert out.id.tolist() == [3, 1, 4]
    out = df >> slice_min(f.y, n=2) >> nrow()
    assert out == 0
    out = df >> slice_max(f.x, n=2)
    assert out.id.tolist() == [1, 4]
    out = df >> slice_max(f.y, n=2) >> nrow()
    assert out == 0
示例#3
0
def test_min_and_max_reorder_results():
    df = tibble(id=range(1, 5), x=c(2, 3, 1, 2))
    out = df >> slice_min(f.x, n=2)
    assert out.id.tolist() == [3, 1, 4]
    out = df >> slice_min(f.x, n=2, with_ties=False)
    assert out.id.tolist() == [3, 1]
    out = df >> slice_max(f.x, n=2)
    assert out.id.tolist() == [2, 1, 4]
    out = df >> slice_max(f.x, n=2, with_ties=False)
    assert out.id.tolist() == [2, 1]
示例#4
0
def test_min_and_max_return_ties_by_default():
    df = tibble(x=c(1, 1, 1, 2, 2))

    out = df >> slice_min(f.x) >> nrow()
    assert out == 3
    out = df >> slice_max(f.x) >> nrow()
    assert out == 2

    out = df >> slice_min(f.x, with_ties=False) >> nrow()
    assert out == 1
    out = df >> slice_max(f.x, with_ties=False) >> nrow()
    assert out == 1
示例#5
0
def test_slice_family_on_rowwise_df():
    df = tibble(x=f[1:6]) >> rowwise()
    out = df >> slice_head(prop=0.1)
    assert out.shape[0] == 0

    out = df >> slice([0, 1, 2])
    assert isinstance(out, TibbleRowwise)
    assert nrow(out) == 5

    out = df >> slice_head(n=3)
    assert isinstance(out, TibbleRowwise)
    assert nrow(out) == 5

    out = df >> slice_tail(n=3)
    assert isinstance(out, TibbleRowwise)
    assert nrow(out) == 5

    out = df >> slice_min(f.x, n=3)
    assert isinstance(out, TibbleRowwise)
    assert nrow(out) == 5

    out = df >> slice_max(f.x, n=3)
    assert isinstance(out, TibbleRowwise)
    assert nrow(out) == 5

    out = df >> slice_sample(n=3)
    assert isinstance(out, TibbleRowwise)
    assert nrow(out) == 5
示例#6
0
def test_proportion_computed_correctly():
    df = tibble(x=range(1, 11))

    out = df >> slice_head(prop=0.11) >> nrow()
    assert out == 1
    out = df >> slice_tail(prop=0.11) >> nrow()
    assert out == 1
    out = df >> slice_sample(prop=0.11) >> nrow()
    assert out == 1
    out = df >> slice_min(f.x, prop=0.11) >> nrow()
    assert out == 1
    out = df >> slice_max(f.x, prop=0.11) >> nrow()
    assert out == 1
    out = df >> slice_max(f.x, prop=0.11, with_ties=False) >> nrow()
    assert out == 1
    out = df >> slice_min(f.x, prop=0.11, with_ties=False) >> nrow()
    assert out == 1
示例#7
0
def test_slicex_on_grouped_data():
    gf = tibble(g=rep([1, 2], each=3), x=seq(1, 6)) >> group_by(f.g)

    out = gf >> slice_min(f.x)
    assert out.equals(tibble(g=[1, 2], x=[1, 4]))
    out = gf >> slice_max(f.x)
    assert out.equals(tibble(g=[1, 2], x=[3, 6]))
    out = gf >> slice_sample()
    assert dim(out) == (2, 2)
示例#8
0
def test_slice_any_checks_for_empty_args_kwargs():
    df = tibble(x=range(1, 11))
    # python recognize n=5
    # with pytest.raises(ValueError):
    #     slice_head(df, 5)
    # with pytest.raises(ValueError):
    #     slice_tail(df, 5)
    with pytest.raises(TypeError):
        df >> slice_min(n=5)
    with pytest.raises(TypeError):
        df >> slice_max(n=5)
示例#9
0
def test_functions_silently_truncate_results():
    df = tibble(x=range(1, 6))
    out = df >> slice_head(n=6) >> nrow()
    assert out == 5
    out = df >> slice_tail(n=6) >> nrow()
    assert out == 5
    out = df >> slice_sample(n=6) >> nrow()
    assert out == 5
    out = df >> slice_min(f.x, n=6) >> nrow()
    assert out == 5
    out = df >> slice_max(f.x, n=6) >> nrow()
    assert out == 5
示例#10
0
def test_preserve_prop_not_support(caplog):
    df = tibble(x=f[:5]) >> group_by(f.x)
    df >> slice(f.x == 2, _preserve=True)
    assert "_preserve" in caplog.text

    with pytest.raises(ValueError):
        df >> slice_min(f.x, prop=0.5)

    with pytest.raises(ValueError):
        df >> slice_max(f.x, prop=0.5)

    with pytest.raises(ValueError):
        df >> slice_sample(f.x, prop=0.5)