def test_order_by(): df = tibble(x=f[1:6]) out = df >> mutate(y=order_by(f[5:], cumsum(f.x))) assert_iterable_equal(out.y, [15, 14, 12, 9, 5]) with pytest.raises(ValueError): order_by(seq(5, 1), cumsum(seq(1, 5)))
def test_diff(): x = cumsum(cumsum(seq(1, 10))) assert_iterable_equal(diff(x, lag=2), x[2:] - x[:-2]) assert_iterable_equal(diff(x, lag=2), seq(3, 10)**2) assert_iterable_equal(diff(diff(x)), diff(x, differences=2)) assert_iterable_equal(diff(x, differences=40), []) x = Series([1, 2, 3, 4, 5]).groupby([1, 2, 2, 3, 3]) out = diff(x) assert_iterable_equal(out.obj, [1, 1]) assert out.grouper.ngroups == 3
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)
def test_can_deframe_3col_df_with_warning(caplog): out = deframe(tibble(name=letters[:3], value=seq(3, 1), oops=[1, 2, 3])) assert out == {"a": 3, "b": 2, "c": 1} assert "one- or two-column" in caplog.text
def test_can_deframe_1col_data_frame(): out = deframe(tibble(value=seq(3, 1))) assert out.tolist() == [3, 2, 1]
def test_can_deframe_2col_data_frame(): out = deframe(tibble(name=letters[:3], value=seq(3, 1))) assert out == {"a": 3, "b": 2, "c": 1}
def test_missing_row_names_stay_missing_when_adding_column(): assert not has_rownames(iris) assert not has_rownames(add_column(iris, x=seq(1, 150), _after=0)) assert not has_rownames( add_column(iris, x=seq(1, 150), _after=ncol(iris) - 1)) assert not has_rownames(add_column(iris, x=seq(1, 150), _before=1))
def test_can_add_column_relative_to_named_column(): df = tibble(a=seq(1, 3), c=seq(4, 6)) df_new = add_column(df, b=seq(-1, 1), _before=f.c) assert_frame_equal(df_new, tibble(a=[1, 2, 3], b=seq(-1, 1), c=[4, 5, 6]))
def test_can_add_column_inbetween(): df = tibble(a=[1, 2, 3], c=[4, 5, 6]) df_new = add_column(df, b=seq(-1, 1), _after=0) assert_frame_equal(df_new, tibble(a=[1, 2, 3], b=seq(-1, 1), c=[4, 5, 6]))
def test_add_column_can_add_to_empty_tibble_or_df(): out = add_column(tibble(_rows=3), a=seq(1, 3)) assert_frame_equal(out, tibble(a=seq(1, 3)))
def test_can_add_new_column(): df_all_new = add_column(df_all, j=seq(1, 3), k=seq(3, 1)) assert nrow(df_all_new) == nrow(df_all) assert_frame_equal(df_all_new.iloc[:, :ncol(df_all)], df_all) assert_iterable_equal(df_all_new.j, [1, 2, 3]) assert_iterable_equal(df_all_new.k, [3, 2, 1])
def test_can_convert_list(): df = enframe(seq(3, 1)) assert df.equals(tibble(name=seq(0, 2), value=seq(3, 1))) # scalar df = enframe(1) assert df.equals(tibble(name=0, value=1))
def test_error_if_both_before_and_after_are_given(): df = tibble(a=seq(1, 3)) with pytest.raises(ValueError): add_row(df, a=[4, 5], _after=1, _before=2)
def test_with_order(): x = [1, 2, 3, 4, 5] out = with_order(seq(5, 1), cumsum, x) assert_iterable_equal(out, [15, 14, 12, 9, 5])
def test_slice_sample_n_defaults_to_1(): df = tibble(g=rep([1, 2], each=3), x=seq(1, 6)) out = df >> slice_sample(n=None) assert dim(out) == (1, 2)