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
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)
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
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
def test_slice_head_tail_on_grouped_data(): df = tibble(g=[1, 1, 1, 2, 2, 2], x=[1, 2, 3, 4, 5, 6]) >> group_by(f.g) out = slice_head(df, 1) >> ungroup() assert_frame_equal(out, tibble(g=[1, 2], x=[1, 4])) out = slice_tail(df, 1) >> ungroup() assert_frame_equal(out, tibble(g=[1, 2], x=[3, 6]))