def test_preserves_grouping(): gf = group_by(tibble(x=[1, 2], y=2), f.x) out = mutate(gf, x=1) assert group_vars(out) == ["x"] assert nrow(group_data(out)) == 1 out = mutate(gf, z=1) assert group_data(out).equals(group_data(gf))
def test_0_vars(df): gdata = group_data(group_by(iris)) assert names(gdata) == ["_rows"] out = gdata assert_iterable_equal(out._rows[0], range(nrow(iris))) gdata = group_data(group_by(iris, **{})) assert names(gdata) == ["_rows"] out = gdata assert_iterable_equal(out._rows[0], range(nrow(iris)))
def test_group_data_dataframerowwise_returns_df(): df = tibble(x=[1, 2, 3]) rf = rowwise(df) gd = group_data(rf) assert isinstance(gd, DataFrame) assert gd._rows.tolist() == [[0], [1], [2]]
def test_group_data_dataframegroupby_returns_df(): df = tibble(x=c(1, 1, 2)) gf = group_by(df, f.x) gd = group_data(gf) assert isinstance(gd, DataFrame) assert gd._rows.tolist() == [[0, 1], [2]]
def test_group_rows_group_keys_partition_group_data(): df = tibble(x=[1, 2], y=[1, 2]) rows = group_rows(df) assert rows == [[0, 1]] gf = group_by(df, f.x, f.y) gd = group_data(gf) assert group_keys(gf).equals(gd.iloc[:, [0, 1]]) assert pull(gd, to="list") == group_rows(gf)
def test_na_last(): res = ( tibble(x=c("apple", NA, "banana"), y=range(1, 4)) >> group_by(f.x) >> group_data() ) x = res.x.fillna("") assert x.tolist() == ["apple", "banana", ""] out = res assert_iterable_equal(out._rows, [[0], [2], [1]])
def test_group_data_dataframe_returns_df(): df = tibble(x=[1, 2, 3]) gd = group_data(df) assert isinstance(gd, DataFrame) assert gd._rows.tolist() == [[0, 1, 2]]