Example #1
0
def test_contains_scalar():
    t = Table()
    t.add_column('a', np.arange(4))
    t.add_column('b', np.arange(2))

    filt = t.a.contains(np.array([11]))
    assert np.all(filt.values == np.array([False, False, False, False]))
    assert np.all(filt.index == np.array([1, 1, 1, 1]))
Example #2
0
def test_add_one():
    tb = Table({
        'a': pd.date_range('2000-01-01', freq='M', periods=10),
        'b': np.random.randn(10)
    })
    tb.add_column('schedule', np.array(['first']))
    assert np.all(tb.index == np.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                                        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                                        [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]]))
Example #3
0
def test_add_align():
    t = Table()
    t.add_column('a', [1, 2, 3, 4, 5, 6], dtype=np.int64)
    t.add_column('b', [1, 2, 3], align='bottom', dtype=np.int64)

    c = t.a + t.b

    assert np.all(c.values == np.array([5, 7, 9]))
    assert t.a.values.dtype == np.int64
    assert c.values.dtype == np.int64
Example #4
0
def test_filter_3():
    t = Table()
    t.a = np.random.rand(10)
    t.b = pd.date_range('2000-01-01', freq='M', periods=10)
    t.c = np.array([1, 2])
    t.add_column('d', np.array([1, 2]), align='bottom')
    t1 = t.filter(t.c > 0)

    assert t1.c.values[0] == 1
    assert np.all(t1.d.values == np.array([]))
Example #5
0
def test_hcat_table():
    t = Table({'a': [1, 2, 3], 'b': np.array([4, 5, 6])})
    t.add_column('c', [7, 8, 9])

    assert np.all(t.index == np.ones((3, 3), dtype=np.uint8))
    assert np.all(t.c.values == np.array([7, 8, 9]))

    t.add_column('d', [0, 1, 2, 3, 4])
    assert np.all(t.index == np.array(
        [[1, 1, 1, 0, 0], [1, 1, 1, 0, 0], [1, 1, 1, 0, 0], [1, 1, 1, 1, 1]],
        dtype=np.uint8))
Example #6
0
def test_sort_0():
    t1 = Table()
    t1.add_column('a', [1, 2, 3, 4, 5])
    t1.add_column('b', [1, 2, 3])

    t2 = t1.copy()
    t3 = t1.copy()

    t4 = Table.from_chunks([t1, t2, t3])

    assert np.all(
        t4.b.index == np.array([1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0]))
Example #7
0
def test_crop_2():
    t = Table()
    t.a = np.random.rand(10)
    t.b = pd.date_range('2000-01-01', freq='D', periods=10)
    t.c = np.array([1, 2])
    t.add_column('d', np.array([1, 2]), align='bottom')
    t1 = t.crop('d')

    assert np.all(t1.c.values == np.array([]))
    assert np.all(t1.d.values == np.array([1, 2]))
    assert np.all(t1.a.values == t.a.values[-2:])
    assert np.all(t1.b.values == t.b.values[-2:])
Example #8
0
def test_vcat_heterogeneous():
    tb = Table({
        'a': pd.date_range('2000-01-01', freq='M', periods=3),
        'b': np.random.randn(3)
    })
    tb.add_column('schedule', np.array(['first']))
    tb1 = tb.copy()
    tb1.schedule.values[0] = 'second'
    tb.stack(tb1)
    assert np.all(tb.index == np.array(
        [[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 0, 0, 1, 0, 0]],
        dtype=np.uint8))
    assert np.all(tb.schedule.values == np.array(['first', 'secon']))
Example #9
0
def test_merge_empty_1():
    table_a = Table()
    table_b = Table()

    table_a.add_column('a', [])
    table_a.add_column('b', [])

    table_b.add_column('a', [1, 1, 1])
    table_b.add_column('c', [1, 2, 3])

    c = merge(table_a, table_b, 'a')
    assert np.all(c.a.index == np.array([1, 1, 1]))
    assert np.all(c.b.index == np.array([0, 0, 0]))
    assert np.all(c.c.index == np.array([1, 1, 1]))
Example #10
0
def test_outer_join_datetime():
    t1 = Table({
        'a': pd.date_range('2000-01-01', periods=20),
        'b': np.arange(20)
    })

    t2 = Table({
        'a': pd.date_range('2000-01-01', periods=10),
        'b': np.arange(10)
    })
    t2.add_column('c', [])

    t3 = full_outer_join(t1, t2, 'a')

    assert np.all(t3.b.values == np.arange(20))
Example #11
0
def test_filter_contains_1():
    t = Table()
    t.add_column('a', np.arange(10))
    t.add_column('b', np.arange(5))

    filt = t.a.contains(t.b)
    assert np.all(t.filter(filt).a.values == t.b.values)

    filt = t.a.contains(t.b.values)
    assert np.all(t.filter(filt).a.values == t.b.values)

    assert len(t.filter(filt)) == 5

    filt = t.a.contains(np.array([11]))
    assert t.filter(filt).a.is_empty()
Example #12
0
def test_inner_join_1():
    t1 = Table()
    t1.add_column('a', [1, 2, 3, 4, 5, 6])
    t1.add_column('b', [1, 2, 3])

    t2 = Table()
    t2.add_column('a', [2, 3, 4])
    t2.add_column('b', [1, 1, 1])
    t2.add_column('c', [5, 6, 7])

    t3 = inner_join(t1, t2, 'a')

    assert np.all(t3.a.values == np.array([2, 3, 4]))
    assert np.all(t3.b.values == np.array([2, 3]))
    assert np.all(t3.c.values == np.array([5, 6, 7]))
Example #13
0
def test_filter_date():
    t = Table()
    t.a = np.random.rand(10)
    t.b = pd.date_range('2000-01-01', freq='D', periods=10)
    t.c = np.array([1, 2])
    t.add_column('d', np.array([1, 2]), align='bottom')

    thres1 = np.array(['2000-01-03'], dtype=np.datetime64)
    thres2 = np.array(['2000-01-05'], dtype=np.datetime64)
    t1 = t.filter(t.b >= thres1)
    assert np.all(t1.c.values == np.array([]))
    assert np.all(t1.d.values == np.array([1, 2]))
    assert np.all(t1.a.values == t.a.values[2:])

    t1 = t.filter((t.b >= thres1) & (t.b <= thres2))
    assert np.all(t1.c.values == np.array([]))
    assert np.all(t1.d.values == np.array([]))
    assert np.all(t1.a.values == t.a.values[2:5])

    t1 = t.filter(t.b.date_range(fr=thres1, to=thres2))
    assert np.all(t1.c.values == np.array([]))
    assert np.all(t1.d.values == np.array([]))
    assert np.all(t1.a.values == t.a.values[2:5])