Example #1
0
def test_outer():
    out = outer([1, 2], [1, 2, 3])
    assert_frame_equal(out, DataFrame([[1, 2, 3], [2, 4, 6]]))

    out = outer([1, 2], [1, 2, 3], fun=paste0)
    assert_frame_equal(out, DataFrame([["11", "12", "13"], ["21", "22",
                                                            "23"]]))
Example #2
0
def test_data_context():
    df = DataFrame(dict(a=[1, 2], b=[3, 4]))
    with data_context(df) as _:
        out = expandgrid(f.a, f.b)

    assert_frame_equal(
        out,
        DataFrame({
            "a": [1, 1, 2, 2],
            "b": [3, 4, 3, 4],
        }),
    )
Example #3
0
def test_expandgrid():
    df = expandgrid([1, 2], [3, 4])
    assert_frame_equal(
        df,
        DataFrame({
            "[1, 2]": [1, 1, 2, 2],
            "[3, 4]": [3, 4, 3, 4],
        }),
    )
Example #4
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))
Example #5
0
def test_rowcolnames():
    df = DataFrame(dict(x=[1, 2, 3]))
    assert colnames(df) == ["x"]
    assert rownames(df).tolist() == [0, 1, 2]
    df = DataFrame([1, 2, 3], index=["a", "b", "c"])
    assert colnames(df) == [0]
    assert rownames(df).tolist() == ["a", "b", "c"]

    df = colnames(df, ["y"])
    assert_iterable_equal(df.columns, ["y"])

    df = colnames(df, ["y"], nested=False)
    assert_iterable_equal(df.columns, ["y"])

    assert_iterable_equal(colnames(df, nested=False), ["y"])

    df = rownames(df, ["a", "b", "c"])
    assert_iterable_equal(df.index, ["a", "b", "c"])

    df = tibble(a=tibble(x=1, y=1), b=tibble(u=2, v=3), z=2)
    df = df >> colnames(["c", "d", "w"], nested=True)
    assert_iterable_equal(df.columns, ["c$x", "c$y", "d$u", "d$v", "w"])
Example #6
0
def test_transform_default():
    @func_factory("transform", "x")
    def double(x):
        return x * 2

    # scalar
    out = double(3)
    assert out[0] == 6

    out = double(np.array([1, 2], dtype=int))
    assert_iterable_equal(out, [2, 4])

    @func_factory("transform", "x")
    def double(x):
        return x * 2

    out = double([1, 2])
    assert_iterable_equal(out, [2, 4])

    # default on series
    x = Series([2, 3], index=["a", "b"])
    out = double(x)
    assert isinstance(out, Series)
    assert_iterable_equal(out.index, ["a", "b"])
    assert_iterable_equal(out, [4, 6])

    # default on dataframe
    x = DataFrame({"a": [3, 4]})
    out = double(x)
    assert isinstance(out, DataFrame)
    assert_iterable_equal(out.a, [6, 8])

    # default on seriesgroupby
    x = Series([1, 2, 1, 2]).groupby([1, 1, 2, 2])
    out = double(x)
    assert isinstance(out, SeriesGroupBy)
    assert_iterable_equal(out.obj, [2, 4, 2, 4])
    assert out.grouper.ngroups == 2

    # on tibble grouped
    x = tibble(x=[1, 2, 1, 2], g=[1, 1, 2, 2]).group_by("g")
    out = double(x)
    # grouping variables not included
    assert_iterable_equal(out.x.obj, [2, 4, 2, 4])

    x = tibble(x=[1, 2, 1, 2], g=[1, 1, 2, 2]).rowwise("g")
    out = double(x)
    assert isinstance(out, TibbleRowwise)
    assert_frame_equal(out, out._datar["grouped"].obj)
    assert_iterable_equal(out.x.obj, [2, 4, 2, 4])
    assert_iterable_equal(out.group_vars, ["g"])
Example #7
0
def test_rownames_preserved():
    df = DataFrame(dict(x=[1, 2]), index=["a", "b"])
    df = mutate(df, y=2)
    assert df.index.tolist() == ["a", "b"]
Example #8
0
def test_non_syntactic_grouping_variable_is_preserved():
    df = DataFrame({"a b": [1]}) >> group_by("a b") >> select()
    assert df.columns.tolist() == ["a b"]
    df = DataFrame({"a b": [1]}) >> group_by(f["a b"]) >> select()
    assert df.columns.tolist() == ["a b"]