def test_dataframe_backed_repr_complex(): df = pd.DataFrame([(1, 'Alice', 100), (2, 'Bob', -200), (3, 'Charlie', 300), (4, 'Denis', 400), (5, 'Edith', -500)], columns=['id', 'name', 'balance']) t = data(df) expr_repr(t[t['balance'] < 0])
def test_no_name_for_simple_data(): d = data([1, 2, 3]) assert expr_repr(d) == ' \n0 1\n1 2\n2 3' assert not d._name d = data(1) assert not d._name assert expr_repr(d) == '1'
def test_csv_with_trailing_commas(): with tmpfile('.csv') as fn: with open(fn, 'wt') as f: # note the trailing space in the header f.write('a,b,c, \n1, 2, 3, ') csv = CSV(fn) assert expr_repr(data(fn)) assert discover(csv).measure.names == ['a', 'b', 'c', ''] with tmpfile('.csv') as fn: with open(fn, 'wt') as f: f.write('a,b,c,\n1, 2, 3, ') # NO trailing space in the header csv = CSV(fn) assert expr_repr(data(fn)) assert discover(csv).measure.names == ['a', 'b', 'c', 'Unnamed: 3']
def test_coerce_date_and_datetime(): x = datetime.datetime.now().date() d = data(x) assert expr_repr(d) == repr(x) x = pd.Timestamp.now() d = data(x) assert expr_repr(d) == repr(x) x = np.nan d = data(x, dshape='datetime') assert expr_repr(d) == repr(pd.NaT) x = float('nan') d = data(x, dshape='datetime') assert expr_repr(d) == repr(pd.NaT)
def test_partially_bound_expr(): df = pd.DataFrame([(1, 'Alice', 100), (2, 'Bob', -200), (3, 'Charlie', 300), (4, 'Denis', 400), (5, 'Edith', -500)], columns=['id', 'name', 'balance']) tdata = data(df, name='data') a = symbol('a', 'int') expr = tdata.name[tdata.balance > a] assert expr_repr(expr) == 'data[data.balance > a].name'
def test_csv_with_trailing_commas(): with tmpfile('.csv') as fn: with open(fn, 'wt') as f: # note the trailing space in the header f.write('a,b,c, \n1, 2, 3, ') csv = CSV(fn) assert expr_repr(data(fn)) assert discover(csv).measure.names == [ 'a', 'b', 'c', '' ] with tmpfile('.csv') as fn: with open(fn, 'wt') as f: f.write('a,b,c,\n1, 2, 3, ') # NO trailing space in the header csv = CSV(fn) assert expr_repr(data(fn)) assert discover(csv).measure.names == [ 'a', 'b', 'c', 'Unnamed: 3' ]
def test_repr(): result = expr_repr(t['name']) print(result) assert isinstance(result, str) assert 'Alice' in result assert 'Bob' in result assert '...' not in result result = expr_repr(t['amount'] + 1) print(result) assert '101' in result t2 = Data(tuple((i, i**2) for i in range(100)), fields=['x', 'y']) assert t2.dshape == dshape('100 * {x: int64, y: int64}') result = expr_repr(t2) print(result) assert len(result.split('\n')) < 20 assert '...' in result
def test_repr(): result = expr_repr(t["name"]) print(result) assert isinstance(result, str) assert "Alice" in result assert "Bob" in result assert "..." not in result result = expr_repr(t["amount"] + 1) print(result) assert "101" in result t2 = Data(tuple((i, i ** 2) for i in range(100)), fields=["x", "y"]) assert t2.dshape == dshape("100 * {x: int64, y: int64}") result = expr_repr(t2) print(result) assert len(result.split("\n")) < 20 assert "..." in result
def test_head_compute(): tdata = tm.makeMixedDataFrame() t = symbol('t', discover(tdata)) db = into('sqlite:///:memory:::t', tdata, dshape=t.dshape) n = 2 d = data(db) # skip the header and the ... at the end of the repr expr = d.head(n) s = expr_repr(expr) assert '...' not in s result = s.split('\n')[1:] assert len(result) == n
def test_generator_reprs_concretely(): x = [1, 2, 3, 4, 5, 6] d = data(x) expr = d[d > 2] + 1 assert '4' in expr_repr(expr)
def test_nameless_data(): tdata = [('a', 1)] assert repr(tdata) in expr_repr(data(tdata))
def test_highly_nested_repr(): tdata = [[0, [[1, 2], [3]], 'abc']] d = data(tdata) assert 'abc' in expr_repr(d.head())
def test_dataframe_backed_repr(): df = pd.DataFrame(data=[0], columns=['col1']) dataframe_backed_table = data(df) expr_repr(dataframe_backed_table)
def test_mutable_backed_repr(): mutable_backed_table = data([[0]], fields=['col1']) expr_repr(mutable_backed_table)
def test_repr_of_scalar(): assert expr_repr(t.amount.sum()) == '300'
def test_repr_on_nd_array_doesnt_err(): d = data(np.ones((2, 2, 2))) expr_repr(d + 1)
def test_coerce_timedelta(): x = datetime.timedelta(days=1, hours=2, minutes=3) d = data(x) assert expr_repr(d) == repr(x)
def test_scalar_sql_compute(): t = into('sqlite:///:memory:::t', tdata, dshape=dshape('var * {name: string, amount: int}')) d = data(t) assert expr_repr(d.amount.sum()) == '300'
def test_expr_repr_empty(): s = expr_repr(t[t.amount > 1e9]) assert isinstance(s, str) assert 'amount' in s
def test_Data_attribute_repr(): t = data(CSV(example('accounts-datetimes.csv'))) result = t.when.day expected = pd.DataFrame({'when_day': [1, 2, 3, 4, 5]}) assert expr_repr(result) == repr(expected)