Exemple #1
0
def test_group_split_bind_rows_round_trip():
    iris["Species"] = iris["Species"].astype("category")
    setosa = iris >> filter(f.Species == "setosa")

    chunks = setosa >> group_split.list(f.Species)
    assert len(chunks) == 1
    assert bind_rows(chunks).equals(setosa)

    chunks = setosa >> group_split.list(f.Species, _drop=False)
    assert len(chunks) == 3
    assert_frame_equal(chunks[0], setosa)
Exemple #2
0
def test_group_list_respects_empty_groups():
    tbl = tibble(
        x=[1, 2, 3, 4],
        g=factor(rep(["a", "b"], each=2), levels=["a", "b", "c"]),
    )

    res = group_split.list(tbl, f.g)

    assert res[0].equals(tbl.iloc[:2, :])
    assert res[1].equals(tbl.iloc[[2, 3], :].reset_index(drop=True))

    res = group_split.list(tbl, f.g, _drop=False)
    assert res[0].equals(tbl.iloc[:2, :])
    assert res[1].equals(tbl.iloc[[2, 3], :].reset_index(drop=True))
    assert res[2].equals(tbl.iloc[[], :])
Exemple #3
0
def test_group_split_on_a_rowwise_df_returns_a_list_of_tibbles():
    df = tibble(x=[1, 2])
    rdf = rowwise(df)
    out = group_split.list(rdf)
    assert len(out) == 2
    assert out[0].equals(df.iloc[[0], :])
    assert out[1].equals(df.iloc[[1], :].reset_index(drop=True))
Exemple #4
0
def test_group_split_on_a_grouped_df_returns_a_list_of_tibbles():
    df = tibble(x=[1, 2])
    gdf = group_by(df, f.x)
    out = group_split.list(gdf)
    assert len(out) == 2
    assert out[0].equals(df.iloc[[0], :])
    assert out[1].equals(df.iloc[[1], :].reset_index(drop=True))
Exemple #5
0
def test_group_split_works_with_subclasses_implementing_group_by_ungroup():
    # test_that("group_split() works with subclasses implementing group_by()
    # / ungroup()", {
    class TibbleGrouped1(TibbleGrouped):
        ...

    df = TibbleGrouped1.from_groupby(DataFrame(dict(x=[1, 2, 2])).groupby("x"))
    out = group_split.list(df, f.x)
    assert len(out) == 2
    assert out[0].equals(df.iloc[[0], :])
    assert out[1].equals(df.iloc[[1, 2], :].reset_index(drop=True))
Exemple #6
0
def test_group_split_keep_false_does_not_tryto_remove_virtual_grouping_cols():
    # test_that("group_split(keep=FALSE) does not try to
    # remove virtual grouping columns (#4045)", {
    iris3 = iris.head(4).copy()
    df = group_by(iris3, _bootstrap=[0, 1, 0, 1])
    rows = [[0, 2], [1, 3]]

    res = group_split.list(df, _keep=False)
    iris3 = select(iris3, ~f._bootstrap)
    assert len(res) == 2
    assert_frame_equal(res[0], iris3.iloc[rows[0], :].reset_index(drop=True))
    assert_frame_equal(res[1], iris3.iloc[rows[1], :].reset_index(drop=True))
Exemple #7
0
def test_group_split_respects__drop():
    # test_that("group_split() respects .drop", {
    chunks = tibble(f=factor(["b"], levels=list("abc"))) >> group_split.list(
        f.f, _drop=True)
    assert len(chunks) == 1
Exemple #8
0
def test_group_split_works_if_no_grouping_column():
    out = group_split.list(iris)
    assert len(out) == 1
    assert out[0].equals(iris)
Exemple #9
0
def test_group_split_can_discard_grouping_vars_by__keep_eqs_false():
    tbl = tibble(x=[1, 2, 3, 4], g=factor(rep(["a", "b"], each=2)))
    res = group_split.list(tbl, f.g, _keep=False)

    assert res[0].equals(tbl.iloc[:2, [0]])
    assert res[1].equals(tbl.iloc[[2, 3], [0]].reset_index(drop=True))