def test_sort(): tm.assert_frame_equal(compute(t.sort('amount'), df), pdsort(df, 'amount')) tm.assert_frame_equal(compute(t.sort('amount', ascending=True), df), pdsort(df, 'amount', ascending=True)) tm.assert_frame_equal(compute(t.sort(['amount', 'id']), df), pdsort(df, ['amount', 'id']))
def test_outer_join(): left = [(1, 'Alice', 100), (2, 'Bob', 200), (4, 'Dennis', 400)] left = DataFrame(left, columns=['id', 'name', 'amount']) right = [('NYC', 1), ('Boston', 1), ('LA', 3), ('Moscow', 4)] right = DataFrame(right, columns=['city', 'id']) lsym = symbol('lsym', 'var * {id: int, name: string, amount: real}') rsym = symbol('rsym', 'var * {city: string, id: int}') convert = lambda df: set(df.to_records(index=False).tolist()) assert (convert(compute(join(lsym, rsym), {lsym: left, rsym: right})) == set([(1, 'Alice', 100, 'NYC'), (1, 'Alice', 100, 'Boston'), (4, 'Dennis', 400, 'Moscow')])) assert (convert(compute(join(lsym, rsym, how='left'), {lsym: left, rsym: right})) == set([(1, 'Alice', 100, 'NYC'), (1, 'Alice', 100, 'Boston'), (2, 'Bob', 200, np.nan), (4, 'Dennis', 400, 'Moscow')])) df = compute(join(lsym, rsym, how='right'), {lsym: left, rsym: right}) expected = DataFrame([(1., 'Alice', 100., 'NYC'), (1., 'Alice', 100., 'Boston'), (3., np.nan, np.nan, 'lsymA'), (4., 'Dennis', 400., 'Moscow')], columns=['id', 'name', 'amount', 'city']) result = pdsort(df, 'id').to_records(index=False) expected = pdsort(expected, 'id').to_records(index=False) np.array_equal(result, expected) df = compute(join(lsym, rsym, how='outer'), {lsym: left, rsym: right}) expected = DataFrame([(1., 'Alice', 100., 'NYC'), (1., 'Alice', 100., 'Boston'), (2., 'Bob', 200., np.nan), (3., np.nan, np.nan, 'LA'), (4., 'Dennis', 400., 'Moscow')], columns=['id', 'name', 'amount', 'city']) result = pdsort(df, 'id').to_records(index=False) expected = pdsort(expected, 'id').to_records(index=False) np.array_equal(result, expected)