-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtables.q
More file actions
69 lines (59 loc) · 1.62 KB
/
tables.q
File metadata and controls
69 lines (59 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
OUTPUT: "test/output/"
// Create a {minute: float} dictionary.
t: 01:00 * til 8
x: count[t] ? 1f
ts: t ! x
"Here is a dictionary:"
show ts
// Update a dict. No protection against duplicate keys!
ts[01:00]: 0f
"I have altered the dict."
show ts
// Create a keyed table from the dictionary. Append a new column.
bids: ( [time: key ts]; price: value ts )
donut: (count ts) ? `Cake`Glazed`BostonCream`Golden
bids: update donut:donut from bids
"Here is a table:"
show bids
// Drop keys from a table
bids: 0! bids
"Here is the same table without keys:"
show bids
// Create an unkeyed table by flipping a dict of lists.
"Here is (hopefully) the same table:"
bids1: flip `time`price`donut ! (key ts; value ts; donut)
show bids1
"Are the tables equal?"
show min bids = bids1
"Are the tables identical?"
show bids ~ bids1
// Append more columns. Sort columns.
nrows: count[bids]
tbl: update genus: donut <> `BostonCream from bids
tbl: tbl ^ ([] mass: nrows ? 0.5f; age: nrows ? 12:00)
tbl: (asc cols tbl) xcols tbl
"This new table has donuts *and* topology."
show tbl
// Run some queries
"Get a column"
show tbl[`donut]
"Get more columns"
show `donut`mass # tbl
"Use q-sql select to generate a column"
show select i, donut, holemass: 0.2 * genus * mass from tbl
"Pretend to be SQL some more"
show select i, donut, price from tbl where (price > 0) & (genus = 0)
// Show metadata
show meta tbl
// Save table to CSV format
path: `$ OUTPUT, "tbl.csv"
raze "Save ", string path
save path
// Read CSV and compare.
types: upper (0! meta tbl)[`t]
delims: enlist ","
raze "Read ", string path
tbl1: (types; delims) 0: path
show tbl1
"Do they match?"
show tbl = tbl1