def test_row_append_combinations(self): first_tbl = [ Tabl(), Tabl({ 'a': 2, 'b': 3 }), Tabl({ 'a': "ff", 'b': "ffr" }) ] seccond_tbl = [{ 'a': 22, 'b': 13 }, [22, 33], (44, 55), np.array([33, 33]), { 'a': "ded", 'b': "fr" }, ["frd", "werf"], ("efds", "dew"), np.array(["frde", "frtg"])] for ftbl, stbl in product(first_tbl, seccond_tbl): ftbl = deepcopy(ftbl) fl = len(ftbl) ftbl.row_append(stbl) assert ftbl.valid assert len(ftbl) == fl + 1
def test_append_combinations(self): first_tbl = [ Tabl(), Tabl({ 'a': 2, 'b': 3 }), Tabl({ 'a': "ff", 'b': "ffr" }) ] seccond_tbl = [ Tabl({ 'a': 22, 'b': 13 }), pd.DataFrame({ 'a': [22], 'b': [13] }), Tabl({ 'a': "ff", 'b': "ffr" }), pd.DataFrame({ 'a': ["jdw"], 'b': ["okdw"] }), ] for ftbl, stbl in product(first_tbl, seccond_tbl): ftbl = deepcopy(ftbl) fl = len(ftbl) ftbl.append(stbl) assert ftbl.valid assert len(ftbl) == fl + 1
def test_inner_int_join(self): tbl = Tabl({"a": list(range(4)), "b": ['a', 'b'] * 2}) tbl_b = Tabl({"a": list(range(2, 6)), "c": ['d', 'e'] * 2}) tbl_j = tbl.join(tbl_b, "a") assert "c_r" in tbl_j.columns assert tbl_j[0] == (2, 'a', 'd') assert len(tbl_j) == 2 assert np.all(tbl[0] == (0, 'a'))
def test_inner_str_join(self): tbl = Tabl({"a": list(map(str, range(4))), "b": ['a', 'b'] * 2}) tbl_b = Tabl({"a": list(map(str, range(4))), "c": ['d', 'e'] * 2}) tbl_j = tbl.join(tbl_b, "a", jointype="inner") assert "c_r" in tbl_j.columns assert tbl_j[0] == ('0', 'a', 'd') assert len(tbl_j) == 4 assert np.all(tbl[0] == ('0', 'a'))
def test_transpose(self, tbls): for tbl in tbls: if len(tbl) == 0: continue lrec = [tbl[i] for i in range(len(tbl))] tbl_n = Tabl(T(lrec)) for i, column in enumerate(tbl.columns): assert np.all(tbl[column] == tbl_n[str(i)]) lrec = [list(tbl[i]) for i in range(len(tbl))] tbl_n = Tabl(T(lrec)) for i, column in enumerate(tbl.columns): assert np.all(tbl[column] == tbl_n[str(i)])
def test_outer_join(self): tbl = Tabl({"a": list(range(4)), "b": ['a', 'b'] * 2}) tbl_b = Tabl({ "a": list(range(2, 6)), "c": [1, 2] * 2, "d": [1.1, 2.2] * 2 }) tbl_j = tbl.join(tbl_b, "a", jointype='outer') naneq(tbl_j[tbl_j['a_l'] == 0, 'c_r'], Tabl.join_fill_value['integer']) naneq(tbl_j[tbl_j['a_l'] == 0, 'd_r'], Tabl.join_fill_value['float']) naneq(tbl_j[tbl_j['a_r'] == 4, 'b_l'], Tabl.join_fill_value['string']) assert "c_r" in tbl_j.columns assert len(tbl_j) == 6
def test_pandas_b_and_f(self): data = { 'a': [12] * 6, 'b': np.arange(6), 'c': list(map(str, range(6))) } df = pd.DataFrame(data) tbl = Tabl(df) for k in data.keys(): assert np.all(tbl.dict['a'] == data['a']) tbl = Tabl(data) df = pd.DataFrame(tbl.dict) for k in data.keys(): assert np.all(df['a'] == data['a'])
def test_del_col(self): tbl1 = Tabl({ "a": list(range(2, 6)), "b": ['1', '2'] * 2, "c": [1.1, 2.2] * 2 }) tbl2 = Tabl({ "a": list(range(2, 6)), "b": ['1', '2'] * 2, "c": [1.1, 2.2] * 2 }) del tbl1['b'] assert tbl1.columns == ['a', 'c'] assert all(tbl1['a'] == tbl2['a']) assert all(tbl1['c'] == tbl2['c'])
def test_del_row_slice(self): tbl1 = Tabl({ "a": list(range(2, 6)), "b": ['1', '2'] * 2, "c": [1.1, 2.2] * 2 }) tbl2 = Tabl({ "a": list(range(2, 6)), "b": ['1', '2'] * 2, "c": [1.1, 2.2] * 2 }) del tbl1[1:3] assert len(tbl1) == 2 assert tbl1[0] == tbl2[0] assert tbl1[1] == tbl2[3]
def test_sort(self): tbl = Tabl({'aa': ['b', 'g', 'd'], 'b': list(range(3))}) tbl.sort('aa') assert tbl[0] == ('b', 0) assert tbl[1] == ('d', 2) tbl.sort(['aa', 'b']) assert tbl[1] == ('g', 1)
def test_set_row(self): tbl = Tabl({ "a": list(range(2, 6)), "b": ['1', '2'] * 2, "c": [1.1, 2.2] * 2 }) tbl[0, :] = (12, "12", 12.12) tbl[1, ['a', 'b']] = [12, "12"] tbl[2] = (13, "13", 13.13) tbl[3, np.array([True, False, True])] = (14, 14.14)
def test_del_row_int(self): tbl1 = Tabl({ "a": list(range(2, 6)), "b": ['1', '2'] * 2, "c": [1.1, 2.2] * 2 }) tbl2 = Tabl({ "a": list(range(2, 6)), "b": ['1', '2'] * 2, "c": [1.1, 2.2] * 2 }) del tbl1[0] assert len(tbl1) == 3 for i in range(len(tbl1)): assert tbl1[i] == tbl2[i + 1] del tbl1[-1] assert len(tbl1) == 2 assert tbl1[0] == tbl2[1] assert tbl1[1] == tbl2[2]
def test_dtype_properties(self): tbl = Tabl({ "a": list(range(2, 6)), "c": [u'1', u'2'] * 2, "d": [1.1, 2.2] * 2 }) assert tbl.dtype == np.dtype([('a', '<i8'), ('c', '<U1'), ('d', '<f8')]) tbl_n = tbl.astype([np.float, np.int16, np.dtype('<U10')]) assert tbl_n.dtype == np.dtype([('a', '<f8'), ('c', '<i2'), ('d', '<U10')])
def test_add_column(self): tbl = Tabl({ "a": list(range(2, 6)), "b": ['1', '2'] * 2, "c": [1.1, 2.2] * 2 }) tbl["d"] = np.array([True] * len(tbl)) tbl["e"] = 100 tbl["f"] = "Blanc" assert len(tbl.columns) == 6 assert np.all([len(c) == len(tbl) for c in tbl.data])
def test_group_by(self): tbl = Tabl({ 'a': [10, 20, 30, 40] * 3, 'b': ["100", "200"] * 6, 'c': [100, 200] * 6 }) tbl_g = tbl.group_by(['b', 'a'], [(np.sum, 'a'), (first, 'c')]) idx = np.argsort(tbl_g['a_sum']) assert np.all(tbl_g[idx, 'a_sum'] == np.array([30, 60, 90, 120])) assert np.all(tbl['a'] == [10, 20, 30, 40] * 3) assert np.all(tbl_g[idx, 'c_first'] == np.array([100, 200, 100, 200])) tbl_g = tbl.group_by(['b', 'a'], []) idx = np.argsort(tbl_g['a']) assert np.all(tbl_g[idx, 'a'] == [10, 20, 30, 40])
def test_replace_column(self): tbl = Tabl({ "a": list(range(2, 6)), "b": ['1', '2'] * 2, "c": [1.1, 2.2] * 2 }) tbl[:, "a"] = np.array([1.4, 2.4] * 2) assert tbl[0, 'a'] == 1 tbl["a"] = np.array([1.1, 2.2] * 2) assert tbl['a'].dtype == np.float tbl[:, "b"] = np.arange(4) assert tbl[0, "b"] == "0" tbl["b"] = np.arange(4) assert tbl[0, "b"] == 0 tbl[:, "c"] = np.arange(4) assert tbl["c"].dtype == np.float tbl["c"] = np.arange(4) assert tbl["c"].dtype == np.int
def tbls(): tbls = [ Tabl({ 'Name': ["John", "Joe", "Jane"], 'Height': [1.82, 1.65, 2.15], 'Married': [False, False, True] }), Tabl([ np.array(["John", "Joe", "Jane"]), np.array([1.82, 1.65, 2.15]), np.array([False, False, True]) ], columns=["Name", "Height", "Married"]), Tabl([["John", "Joe", "Jane"], [1.82, 1.65, 2.15], [False, False, True]], columns=["Name", "Height", "Married"]), Tabl(), Tabl([1, 2, 3]), Tabl({ 'a': range(10), 'b': range(10) }), Tabl({ 'a': range(10), 'b': list(map(str, range(10))) }), Tabl([range(10), range(10), range(10)]), Tabl(np.array([np.arange(10), np.arange(10, 20)])), Tabl({ 'a': 12, 'b': 13 }), Tabl(pd.DataFrame({ 'a': [12] * 6, 'b': np.arange(6), 'c': range(6) })), Tabl({ 'a': [3, 4, 5], 'b': 1 }), Tabl({ 'a': 1, 'b': [3, 4, 5] }), Tabl({ 'aa': [1, 1, 1], 'b': [3, 4, 5] }), ] return tbls