v2 / vlib / x / dataframe / dataframe_test.v
67 lines · 57 sloc · 1.75 KB · e99c0f40f94e1c6057a8b058a43a7bf63f786b5f
Raw
1import math
2import x.dataframe
3
4const prices_csv = 'symbol,price,qty
5AAPL,189.5,2
6MSFT,420.25,3
7AAPL,191.0,5
8'
9
10fn test_from_csv_and_numeric_series() {
11 df := dataframe.from_csv(prices_csv, dataframe.CsvConfig{})!
12 rows, columns := df.shape()
13 assert rows == 3
14 assert columns == 3
15 assert df.columns == ['symbol', 'price', 'qty']
16 assert df.cell(1, 'symbol')! == 'MSFT'
17
18 prices := df.column('price')!
19 assert prices.len() == 3
20 assert math.alike(prices.mean()!, 266.9166666666667)
21 assert math.alike(prices.min()!, 189.5)
22 assert math.alike(prices.max()!, 420.25)
23 assert math.alike(prices.median()!, 191.0)
24
25 summary := df.describe('qty')!
26 assert summary.count == 3
27 assert math.alike(summary.sum, 10.0)
28 assert math.alike(summary.mean, 3.3333333333333335)
29}
30
31fn test_select_filter_sort_and_value_counts() {
32 df := dataframe.from_csv(prices_csv, dataframe.CsvConfig{})!
33 aapl := df.filter(fn (row dataframe.Row) bool {
34 return row.values['symbol'] == 'AAPL'
35 })
36 assert aapl.height() == 2
37
38 selected := aapl.select(['symbol', 'qty'])!
39 assert selected.width() == 2
40 assert selected.cell(1, 'qty')! == '5'
41
42 sorted := df.sort_by_f64('price', .asc)!
43 assert sorted.cell(0, 'symbol')! == 'AAPL'
44 assert sorted.cell(2, 'symbol')! == 'MSFT'
45
46 counts := df.value_counts('symbol')!
47 assert counts['AAPL'] == 2
48 assert counts['MSFT'] == 1
49}
50
51fn test_from_columns_and_csv_without_header() {
52 df := dataframe.from_columns({
53 'b': ['2', '1']
54 'a': ['x', 'y']
55 })!
56 assert df.columns == ['a', 'b']
57 assert df.cell(0, 'a')! == 'x'
58 assert math.alike(df.column('b')!.median()!, 1.5)
59
60 no_header := dataframe.from_csv('AAPL,189.5
61MSFT,420.25
62', dataframe.CsvConfig{
63 has_header: false
64 })!
65 assert no_header.columns == ['column_0', 'column_1']
66 assert no_header.cell(1, 'column_1')! == '420.25'
67}
68