fat_table 0.2.6 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fc10b9aadaada3eb303831288c6cc65d9362a5c6
4
- data.tar.gz: 1a4c9e66382b2879ca6306f955d7aba5c4b9eef1
3
+ metadata.gz: 4c545f67e0496d870adfa57ec7bdbe6b8b0b5af8
4
+ data.tar.gz: 59a03a6b61876344facf14a5ec6a65bf47300b33
5
5
  SHA512:
6
- metadata.gz: 2f263f677bdd964642a6700c5c139d689184059ee994e551941dba6fac2d1d3bc3830ac63c7b28e526356b0166ee63c255ffac00769a38ee6e71fba090216b9f
7
- data.tar.gz: 5d02b955317232d48a78f8ddb73632a4aaea5c49da31f72a808303a362c295203b7f6d508b55f03b14687953b72c7870a840844ff2779bb0cbc1ae16a966bd22
6
+ metadata.gz: ec213d073ddad80a8167b05e3a0fad399886c605a41160c41c428e2664f030f862c159bb077558521ce1bb32fd004527ec540fb8e3e51906d993712b9be0abeb
7
+ data.tar.gz: e160d5bacf83be72296029835f22412565e27649c097869f2653b86b8b23a65d0d541593349221fde1c5b6dc381785b0d192f3cd434269c6a2647362f433cfdc
data/.gitignore CHANGED
@@ -23,3 +23,4 @@
23
23
  /GPATH
24
24
  /GRTAGS
25
25
  /GTAGS
26
+ /TAGS
@@ -2,6 +2,8 @@ language: ruby
2
2
  before_install:
3
3
  - sudo apt-get -qq update
4
4
  - sudo apt-get install -y texlive-latex-base texlive-latex-recommended
5
+ before_script:
6
+ - createdb -U travis travis
5
7
  after_failure:
6
8
  - "pwd"
7
9
  - "cat ./spec/tmp/latex.err"
@@ -0,0 +1,2168 @@
1
+
2
+ # Table of Contents
3
+
4
+ 1. [Introduction](#org23d768e)
5
+ 2. [Installation](#org8d90fdf)
6
+ 1. [Prerequisites](#org26d2aee)
7
+ 2. [Installing the gem](#orga19109b)
8
+ 3. [Usage](#org0b5ecd8)
9
+ 1. [Quick Start](#org199fc3a)
10
+ 2. [A Word About the Examples](#org1e51988)
11
+ 3. [Anatomy of a Table](#org7d48b5d)
12
+ 1. [Columns](#org4a6c98f)
13
+ 2. [Headers](#org37bbf47)
14
+ 3. [Groups](#org1c03cc1)
15
+ 4. [Constructing Tables](#orgbf0e735)
16
+ 1. [Empty Tables](#org80c41f5)
17
+ 2. [From CSV or Org Mode files or strings](#org681a599)
18
+ 3. [From Arrays of Arrays](#org4f683cf)
19
+ 4. [From Arrays of Hashes](#org7980800)
20
+ 5. [From SQL queries](#orgdab2ec1)
21
+ 6. [Marking Groups in Input](#orgeb97e36)
22
+ 5. [Accessing Parts of Tables](#orgf9cb237)
23
+ 1. [Rows](#org4453cea)
24
+ 2. [Columns](#org8a6dd85)
25
+ 3. [Cells](#orgcc87a8b)
26
+ 4. [Other table attributes](#org4a41de4)
27
+ 6. [Operations on Tables](#org731fd13)
28
+ 1. [Example Input Table](#orga96ca08)
29
+ 2. [Select](#orga0c49b3)
30
+ 3. [Where](#orge185ad7)
31
+ 4. [Order\_by](#org57f51d1)
32
+ 5. [Group\_by](#org1ee0a85)
33
+ 6. [Join](#org6432f26)
34
+ 7. [Set Operations](#org7d2857d)
35
+ 8. [Uniq (aka Distinct)](#org073a8b5)
36
+ 9. [Remove groups with degroup!](#orgd147303)
37
+ 7. [Formatting Tables](#org9f4d633)
38
+ 1. [Available Formatters](#orgb7b2335)
39
+ 2. [Table Locations](#org4db9ae4)
40
+ 3. [Formatting Directives](#orgd2128a3)
41
+ 4. [Footers Methods](#org947e8a4)
42
+ 5. [Formatting Methods](#orgcef241a)
43
+ 6. [The `format` and `format_for` methods](#org7b25866)
44
+ 4. [Development](#org62e325b)
45
+ 5. [Contributing](#orgf51a2c9)
46
+
47
+ <https://travis-ci.org/ddoherty03/fat_table.svg?branch=master>
48
+
49
+
50
+ <a id="org23d768e"></a>
51
+
52
+ # Introduction
53
+
54
+ `FatTable` is a gem that treats tables as a data type. It provides methods for
55
+ constructing tables from a variety of sources, building them row-by-row,
56
+ extracting rows, columns, and cells, and performing aggregate operations on
57
+ columns. It also provides as set of SQL-esque methods for manipulating table
58
+ objects: `select` for filtering by columns or for creating new columns, `where`
59
+ for filtering by rows, `order_by` for sorting rows, `distinct` for eliminating
60
+ duplicate rows, `group_by` for aggregating multiple rows into single rows and
61
+ applying column aggregate methods to ungrouped columns, a collection of `join`
62
+ methods for combining tables, and more.
63
+
64
+ Furthermore, `FatTable` provides methods for formatting tables and producing
65
+ output that targets various output media: text, ANSI terminals, ruby data
66
+ structures, LaTeX tables, Emacs org-mode tables, and more. The formatting
67
+ methods can specify cell formatting in a way that is uniform across all the
68
+ output methods and can also decorate the output with any number of footers,
69
+ including group footers. `FatTable` applies formatting directives to the extent
70
+ they makes sense for the output medium and treats other formatting directives as
71
+ no-ops.
72
+
73
+ `FatTable` can be used to perform operations on data that are naturally best
74
+ conceived of as tables, which in my experience is quite often. It can also serve
75
+ as a foundation for providing reporting functions where flexibility about the
76
+ output medium can be quite useful. Finally `FatTable` can be used within Emacs
77
+ `org-mode` files in code blocks targeting the Ruby language. Org mode tables are
78
+ presented to a ruby code block as an array of arrays, so `FatTable` can read
79
+ them in with its `.from_aoa` constructor. A `FatTable` table output as an array
80
+ of arrays with its `.to_aoa` output function will be rendered in an org-mode
81
+ buffer as an org-table, ready for processing by other code blocks.
82
+
83
+
84
+ <a id="org8d90fdf"></a>
85
+
86
+ # Installation
87
+
88
+
89
+ <a id="org26d2aee"></a>
90
+
91
+ ## Prerequisites
92
+
93
+ The `fat_table` gem depends on several libraries being available for building,
94
+ mostly those concerned with accessing databases. On an ubuntu system, the
95
+ following packages should be installed before you install the `fat_table` gem:
96
+
97
+ - ruby-dev
98
+ - build-essential
99
+ - libsqlite3-dev
100
+ - libpq-dev
101
+ - libmysqlclient-dev
102
+
103
+
104
+ <a id="orga19109b"></a>
105
+
106
+ ## Installing the gem
107
+
108
+ Add this line to your application&rsquo;s Gemfile:
109
+
110
+ gem 'fat_table'
111
+
112
+ And then execute:
113
+
114
+ $ bundle
115
+
116
+ Or install it yourself as:
117
+
118
+ $ gem install fat_table
119
+
120
+
121
+ <a id="org0b5ecd8"></a>
122
+
123
+ # Usage
124
+
125
+
126
+ <a id="org199fc3a"></a>
127
+
128
+ ## Quick Start
129
+
130
+ `FatTable` provides table objects as a data type that can be constructed and
131
+ operated on in a number of ways. Here&rsquo;s a quick example to illustrate the use of
132
+ the main features of `FatTable`. See the detailed explanations further on down.
133
+
134
+ require 'fat_table'
135
+
136
+ data =
137
+ [['Date', 'Code', 'Raw', 'Shares', 'Price', 'Info', 'Ok'],
138
+ ['2013-05-29', 'S', 15_700.00, 6601.85, 24.7790, 'ENTITY3', 'F'],
139
+ ['2013-05-02', 'P', 118_186.40, 118_186.4, 11.8500, 'ENTITY1', 'T'],
140
+ ['2013-05-20', 'S', 12_000.00, 5046.00, 28.2804, 'ENTITY3', 'F'],
141
+ ['2013-05-23', 'S', 8000.00, 3364.00, 27.1083, 'ENTITY3', 'T'],
142
+ ['2013-05-23', 'S', 39_906.00, 16_780.47, 25.1749, 'ENTITY3', 'T'],
143
+ ['2013-05-20', 'S', 85_000.00, 35_742.50, 28.3224, 'ENTITY3', 'T'],
144
+ ['2013-05-02', 'P', 795_546.20, 795_546.2, 1.1850, 'ENTITY1', 'T'],
145
+ ['2013-05-29', 'S', 13_459.00, 5659.51, 24.7464, 'ENTITY3', 'T'],
146
+ ['2013-05-20', 'S', 33_302.00, 14_003.49, 28.6383, 'ENTITY3', 'T'],
147
+ ['2013-05-29', 'S', 15_900.00, 6685.95, 24.5802, 'ENTITY3', 'T'],
148
+ ['2013-05-30', 'S', 6_679.00, 2808.52, 25.0471, 'ENTITY3', 'T'],
149
+ ['2013-05-23', 'S', 23_054.00, 9694.21, 26.8015, 'ENTITY3', 'F']]
150
+
151
+ # Build the Table and then perform chained operations on it
152
+
153
+ table = FatTable.from_aoa(data) \
154
+ .where('shares > 2000') \
155
+ .order_by(:date, :code) \
156
+ .select(:date, :code, :shares,
157
+ :price, :ok, ref: '@row') \
158
+ .select(:ref, :date, :code,
159
+ :shares, :price, :ok)
160
+
161
+ # Convert the table to an ASCII text string
162
+
163
+ table.to_text do |fmt|
164
+ # Add some table footers
165
+ fmt.avg_footer(:price, :shares)
166
+ fmt.sum_footer(:shares)
167
+ # Add a group footer
168
+ fmt.gfooter('Avg', shares: :avg, price: :avg)
169
+ # Formats for all locations
170
+ fmt.format(ref: 'CB', numeric: 'R', boolean: 'CY')
171
+ # Formats for different "locations" in the table
172
+ fmt.format_for(:header, string: 'CB')
173
+ fmt.format_for(:body, code: 'C', shares: ',0.1', price: '0.4', )
174
+ fmt.format_for(:bfirst, price: '$0.4', )
175
+ fmt.format_for(:footer, shares: 'B,0.1', price: '$B0.4', )
176
+ fmt.format_for(:gfooter, shares: 'B,0.1', price: 'B0.4', )
177
+ end
178
+
179
+ +=========+============+======+=============+==========+====+
180
+ | Ref | Date | Code | Shares | Price | Ok |
181
+ +---------|------------|------|-------------|----------|----+
182
+ | 1 | 2013-05-02 | P | 118,186.4 | $11.8500 | Y |
183
+ | 2 | 2013-05-02 | P | 795,546.2 | 1.1850 | Y |
184
+ +---------|------------|------|-------------|----------|----+
185
+ | Avg | | | 456,866.3 | 6.5175 | |
186
+ +---------|------------|------|-------------|----------|----+
187
+ | 3 | 2013-05-20 | S | 5,046.0 | 28.2804 | N |
188
+ | 4 | 2013-05-20 | S | 35,742.5 | 28.3224 | Y |
189
+ | 5 | 2013-05-20 | S | 14,003.5 | 28.6383 | Y |
190
+ +---------|------------|------|-------------|----------|----+
191
+ | Avg | | | 18,264.0 | 28.4137 | |
192
+ +---------|------------|------|-------------|----------|----+
193
+ | 6 | 2013-05-23 | S | 3,364.0 | 27.1083 | Y |
194
+ | 7 | 2013-05-23 | S | 16,780.5 | 25.1749 | Y |
195
+ | 8 | 2013-05-23 | S | 9,694.2 | 26.8015 | N |
196
+ +---------|------------|------|-------------|----------|----+
197
+ | Avg | | | 9,946.2 | 26.3616 | |
198
+ +---------|------------|------|-------------|----------|----+
199
+ | 9 | 2013-05-29 | S | 6,601.9 | 24.7790 | N |
200
+ | 10 | 2013-05-29 | S | 5,659.5 | 24.7464 | Y |
201
+ | 11 | 2013-05-29 | S | 6,686.0 | 24.5802 | Y |
202
+ +---------|------------|------|-------------|----------|----+
203
+ | Avg | | | 6,315.8 | 24.7019 | |
204
+ +---------|------------|------|-------------|----------|----+
205
+ | 12 | 2013-05-30 | S | 2,808.5 | 25.0471 | Y |
206
+ +---------|------------|------|-------------|----------|----+
207
+ | Avg | | | 2,808.5 | 25.0471 | |
208
+ +---------|------------|------|-------------|----------|----+
209
+ | Average | | | 85,009.9 | $23.0428 | |
210
+ +---------|------------|------|-------------|----------|----+
211
+ | Total | | | 1,020,119.1 | | |
212
+ +=========+============+======+=============+==========+====+
213
+
214
+
215
+ <a id="org1e51988"></a>
216
+
217
+ ## A Word About the Examples
218
+
219
+ When you install the `fat_table` gem, you have access to a program `ft_console`
220
+ which opens a `pry` session with `fat_table` loaded and the tables used in the
221
+ examples in this `README` defined as instance variables so you can experiment
222
+ with them. Because they are defined as instance variables, you have to write
223
+ `tab1` as `@tab1` in `ft_console`, but otherwise the examples should work.
224
+
225
+ The examples in this `README` file are executed as code blocks within the
226
+ `README.org` file, so they typically end with a call to `.to_aoa`. That causes
227
+ the table to be inserted into the file and formatted as a table. With
228
+ `ft_console`, you should instead display your tables with `.to_text` or
229
+ `.to_term`. These will return a string that you can print to the terminal with
230
+ `puts`.
231
+
232
+ To read in the table used in the Quick Start section above, you might do the
233
+ following:
234
+
235
+ $ ft_console[1] pry(main)> ls
236
+ ActiveSupport::ToJsonWithActiveSupportEncoder#methods: to_json
237
+ self.methods: inspect to_s
238
+ instance variables:
239
+ @aoa @tab1 @tab2 @tab_a @tab_b @tt
240
+ @data @tab1_str @tab2_str @tab_a_str @tab_b_str
241
+ locals: _ __ _dir_ _ex_ _file_ _in_ _out_ _pry_ lib str version
242
+ [2] pry(main)> table = FatTable.from_aoa(@data)
243
+ => #<FatTable::Table:0x0055b40e6cd870
244
+ @boundaries=[],
245
+ @columns=
246
+ [#<FatTable::Column:0x0055b40e6cc948
247
+ @header=:date,
248
+ @items=
249
+ [Wed, 29 May 2013,
250
+ Thu, 02 May 2013,
251
+ Mon, 20 May 2013,
252
+ Thu, 23 May 2013,
253
+ Thu, 23 May 2013,
254
+ Mon, 20 May 2013,
255
+ Thu, 02 May 2013,
256
+ Wed, 29 May 2013,
257
+ Mon, 20 May 2013,
258
+ ...
259
+ @items=["ENTITY3", "ENTITY1", "ENTITY3", "ENTITY3", "ENTITY3", "ENTITY3", "ENTITY1", "ENTITY3", "ENTITY3", "ENTITY3", "ENTITY3", "ENTITY3"],
260
+ @raw_header=:info,
261
+ @type="String">,
262
+ #<FatTable::Column:0x0055b40e6d2668 @header=:ok, @items=[false, true, false, true, true, true, true, true, true, true, true, false], @raw_header=:ok, @type="Boolean">]>
263
+ [3] pry(main)> puts table.to_text
264
+ +============+======+==========+==========+=========+=========+====+
265
+ | Date | Code | Raw | Shares | Price | Info | Ok |
266
+ +------------|------|----------|----------|---------|---------|----+
267
+ | 2013-05-29 | S | 15700.0 | 6601.85 | 24.779 | ENTITY3 | F |
268
+ | 2013-05-02 | P | 118186.4 | 118186.4 | 11.85 | ENTITY1 | T |
269
+ | 2013-05-20 | S | 12000.0 | 5046.0 | 28.2804 | ENTITY3 | F |
270
+ | 2013-05-23 | S | 8000.0 | 3364.0 | 27.1083 | ENTITY3 | T |
271
+ | 2013-05-23 | S | 39906.0 | 16780.47 | 25.1749 | ENTITY3 | T |
272
+ | 2013-05-20 | S | 85000.0 | 35742.5 | 28.3224 | ENTITY3 | T |
273
+ | 2013-05-02 | P | 795546.2 | 795546.2 | 1.185 | ENTITY1 | T |
274
+ | 2013-05-29 | S | 13459.0 | 5659.51 | 24.7464 | ENTITY3 | T |
275
+ | 2013-05-20 | S | 33302.0 | 14003.49 | 28.6383 | ENTITY3 | T |
276
+ | 2013-05-29 | S | 15900.0 | 6685.95 | 24.5802 | ENTITY3 | T |
277
+ | 2013-05-30 | S | 6679.0 | 2808.52 | 25.0471 | ENTITY3 | T |
278
+ | 2013-05-23 | S | 23054.0 | 9694.21 | 26.8015 | ENTITY3 | F |
279
+ +============+======+==========+==========+=========+=========+====+
280
+ => nil
281
+ [4] pry(main)>
282
+
283
+ And if you use `.to_term`, you can see the effect of the color formatting
284
+ directives.
285
+
286
+
287
+ <a id="org7d48b5d"></a>
288
+
289
+ ## Anatomy of a Table
290
+
291
+
292
+ <a id="org4a6c98f"></a>
293
+
294
+ ### Columns
295
+
296
+ `FatTable::Table` objects consist of an array of `FatTable::Column` objects.
297
+ Each `Column` has a header, a type, and an array of items, all of the given type
298
+ or nil. There are only five permissible types for a `Column`:
299
+
300
+ 1. **Boolean** (for holding ruby `TrueClass` and `FalseClass` objects),
301
+ 2. **DateTime** (for holding ruby `DateTime` or `Date` objects),
302
+ 3. **Numeric** (for holding ruby `Integer`, `Rational`, or `BigDecimal` objects),
303
+ 4. **String** (for ruby `String` objects), or
304
+ 5. **NilClass** (for the undetermined column type).
305
+
306
+ When a `Table` is constructed from an external source, all `Columns` start out
307
+ having a type of `NilClass`, that is, their type is as yet undetermined. When a
308
+ string or object of one of the four determined types is added to a `Column`, it
309
+ fixes the type of the column and all further items added to the `Column` must
310
+ either be `nil` (indicating no value) or be capable of being coerced to the
311
+ column&rsquo;s type. Otherwise, `FatTable` raises an exception.
312
+
313
+ Items of input must be either one of the permissible ruby objects or strings. If
314
+ they are strings, `FatTable` attempts to parse them as one of the permissible
315
+ types as follows:
316
+
317
+ - **Boolean:** the strings, `'t'`, `'true'`, `'yes'`, or `'y'`, regardless of
318
+ case, are interpreted as `TrueClass` and the strings, `'f'`, `'false'`,
319
+ `'no'`, or `'n'`, regardless of case, are interpreted as `FalseClass`, in
320
+ either case resulting in a Boolean column. Empty strings in a column
321
+ already having a Boolean type are converted to `nil`.
322
+ - **DateTime:** strings that contain patterns of `'yyyy-mm-dd'` or `'yyyy/mm/dd'`
323
+ or `'mm-dd-yyy'` or `'mm/dd/yyyy'` or any of the foregoing with an added
324
+ `'Thh:mm:ss'` or `'Thh:mm'` will be interpreted as a `DateTime` or a `Date`
325
+ (if there are no sub-day time components present). The number of digits in
326
+ the month and day can be one or two, but the year component must be four
327
+ digits. Any time components are valid if they can be properly interpreted
328
+ by `DateTime.parse`. Org mode timestamps (any of the foregoing surrounded
329
+ by square &rsquo;`[]`&rsquo; or pointy &rsquo;`<>`&rsquo; brackets), active or inactive, are valid
330
+ input strings for `DateTime` columns. Empty strings in a column already
331
+ having the `DateTime` type are converted to `nil`.
332
+ - **Numeric:** all commas `','`, underscores, `'_'`, and `'$'` dollar signs (or
333
+ other currency symbol as set by `FatTable.currency_symbol` are removed from
334
+ the string and if the remaining string can be interpreted as a `Numeric`,
335
+ it will be. It is interpreted as an `Integer` if there are no decimal
336
+ places in the remaining string, as a `Rational` if the string has the form
337
+ &rsquo;`<number>:<number>`&rsquo; or &rsquo;`<number>/<number>`&rsquo;, or as a `BigDecimal` if
338
+ there is a decimal point in the remaining string. Empty strings in a column
339
+ already having the Numeric type are converted to nil.
340
+ - **String:** if all else fails, `FatTable` applies `#to_s` to the input value
341
+ and, treats it as an item of type `String`. Empty strings in a column
342
+ already having the `String` type are kept as empty strings.
343
+ - **NilClass:** until the input contains a non-blank string that can be parsed as
344
+ one of the other types, it has this type, meaning that the type is still
345
+ open. A column comprised completely of blank strings or `nils` will retain
346
+ the `NilClass` type.
347
+
348
+
349
+ <a id="org37bbf47"></a>
350
+
351
+ ### Headers
352
+
353
+ Headers for the columns are formed from the input. No two columns in a table can
354
+ have the same header. Headers in the input are converted to symbols by
355
+
356
+ - converting the header to a string with `#to_s`,
357
+ - converting any run of blanks to an underscore `_`,
358
+ - removing any characters that are not letters, numbers, or underscores, and
359
+ - lowercasing all remaining letters
360
+
361
+ Thus, a header of `'Date'` becomes `:date`, a header of `'Id Number'` becomes,
362
+ `:id_number`, etc. When referring to a column in code, you must use the symbol
363
+ form of the header.
364
+
365
+ If no sensible headers can be discerned from the input, headers of the form
366
+ `:col_1`, `:col_2`, etc., are synthesized.
367
+
368
+
369
+ <a id="org1c03cc1"></a>
370
+
371
+ ### Groups
372
+
373
+ The rows of a `FatTable` table can be sub-divided into groups, either from
374
+ markers in the input or as a result of certain operations. There is only one
375
+ level of grouping, so `FatTable` has no concept of sub-groups. Groups can be
376
+ shown on output with rules or &ldquo;hlines&rdquo; that underline the last row in each
377
+ group, and you can decorate the output with group footers that summarize the
378
+ columns in each group.
379
+
380
+
381
+ <a id="orgbf0e735"></a>
382
+
383
+ ## Constructing Tables
384
+
385
+
386
+ <a id="org80c41f5"></a>
387
+
388
+ ### Empty Tables
389
+
390
+ You can create an empty table with `FatTable.new`, and then add rows with the
391
+ `<<` operator and a Hash:
392
+
393
+ tab = FatTable.new
394
+ tab << { a: 1, b: 2, c: "<2017-01-21>', d: 'f', e: '' }
395
+ tab << { a: 3.14, b: 2.17, c: '[2016-01-21 Thu]', d: 'Y', e: nil }
396
+ tab.to_aoa
397
+
398
+ After this, the table will have column headers `:a`, `:b`, `:c`, `:d`, and `:e`.
399
+ Column, `:a` and `:b` will have type Numeric, column `:c` will have type
400
+ `DateTime`, and column `:d` will have type `Boolean`. Column `:e` will still
401
+ have an open type. Notice that dates in the input can be wrapped in brackets as
402
+ in org-mode time stamps.
403
+
404
+
405
+ <a id="org681a599"></a>
406
+
407
+ ### From CSV or Org Mode files or strings
408
+
409
+ Tables can also be read from `.csv` files or files containing `org-mode` tables.
410
+ In the case of org-mode files, `FatTable` skips through the file until it finds
411
+ a line that look like a table, that is, it begins with any number of spaces
412
+ followed by `|-`. Only the first table in an `.org` file is read.
413
+
414
+ For both `.csv` and `.org` files, the first row in the tables is taken as the
415
+ header row, and the headers are converted to symbols as described above.
416
+
417
+ tab1 = FatTable.from_csv_file('~/data.csv')
418
+ tab2 = FatTable.from_org_file('~/project.org')
419
+
420
+ csv_body = <<-EOS
421
+ Ref,Date,Code,RawShares,Shares,Price,Info
422
+ 1,2006-05-02,P,5000,5000,8.6000,2006-08-09-1-I
423
+ 2,2006-05-03,P,5000,5000,8.4200,2006-08-09-1-I
424
+ 3,2006-05-04,P,5000,5000,8.4000,2006-08-09-1-I
425
+ 4,2006-05-10,P,8600,8600,8.0200,2006-08-09-1-D
426
+ 5,2006-05-12,P,10000,10000,7.2500,2006-08-09-1-D
427
+ 6,2006-05-12,P,2000,2000,6.7400,2006-08-09-1-I
428
+ EOS
429
+
430
+ tab3 = FatTable.from_csv_string(csv_body)
431
+
432
+ org_body = <<-EOS
433
+ .* Smith Transactions
434
+ :PROPERTIES:
435
+ :TABLE_EXPORT_FILE: smith.csv
436
+ :END:
437
+
438
+ #+TBLNAME: smith_tab
439
+ | Ref | Date | Code | Raw | Shares | Price | Info |
440
+ |-----|------------|------|---------|--------|----------|---------|
441
+ | 29 | 2013-05-02 | P | 795,546 | 2,609 | 1.18500 | ENTITY1 |
442
+ | 30 | 2013-05-02 | P | 118,186 | 388 | 11.85000 | ENTITY1 |
443
+ | 31 | 2013-05-02 | P | 340,948 | 1,926 | 1.18500 | ENTITY2 |
444
+ | 32 | 2013-05-02 | P | 50,651 | 286 | 11.85000 | ENTITY2 |
445
+ | 33 | 2013-05-20 | S | 12,000 | 32 | 28.28040 | ENTITY3 |
446
+ | 34 | 2013-05-20 | S | 85,000 | 226 | 28.32240 | ENTITY3 |
447
+ | 35 | 2013-05-20 | S | 33,302 | 88 | 28.63830 | ENTITY3 |
448
+ | 36 | 2013-05-23 | S | 8,000 | 21 | 27.10830 | ENTITY3 |
449
+ | 37 | 2013-05-23 | S | 23,054 | 61 | 26.80150 | ENTITY3 |
450
+ | 38 | 2013-05-23 | S | 39,906 | 106 | 25.17490 | ENTITY3 |
451
+ | 39 | 2013-05-29 | S | 13,459 | 36 | 24.74640 | ENTITY3 |
452
+ | 40 | 2013-05-29 | S | 15,700 | 42 | 24.77900 | ENTITY3 |
453
+ | 41 | 2013-05-29 | S | 15,900 | 42 | 24.58020 | ENTITY3 |
454
+ | 42 | 2013-05-30 | S | 6,679 | 18 | 25.04710 | ENTITY3 |
455
+
456
+ .* Another Heading
457
+ EOS
458
+
459
+ tab4 = FatTable.from_org_string(org_body)
460
+
461
+
462
+ <a id="org4f683cf"></a>
463
+
464
+ ### From Arrays of Arrays
465
+
466
+ You can also initialize a table directly from ruby data structures. You can, for
467
+ example, build a table from an array of arrays:
468
+
469
+ aoa = [
470
+ ['Ref', 'Date', 'Code', 'Raw', 'Shares', 'Price', 'Info', 'Bool'],
471
+ [1, '2013-05-02', 'P', 795_546.20, 795_546.2, 1.1850, 'ENTITY1', 'T'],
472
+ [2, '2013-05-02', 'P', 118_186.40, 118_186.4, 11.8500, 'ENTITY1', 'T'],
473
+ [7, '2013-05-20', 'S', 12_000.00, 5046.00, 28.2804, 'ENTITY3', 'F'],
474
+ [8, '2013-05-20', 'S', 85_000.00, 35_742.50, 28.3224, 'ENTITY3', 'T'],
475
+ [9, '2013-05-20', 'S', 33_302.00, 14_003.49, 28.6383, 'ENTITY3', 'T'],
476
+ [10, '2013-05-23', 'S', 8000.00, 3364.00, 27.1083, 'ENTITY3', 'T'],
477
+ [11, '2013-05-23', 'S', 23_054.00, 9694.21, 26.8015, 'ENTITY3', 'F'],
478
+ [12, '2013-05-23', 'S', 39_906.00, 16_780.47, 25.1749, 'ENTITY3', 'T'],
479
+ [13, '2013-05-29', 'S', 13_459.00, 5659.51, 24.7464, 'ENTITY3', 'T'],
480
+ [14, '2013-05-29', 'S', 15_700.00, 6601.85, 24.7790, 'ENTITY3', 'F'],
481
+ [15, '2013-05-29', 'S', 15_900.00, 6685.95, 24.5802, 'ENTITY3', 'T'],
482
+ [16, '2013-05-30', 'S', 6_679.00, 2808.52, 25.0471, 'ENTITY3', 'T']
483
+ ]
484
+ tab = FatTable.from_aoa(aoa)
485
+
486
+ Notice that the values can either be ruby objects, such as the Integer `85_000`,
487
+ or strings that can be parsed into one of the permissible column types.
488
+
489
+ This method of building a table, `.from_aoa`, is particularly useful in dealing
490
+ with Emacs org-mode code blocks. Tables in org-mode are passed to code blocks as
491
+ arrays of arrays. Likewise, a result of a code block in the form of an array of
492
+ arrays is displayed as an org-mode table:
493
+
494
+ #+NAME: trades1
495
+ | Ref | Date | Code | Price | G10 | QP10 | Shares | LP | QP | IPLP | IPQP |
496
+ |------|------------|------|--------|-----|------|--------|-------|--------|--------|--------|
497
+ | T001 | 2016-11-01 | P | 7.7000 | T | F | 100 | 14 | 86 | 0.2453 | 0.1924 |
498
+ | T002 | 2016-11-01 | P | 7.7500 | T | F | 200 | 28 | 172 | 0.2453 | 0.1924 |
499
+ | T003 | 2016-11-01 | P | 7.5000 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
500
+ | T004 | 2016-11-01 | S | 7.5500 | T | F | 6811 | 966 | 5845 | 0.2453 | 0.1924 |
501
+ | T005 | 2016-11-01 | S | 7.5000 | F | F | 4000 | 572 | 3428 | 0.2453 | 0.1924 |
502
+ | T006 | 2016-11-01 | S | 7.6000 | F | T | 1000 | 143 | 857 | 0.2453 | 0.1924 |
503
+ | T007 | 2016-11-01 | S | 7.6500 | T | F | 200 | 28 | 172 | 0.2453 | 0.1924 |
504
+ | T008 | 2016-11-01 | P | 7.6500 | F | F | 2771 | 393 | 2378 | 0.2453 | 0.1924 |
505
+ | T009 | 2016-11-01 | P | 7.6000 | F | F | 9550 | 1363 | 8187 | 0.2453 | 0.1924 |
506
+ | T010 | 2016-11-01 | P | 7.5500 | F | T | 3175 | 451 | 2724 | 0.2453 | 0.1924 |
507
+ | T011 | 2016-11-02 | P | 7.4250 | T | F | 100 | 14 | 86 | 0.2453 | 0.1924 |
508
+ | T012 | 2016-11-02 | P | 7.5500 | F | F | 4700 | 677 | 4023 | 0.2453 | 0.1924 |
509
+ | T013 | 2016-11-02 | P | 7.3500 | T | T | 53100 | 7656 | 45444 | 0.2453 | 0.1924 |
510
+ | T014 | 2016-11-02 | P | 7.4500 | F | T | 5847 | 835 | 5012 | 0.2453 | 0.1924 |
511
+ | T015 | 2016-11-02 | P | 7.7500 | F | F | 500 | 72 | 428 | 0.2453 | 0.1924 |
512
+ | T016 | 2016-11-02 | P | 8.2500 | T | T | 100 | 14 | 86 | 0.2453 | 0.1924 |
513
+
514
+ #+HEADER: :colnames no
515
+ :#+BEGIN_SRC ruby :var tab=trades1
516
+ require 'fat_table'
517
+ tab = FatTable.from_aoa(tab).where('shares > 500')
518
+ tab.to_aoa
519
+ :#+END_SRC
520
+
521
+ #+RESULTS:
522
+ | Ref | Date | Code | Price | G10 | QP10 | Shares | Lp | Qp | Iplp | Ipqp |
523
+ |------|------------|------|-------|-----|------|--------|------|-------|--------|--------|
524
+ | T003 | 2016-11-01 | P | 7.5 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
525
+ | T004 | 2016-11-01 | S | 7.55 | T | F | 6811 | 966 | 5845 | 0.2453 | 0.1924 |
526
+ | T005 | 2016-11-01 | S | 7.5 | F | F | 4000 | 572 | 3428 | 0.2453 | 0.1924 |
527
+ | T006 | 2016-11-01 | S | 7.6 | F | T | 1000 | 143 | 857 | 0.2453 | 0.1924 |
528
+ | T008 | 2016-11-01 | P | 7.65 | F | F | 2771 | 393 | 2378 | 0.2453 | 0.1924 |
529
+ | T009 | 2016-11-01 | P | 7.6 | F | F | 9550 | 1363 | 8187 | 0.2453 | 0.1924 |
530
+ | T010 | 2016-11-01 | P | 7.55 | F | T | 3175 | 451 | 2724 | 0.2453 | 0.1924 |
531
+ | T012 | 2016-11-02 | P | 7.55 | F | F | 4700 | 677 | 4023 | 0.2453 | 0.1924 |
532
+ | T013 | 2016-11-02 | P | 7.35 | T | T | 53100 | 7656 | 45444 | 0.2453 | 0.1924 |
533
+ | T014 | 2016-11-02 | P | 7.45 | F | T | 5847 | 835 | 5012 | 0.2453 | 0.1924 |
534
+
535
+ This example illustrates several things:
536
+
537
+ 1. The named org-mode table, `trades1`, can be passed into a ruby code block
538
+ using the `:var tab=trades1` header argument to the code block; that makes
539
+ the variable `tab` available to the code block as an array of arrays, which
540
+ `FatTable` then uses to initialize the table.
541
+ 2. The code block requires that you set `:colnames no` in the header arguments.
542
+ This suppresses org-mode&rsquo;s own processing of the header line so that
543
+ `FatTable` can see the headers. Failure to do this will cause an error.
544
+ 3. The table is subjected to some processing, in this case selecting those rows
545
+ where the number of shares is greater than 500. More on that later.
546
+ 4. `FatTable` passes back to org-mode an array of arrays using the `.to_aoa`
547
+ method. In an `org-mode` buffer, these are rendered as tables. We&rsquo;ll often
548
+ apply `.to_aoa` at the end of example blocks to render the results inside
549
+ this `README.org` file. As we&rsquo;ll see below, this method can also take a block
550
+ to which formatting directives and footers can be attached.
551
+
552
+
553
+ <a id="org7980800"></a>
554
+
555
+ ### From Arrays of Hashes
556
+
557
+ A second ruby data structure that can be used to initialize a `FatTable` table
558
+ is an array of ruby Hashes. Each hash represents a row of the table, and the
559
+ headers of the table are take from the keys of the hashes. Accordingly, all the
560
+ hashes should have the same keys.
561
+
562
+ This same method can in fact take an array of any objects that can be converted
563
+ to a Hash with the `#to_h` method, so you can use an array of your own objects
564
+ to initialize a table, provided that you define a suitable `#to_h` method for
565
+ the objects&rsquo; class.
566
+
567
+ aoh = [
568
+ { ref: 'T001', date: '2016-11-01', code: 'P', price: '7.7000', shares: 100 },
569
+ { ref: 'T002', date: '2016-11-01', code: 'P', price: 7.7500, shares: 200 },
570
+ { ref: 'T003', date: '2016-11-01', code: 'P', price: 7.5000, shares: 800 },
571
+ { ref: 'T004', date: '2016-11-01', code: 'S', price: 7.5500, shares: 6811 },
572
+ { ref: 'T005', date: Date.today, code: 'S', price: 7.5000, shares: 4000 },
573
+ { ref: 'T006', date: '2016-11-01', code: 'S', price: 7.6000, shares: 1000 },
574
+ { ref: 'T007', date: '2016-11-01', code: 'S', price: 7.6500, shares: 200 },
575
+ { ref: 'T008', date: '2016-11-01', code: 'P', price: 7.6500, shares: 2771 },
576
+ { ref: 'T009', date: '2016-11-01', code: 'P', price: 7.6000, shares: 9550 },
577
+ { ref: 'T010', date: '2016-11-01', code: 'P', price: 7.5500, shares: 3175 },
578
+ { ref: 'T011', date: '2016-11-02', code: 'P', price: 7.4250, shares: 100 },
579
+ { ref: 'T012', date: '2016-11-02', code: 'P', price: 7.5500, shares: 4700 },
580
+ { ref: 'T013', date: '2016-11-02', code: 'P', price: 7.3500, shares: 53100 },
581
+ { ref: 'T014', date: '2016-11-02', code: 'P', price: 7.4500, shares: 5847 },
582
+ { ref: 'T015', date: '2016-11-02', code: 'P', price: 7.7500, shares: 500 },
583
+ { ref: 'T016', date: '2016-11-02', code: 'P', price: 8.2500, shares: 100 }
584
+ ]
585
+ tab = FatTable.from_aoh(aoh)
586
+
587
+ Notice, again, that the values can either be ruby objects, such as `Date.today`,
588
+ or strings that can parsed into one of the permissible column types.
589
+
590
+
591
+ <a id="orgdab2ec1"></a>
592
+
593
+ ### From SQL queries
594
+
595
+ Another way to initialize a `FatTable` table is with the results of a SQL query.
596
+ `FatTable` uses the `sequel` gem to query databases. You must first set the
597
+ database parameters to be used for the queries.
598
+
599
+ # This automatically requires sequel.
600
+ require 'fat_table'
601
+ FatTable.set_db(driver: 'Pg',
602
+ database: 'XXX_development',
603
+ user: 'dtd',
604
+ password: 'slflpowert',
605
+ host: 'localhost',
606
+ socket: '/tmp/.s.PGSQL.5432')
607
+ tab = FatTable.from_sql('select * from trades;')
608
+
609
+ Some of the parameters to the `.set_db` function have defaults. The driver
610
+ defaults to `'Pg'` for postgresql and the socket defaults to
611
+ `/tmp/.s.PGSQL.5432` if the host is &rsquo;localhost&rsquo;, which it is by default. If the
612
+ host is not `'localhost'`, the dsn uses a port rather than a socket and defaults
613
+ to port `'5432'`. While user and password default to nil, the database parameter
614
+ is required.
615
+
616
+ The `.set_db` function need only be called once, and the database handle it
617
+ creates will be used for all subsequent `.from_sql` calls until `.set_db` is
618
+ called again.
619
+
620
+ Alternatively, you can build the `Sequel` connection with `Sequel.connect` or
621
+ with adapter-specific `Sequel` connection methods and let `FatTable` know to use
622
+ that connection:
623
+
624
+ require 'fat_table'
625
+ FatTable.db = Sequel.connect('postgres://user:password@localhost/dbname')
626
+ FatTable.db = Sequel.ado(conn_string: 'Provider=Microsoft.ACE.OLEDB.12.0;Data Source=drive:\path\filename.accdb')
627
+
628
+ Consult `Sequel's` documentation for details on its connection methods.
629
+ <http://sequel.jeremyevans.net/rdoc/files/doc/opening_databases_rdoc.html>
630
+
631
+
632
+ <a id="orgeb97e36"></a>
633
+
634
+ ### Marking Groups in Input
635
+
636
+ The `.from_aoa` and `.from_aoh` functions take an optional keyword parameter
637
+ `hlines:` that, if set to `true`, causes them to mark group boundaries in the
638
+ table wherever a row Array (for `.from_aoa`) or Hash (for `.from_aoh`) is
639
+ followed by a `nil`. Each boundary means that the rows above it and after the
640
+ header or prior group boundary all belong to a group. By default `hlines` is
641
+ false for both functions so neither expects hlines in its input.
642
+
643
+ In the case of `.from_aoa`, if `hlines:` is set true, the input must also
644
+ include a `nil` in the second element of the outer array to indicate that the
645
+ first row is to be used as headers. Otherwise, it will synthesize headers of
646
+ the form `:col_1`, `:col_2`, &#x2026; `:col_n`.
647
+
648
+ In org mode table text passed to `.from_org_file` and `.from_org_string`, you
649
+ *must* mark the header row by following it with an hrule and you *may* mark
650
+ group boundaries with an hrule. In org mode tables, hlines are table rows
651
+ beginning with something like &rsquo;`|---`&rsquo;. The `.from_org_...` functions always
652
+ recognizes hlines in the input, so it takes no `hlines:` keyword parameter.
653
+
654
+
655
+ <a id="orgf9cb237"></a>
656
+
657
+ ## Accessing Parts of Tables
658
+
659
+
660
+ <a id="org4453cea"></a>
661
+
662
+ ### Rows
663
+
664
+ A `FatTable` table is an Enumerable, yielding each row of the table as a Hash
665
+ keyed on the header symbols. The method `Table#rows` returns an Array of the
666
+ rows as Hashes as well.
667
+
668
+ You can also use indexing to access a row of the table by number. Using an
669
+ integer index returns a Hash of the given row. Thus, `tab[20]` returns the 21st
670
+ data row of the table, while `tab[0]` returns the first row and tab[-1] returns
671
+ the last row.
672
+
673
+
674
+ <a id="org8a6dd85"></a>
675
+
676
+ ### Columns
677
+
678
+ If the index provided to `[]` is a string or a symbol, it returns an Array of
679
+ the items of the column with that header. Thus, `tab[:ref]` returns an Array of
680
+ all the items of the table&rsquo;s `:ref` column.
681
+
682
+
683
+ <a id="orgcc87a8b"></a>
684
+
685
+ ### Cells
686
+
687
+ The two forms of indexing can be combined to access individual cells of the
688
+ table:
689
+
690
+ tab[13] # => Hash of the 14th row
691
+ tab[:date] # => Array of all Dates in the :date column
692
+ tab[13][:date] # => The Date in the 14th row
693
+ tab[:date][13] # => The Date in the 14th row; indexes can be in either order.
694
+
695
+
696
+ <a id="org4a41de4"></a>
697
+
698
+ ### Other table attributes
699
+
700
+ tab.headers # => an Array of the headers in symbol form
701
+ tab.types # => a Hash mapping headers to column types
702
+ tab.size # => the number of rows in the table
703
+ tab.width # => the number of columns in the table
704
+ tab.empty? # => is the table empty?
705
+ tab.column?(head) # => does the table have a column with the given header?
706
+ tab.groups # => return an Array of the table's groups as Arrays of row Hashes.
707
+
708
+
709
+ <a id="org731fd13"></a>
710
+
711
+ ## Operations on Tables
712
+
713
+ Once you have one or more tables, you will likely want to perform operations on
714
+ them. The operations provided by `FatTable` are the subject of this section.
715
+ Before getting into the operations, though, there are a couple of issues that
716
+ cut across all or many of the operations.
717
+
718
+ First, tables are by and large immutable objects. Each operation creates a new
719
+ table without affecting the input tables. The only exception is the `degroup!`
720
+ operation, which mutates the receiver table by removing its group boundaries.
721
+
722
+ Second, because each operation returns a `FatTable::Table` object, the
723
+ operations are chainable.
724
+
725
+ Third, `FatTable::Table` objects can have &ldquo;groups&rdquo; of rows within the table.
726
+ These can be decorated with hlines and group footers on output. Some of these
727
+ operations result in marking group boundaries in the result table, others remove
728
+ group boundaries that may have existed in the input table. Operations that
729
+ either create or remove groups will be noted below.
730
+
731
+ Finally, the operations are for the most part patterned on SQL table operations,
732
+ but when expressions play a role, you write them using ruby syntax rather than
733
+ SQL.
734
+
735
+
736
+ <a id="orga96ca08"></a>
737
+
738
+ ### Example Input Table
739
+
740
+ For illustration purposes assume that the following tables are read into ruby
741
+ variables called &rsquo;`tab1`&rsquo; and &rsquo;`tab2`. We have given the table groups, marked by
742
+ the hlines below, and included some duplicate rows to illustrate the effect of
743
+ certain operations on groups and duplicates.
744
+
745
+ require 'fat_table'
746
+
747
+ tab1_str = <<-EOS
748
+ | Ref | Date | Code | Price | G10 | QP10 | Shares | LP | QP | IPLP | IPQP |
749
+ |------|------------------|------|--------|-----|------|--------|------|-------|--------|--------|
750
+ | T001 | [2016-11-01 Tue] | P | 7.7000 | T | F | 100 | 14 | 86 | 0.2453 | 0.1924 |
751
+ | T002 | [2016-11-01 Tue] | P | 7.7500 | T | F | 200 | 28 | 172 | 0.2453 | 0.1924 |
752
+ | T003 | [2016-11-01 Tue] | P | 7.5000 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
753
+ | T003 | [2016-11-01 Tue] | P | 7.5000 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
754
+ |------|------------------|------|--------|-----|------|--------|------|-------|--------|--------|
755
+ | T004 | [2016-11-01 Tue] | S | 7.5500 | T | F | 6811 | 966 | 5845 | 0.2453 | 0.1924 |
756
+ | T005 | [2016-11-01 Tue] | S | 7.5000 | F | F | 4000 | 572 | 3428 | 0.2453 | 0.1924 |
757
+ | T006 | [2016-11-01 Tue] | S | 7.6000 | F | T | 1000 | 143 | 857 | 0.2453 | 0.1924 |
758
+ | T006 | [2016-11-01 Tue] | S | 7.6000 | F | T | 1000 | 143 | 857 | 0.2453 | 0.1924 |
759
+ | T007 | [2016-11-01 Tue] | S | 7.6500 | T | F | 200 | 28 | 172 | 0.2453 | 0.1924 |
760
+ | T008 | [2016-11-01 Tue] | P | 7.6500 | F | F | 2771 | 393 | 2378 | 0.2453 | 0.1924 |
761
+ | T009 | [2016-11-01 Tue] | P | 7.6000 | F | F | 9550 | 1363 | 8187 | 0.2453 | 0.1924 |
762
+ |------|------------------|------|--------|-----|------|--------|------|-------|--------|--------|
763
+ | T010 | [2016-11-01 Tue] | P | 7.5500 | F | T | 3175 | 451 | 2724 | 0.2453 | 0.1924 |
764
+ | T011 | [2016-11-02 Wed] | P | 7.4250 | T | F | 100 | 14 | 86 | 0.2453 | 0.1924 |
765
+ | T012 | [2016-11-02 Wed] | P | 7.5500 | F | F | 4700 | 677 | 4023 | 0.2453 | 0.1924 |
766
+ | T012 | [2016-11-02 Wed] | P | 7.5500 | F | F | 4700 | 677 | 4023 | 0.2453 | 0.1924 |
767
+ | T013 | [2016-11-02 Wed] | P | 7.3500 | T | T | 53100 | 7656 | 45444 | 0.2453 | 0.1924 |
768
+ |------|------------------|------|--------|-----|------|--------|------|-------|--------|--------|
769
+ | T014 | [2016-11-02 Wed] | P | 7.4500 | F | T | 5847 | 835 | 5012 | 0.2453 | 0.1924 |
770
+ | T015 | [2016-11-02 Wed] | P | 7.7500 | F | F | 500 | 72 | 428 | 0.2453 | 0.1924 |
771
+ | T016 | [2016-11-02 Wed] | P | 8.2500 | T | T | 100 | 14 | 86 | 0.2453 | 0.1924 |
772
+ EOS
773
+
774
+ tab2_str = <<-EOS
775
+ | Ref | Date | Code | Price | G10 | QP10 | Shares | LP | QP | IPLP | IPQP |
776
+ |------|------------------|------|--------|-----|------|--------|-------|------|--------|--------|
777
+ | T003 | [2016-11-01 Tue] | P | 7.5000 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
778
+ | T003 | [2016-11-01 Tue] | P | 7.5000 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
779
+ | T017 | [2016-11-01 Tue] | P | 8.3 | F | T | 1801 | 1201 | 600 | 0.2453 | 0.1924 |
780
+ |------|------------------|------|--------|-----|------|--------|-------|------|--------|--------|
781
+ | T018 | [2016-11-01 Tue] | S | 7.152 | T | F | 2516 | 2400 | 116 | 0.2453 | 0.1924 |
782
+ | T018 | [2016-11-01 Tue] | S | 7.152 | T | F | 2516 | 2400 | 116 | 0.2453 | 0.1924 |
783
+ | T006 | [2016-11-01 Tue] | S | 7.6000 | F | T | 1000 | 143 | 857 | 0.2453 | 0.1924 |
784
+ | T007 | [2016-11-01 Tue] | S | 7.6500 | T | F | 200 | 28 | 172 | 0.2453 | 0.1924 |
785
+ |------|------------------|------|--------|-----|------|--------|-------|------|--------|--------|
786
+ | T014 | [2016-11-02 Wed] | P | 7.4500 | F | T | 5847 | 835 | 5012 | 0.2453 | 0.1924 |
787
+ | T015 | [2016-11-02 Wed] | P | 7.7500 | F | F | 500 | 72 | 428 | 0.2453 | 0.1924 |
788
+ | T015 | [2016-11-02 Wed] | P | 7.7500 | F | F | 500 | 72 | 428 | 0.2453 | 0.1924 |
789
+ | T016 | [2016-11-02 Wed] | P | 8.2500 | T | T | 100 | 14 | 86 | 0.2453 | 0.1924 |
790
+ |------|------------------|------|--------|-----|------|--------|-------|------|--------|--------|
791
+ | T019 | [2017-01-15 Sun] | S | 8.75 | T | F | 300 | 175 | 125 | 0.2453 | 0.1924 |
792
+ | T020 | [2017-01-19 Thu] | S | 8.25 | F | T | 700 | 615 | 85 | 0.2453 | 0.1924 |
793
+ | T021 | [2017-01-23 Mon] | P | 7.16 | T | T | 12100 | 11050 | 1050 | 0.2453 | 0.1924 |
794
+ | T021 | [2017-01-23 Mon] | P | 7.16 | T | T | 12100 | 11050 | 1050 | 0.2453 | 0.1924 |
795
+ EOS
796
+
797
+ tab1 = FatTable.from_org_string(tab1_str)
798
+ tab2 = FatTable.from_org_string(tab2_str)
799
+
800
+
801
+ <a id="orga0c49b3"></a>
802
+
803
+ ### Select
804
+
805
+ With the `select` method, you can select which existing columns should appear in
806
+ the output table and create new columns in the output table that are a function
807
+ of existing and new columns.
808
+
809
+ 1. Selecting Existing Columns
810
+
811
+ Here we select three existing columns by simply passing header symbols in the
812
+ order we want them to appear in the output. Thus, one use of `select` is to
813
+ filter and permute the order of existing columns. The `select` method preserves
814
+ any group boundaries present in the input table.
815
+
816
+ tab1.select(:price, :ref, :shares).to_aoa
817
+
818
+ | Price | Ref | Shares |
819
+ |-------|------|--------|
820
+ | 7.7 | T001 | 100 |
821
+ | 7.75 | T002 | 200 |
822
+ | 7.5 | T003 | 800 |
823
+ | 7.5 | T003 | 800 |
824
+ |-------|------|--------|
825
+ | 7.55 | T004 | 6811 |
826
+ | 7.5 | T005 | 4000 |
827
+ | 7.6 | T006 | 1000 |
828
+ | 7.6 | T006 | 1000 |
829
+ | 7.65 | T007 | 200 |
830
+ | 7.65 | T008 | 2771 |
831
+ | 7.6 | T009 | 9550 |
832
+ |-------|------|--------|
833
+ | 7.55 | T010 | 3175 |
834
+ | 7.425 | T011 | 100 |
835
+ | 7.55 | T012 | 4700 |
836
+ | 7.55 | T012 | 4700 |
837
+ | 7.35 | T013 | 53100 |
838
+ |-------|------|--------|
839
+ | 7.45 | T014 | 5847 |
840
+ | 7.75 | T015 | 500 |
841
+ | 8.25 | T016 | 100 |
842
+
843
+ 2. Adding New Columns
844
+
845
+ More interesting is that `select` can take hash-like keyword arguments after the
846
+ symbol arguments to create new columns in the output as functions of other
847
+ columns. For each hash-like parameter, the keyword given must be a symbol, which
848
+ becomes the header for the new column, and the value must be either: (1) a
849
+ symbol representing an existing column, which has the effect of renaming an
850
+ existing column, or (2) a string representing a ruby expression for the value of
851
+ a new column.
852
+
853
+ Within the string expression, the names of existing or already-specified columns
854
+ are available as local variables, as well as the instance variables &rsquo;@row&rsquo; and
855
+ &rsquo;@group&rsquo;. So for our example table, the string expressions for new columns have
856
+ access to local variables `ref`, `date`, `code`, `price`, `g10`, `qp10`,
857
+ `shares`, `lp`, `qp`, `iplp`, and `ipqp` as well as the instance variables
858
+ `@row` and `@group`. The local variables are set to the values of the cell in
859
+ their respective columns for each row in the input table and the instance
860
+ variables are set the number of the current row and group respectively.
861
+
862
+ For example, if we want to rename the `:date` column and add a new column to
863
+ compute the cost of shares, we could do the following:
864
+
865
+ tab1.select(:ref, :price, :shares, traded_on: :date, cost: 'price * shares').to_aoa
866
+
867
+ | Ref | Price | Shares | Traded On | Cost |
868
+ |------|-------|--------|------------|----------|
869
+ | T001 | 7.7 | 100 | 2016-11-01 | 770.0 |
870
+ | T002 | 7.75 | 200 | 2016-11-01 | 1550.0 |
871
+ | T003 | 7.5 | 800 | 2016-11-01 | 6000.0 |
872
+ | T003 | 7.5 | 800 | 2016-11-01 | 6000.0 |
873
+ |------|-------|--------|------------|----------|
874
+ | T004 | 7.55 | 6811 | 2016-11-01 | 51423.05 |
875
+ | T005 | 7.5 | 4000 | 2016-11-01 | 30000.0 |
876
+ | T006 | 7.6 | 1000 | 2016-11-01 | 7600.0 |
877
+ | T006 | 7.6 | 1000 | 2016-11-01 | 7600.0 |
878
+ | T007 | 7.65 | 200 | 2016-11-01 | 1530.0 |
879
+ | T008 | 7.65 | 2771 | 2016-11-01 | 21198.15 |
880
+ | T009 | 7.6 | 9550 | 2016-11-01 | 72580.0 |
881
+ |------|-------|--------|------------|----------|
882
+ | T010 | 7.55 | 3175 | 2016-11-01 | 23971.25 |
883
+ | T011 | 7.425 | 100 | 2016-11-02 | 742.5 |
884
+ | T012 | 7.55 | 4700 | 2016-11-02 | 35485.0 |
885
+ | T012 | 7.55 | 4700 | 2016-11-02 | 35485.0 |
886
+ | T013 | 7.35 | 53100 | 2016-11-02 | 390285.0 |
887
+ |------|-------|--------|------------|----------|
888
+ | T014 | 7.45 | 5847 | 2016-11-02 | 43560.15 |
889
+ | T015 | 7.75 | 500 | 2016-11-02 | 3875.0 |
890
+ | T016 | 8.25 | 100 | 2016-11-02 | 825.0 |
891
+
892
+ The parameter &rsquo;`traded_on: :date`&rsquo; caused the `:date` column of the input table
893
+ to be renamed &rsquo;`:traded_on`, and the parameter `cost: 'price * shares'` created
894
+ a new column, `:cost`, as the product of values in the `:price` and `:shares`
895
+ columns.
896
+
897
+ The order of the columns in the result tables is the same as the order of the
898
+ parameters to the `select` method. So, you can re-order the columns with a
899
+ second, chained call to `select`:
900
+
901
+ tab1.select(:ref, :price, :shares, traded_on: :date, cost: 'price * shares') \
902
+ .select(:ref, :traded_on, :price, :shares, :cost) \
903
+ .to_aoa
904
+
905
+ | Ref | Traded On | Price | Shares | Cost |
906
+ |------|------------|-------|--------|----------|
907
+ | T001 | 2016-11-01 | 7.7 | 100 | 770.0 |
908
+ | T002 | 2016-11-01 | 7.75 | 200 | 1550.0 |
909
+ | T003 | 2016-11-01 | 7.5 | 800 | 6000.0 |
910
+ | T003 | 2016-11-01 | 7.5 | 800 | 6000.0 |
911
+ |------|------------|-------|--------|----------|
912
+ | T004 | 2016-11-01 | 7.55 | 6811 | 51423.05 |
913
+ | T005 | 2016-11-01 | 7.5 | 4000 | 30000.0 |
914
+ | T006 | 2016-11-01 | 7.6 | 1000 | 7600.0 |
915
+ | T006 | 2016-11-01 | 7.6 | 1000 | 7600.0 |
916
+ | T007 | 2016-11-01 | 7.65 | 200 | 1530.0 |
917
+ | T008 | 2016-11-01 | 7.65 | 2771 | 21198.15 |
918
+ | T009 | 2016-11-01 | 7.6 | 9550 | 72580.0 |
919
+ |------|------------|-------|--------|----------|
920
+ | T010 | 2016-11-01 | 7.55 | 3175 | 23971.25 |
921
+ | T011 | 2016-11-02 | 7.425 | 100 | 742.5 |
922
+ | T012 | 2016-11-02 | 7.55 | 4700 | 35485.0 |
923
+ | T012 | 2016-11-02 | 7.55 | 4700 | 35485.0 |
924
+ | T013 | 2016-11-02 | 7.35 | 53100 | 390285.0 |
925
+ |------|------------|-------|--------|----------|
926
+ | T014 | 2016-11-02 | 7.45 | 5847 | 43560.15 |
927
+ | T015 | 2016-11-02 | 7.75 | 500 | 3875.0 |
928
+ | T016 | 2016-11-02 | 8.25 | 100 | 825.0 |
929
+
930
+ 3. Custom Instance Variables and Hooks
931
+
932
+ As the above examples demonstrate, the instance variables `@row` and `@group`
933
+ are available when evaluating expressions that add new columns. You can also set
934
+ up your own instance variables as well for keeping track of things that cross
935
+ row boundaries, such as running sums.
936
+
937
+ To declare instance variables, you can use the `ivars:` hash parameter to
938
+ `select`. Each key of the hash becomes an instance variable and each value
939
+ becomes its initial value before any rows are evaluated.
940
+
941
+ In addition, you can provide `before_hook:` and `after_hook:` parameters to
942
+ `select` as strings that are evaluated as ruby expressions before and after each
943
+ row is processed. You can use these to update instance variables. The values set
944
+ in the `before_hook:` can be used in expressions for adding new columns by
945
+ referencing them with the &rsquo;@&rsquo; prefix.
946
+
947
+ For example, suppose we wanted to not only add a cost column, but a column that
948
+ shows the cumulative cost after each transaction in our example table. The
949
+ following example uses the `ivars:` and `before_hook:` parameters to keep track
950
+ of the running cost of shares, then formats the table.
951
+
952
+ tab = tab1.select(:ref, :price, :shares, traded_on: :date, \
953
+ cost: 'price * shares', cumulative: '@total_cost', \
954
+ ivars: { total_cost: 0 }, \
955
+ before_hook: '@total_cost += price * shares')
956
+ FatTable.to_aoa(tab) do |f|
957
+ f.format(price: '0.4', shares: '0.0,', cost: '0.2,', cumulative: '0.2,')
958
+ end
959
+
960
+ | Ref | Price | Shares | Traded On | Cost | Cumulative |
961
+ |------|--------|--------|------------|------------|------------|
962
+ | T001 | 7.7000 | 100 | 2016-11-01 | 770.00 | 770.00 |
963
+ | T002 | 7.7500 | 200 | 2016-11-01 | 1,550.00 | 2,320.00 |
964
+ | T003 | 7.5000 | 800 | 2016-11-01 | 6,000.00 | 8,320.00 |
965
+ | T003 | 7.5000 | 800 | 2016-11-01 | 6,000.00 | 14,320.00 |
966
+ |------|--------|--------|------------|------------|------------|
967
+ | T004 | 7.5500 | 6,811 | 2016-11-01 | 51,423.05 | 65,743.05 |
968
+ | T005 | 7.5000 | 4,000 | 2016-11-01 | 30,000.00 | 95,743.05 |
969
+ | T006 | 7.6000 | 1,000 | 2016-11-01 | 7,600.00 | 103,343.05 |
970
+ | T006 | 7.6000 | 1,000 | 2016-11-01 | 7,600.00 | 110,943.05 |
971
+ | T007 | 7.6500 | 200 | 2016-11-01 | 1,530.00 | 112,473.05 |
972
+ | T008 | 7.6500 | 2,771 | 2016-11-01 | 21,198.15 | 133,671.20 |
973
+ | T009 | 7.6000 | 9,550 | 2016-11-01 | 72,580.00 | 206,251.20 |
974
+ |------|--------|--------|------------|------------|------------|
975
+ | T010 | 7.5500 | 3,175 | 2016-11-01 | 23,971.25 | 230,222.45 |
976
+ | T011 | 7.4250 | 100 | 2016-11-02 | 742.50 | 230,964.95 |
977
+ | T012 | 7.5500 | 4,700 | 2016-11-02 | 35,485.00 | 266,449.95 |
978
+ | T012 | 7.5500 | 4,700 | 2016-11-02 | 35,485.00 | 301,934.95 |
979
+ | T013 | 7.3500 | 53,100 | 2016-11-02 | 390,285.00 | 692,219.95 |
980
+ |------|--------|--------|------------|------------|------------|
981
+ | T014 | 7.4500 | 5,847 | 2016-11-02 | 43,560.15 | 735,780.10 |
982
+ | T015 | 7.7500 | 500 | 2016-11-02 | 3,875.00 | 739,655.10 |
983
+ | T016 | 8.2500 | 100 | 2016-11-02 | 825.00 | 740,480.10 |
984
+
985
+ 4. Argument Order and Boundaries
986
+
987
+ Notice that `select` can take any number of arguments but all the symbol
988
+ arguments must come first followed by all the hash-like keyword arguments,
989
+ including the special arguments for instance variables and hooks.
990
+
991
+ As the example illustrates, `.select` transmits any group boundaries in its
992
+ input table to the result table.
993
+
994
+
995
+ <a id="orge185ad7"></a>
996
+
997
+ ### Where
998
+
999
+ You can filter the rows of the result table with the `.where` method. It takes a
1000
+ single string expression as an argument which is evaluated in a manner similar
1001
+ to `.select` in which the value of the cells in each column are available as
1002
+ local variables and the instance variables `@row` and `@group` are available for
1003
+ testing. The expression is evaluated for each row, and if the expression
1004
+ evaluates to a truthy value, the row is included in the output, otherwise it is
1005
+ not. The `.where` method obliterates any group boundaries in the input, so the
1006
+ output table has only a single group.
1007
+
1008
+ Here we select only those even-numbered rows where either of the two boolean
1009
+ fields is true:
1010
+
1011
+ tab1.where('@row.even? && (g10 || qp10)') \
1012
+ .to_aoa
1013
+
1014
+ | Ref | Date | Code | Price | G10 | QP10 | Shares | Lp | Qp | Iplp | Ipqp |
1015
+ |------|------------|------|-------|-----|------|--------|------|-------|--------|--------|
1016
+ | T002 | 2016-11-01 | P | 7.75 | T | F | 200 | 28 | 172 | 0.2453 | 0.1924 |
1017
+ | T003 | 2016-11-01 | P | 7.5 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
1018
+ | T006 | 2016-11-01 | S | 7.6 | F | T | 1000 | 143 | 857 | 0.2453 | 0.1924 |
1019
+ | T010 | 2016-11-01 | P | 7.55 | F | T | 3175 | 451 | 2724 | 0.2453 | 0.1924 |
1020
+ | T013 | 2016-11-02 | P | 7.35 | T | T | 53100 | 7656 | 45444 | 0.2453 | 0.1924 |
1021
+
1022
+
1023
+ <a id="org57f51d1"></a>
1024
+
1025
+ ### Order\_by
1026
+
1027
+ You can sort a table on any number of columns with `order_by`. The `order_by`
1028
+ method takes any number of symbol arguments for the columns to sort on. If you
1029
+ specify more than one column, the sort is performed on the first column, then
1030
+ all columns that are equal with respect to the first column are sorted by the
1031
+ second column, and so on. All columns of the input table are included in the
1032
+ output.
1033
+
1034
+ Let&rsquo;s sort our table first by `:code`, then by `:date`.
1035
+
1036
+ tab1.order_by(:code, :date) \
1037
+ .to_aoa
1038
+
1039
+ | Ref | Date | Code | Price | G10 | QP10 | Shares | Lp | Qp | Iplp | Ipqp |
1040
+ |------|------------|------|-------|-----|------|--------|------|-------|--------|--------|
1041
+ | T001 | 2016-11-01 | P | 7.7 | T | F | 100 | 14 | 86 | 0.2453 | 0.1924 |
1042
+ | T002 | 2016-11-01 | P | 7.75 | T | F | 200 | 28 | 172 | 0.2453 | 0.1924 |
1043
+ | T003 | 2016-11-01 | P | 7.5 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
1044
+ | T003 | 2016-11-01 | P | 7.5 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
1045
+ | T008 | 2016-11-01 | P | 7.65 | F | F | 2771 | 393 | 2378 | 0.2453 | 0.1924 |
1046
+ | T009 | 2016-11-01 | P | 7.6 | F | F | 9550 | 1363 | 8187 | 0.2453 | 0.1924 |
1047
+ | T010 | 2016-11-01 | P | 7.55 | F | T | 3175 | 451 | 2724 | 0.2453 | 0.1924 |
1048
+ |------|------------|------|-------|-----|------|--------|------|-------|--------|--------|
1049
+ | T011 | 2016-11-02 | P | 7.425 | T | F | 100 | 14 | 86 | 0.2453 | 0.1924 |
1050
+ | T012 | 2016-11-02 | P | 7.55 | F | F | 4700 | 677 | 4023 | 0.2453 | 0.1924 |
1051
+ | T012 | 2016-11-02 | P | 7.55 | F | F | 4700 | 677 | 4023 | 0.2453 | 0.1924 |
1052
+ | T013 | 2016-11-02 | P | 7.35 | T | T | 53100 | 7656 | 45444 | 0.2453 | 0.1924 |
1053
+ | T014 | 2016-11-02 | P | 7.45 | F | T | 5847 | 835 | 5012 | 0.2453 | 0.1924 |
1054
+ | T015 | 2016-11-02 | P | 7.75 | F | F | 500 | 72 | 428 | 0.2453 | 0.1924 |
1055
+ | T016 | 2016-11-02 | P | 8.25 | T | T | 100 | 14 | 86 | 0.2453 | 0.1924 |
1056
+ |------|------------|------|-------|-----|------|--------|------|-------|--------|--------|
1057
+ | T004 | 2016-11-01 | S | 7.55 | T | F | 6811 | 966 | 5845 | 0.2453 | 0.1924 |
1058
+ | T005 | 2016-11-01 | S | 7.5 | F | F | 4000 | 572 | 3428 | 0.2453 | 0.1924 |
1059
+ | T006 | 2016-11-01 | S | 7.6 | F | T | 1000 | 143 | 857 | 0.2453 | 0.1924 |
1060
+ | T006 | 2016-11-01 | S | 7.6 | F | T | 1000 | 143 | 857 | 0.2453 | 0.1924 |
1061
+ | T007 | 2016-11-01 | S | 7.65 | T | F | 200 | 28 | 172 | 0.2453 | 0.1924 |
1062
+
1063
+ The interesting thing about `order_by` is that, while it ignores groups in its
1064
+ input, it adds group boundaries in the output table at those rows where the sort
1065
+ keys change. Thus, in each group, `:code` and `:date` are the same, and when
1066
+ either changes, `order_by` inserts a group boundary.
1067
+
1068
+
1069
+ <a id="org1ee0a85"></a>
1070
+
1071
+ ### Group\_by
1072
+
1073
+ Like `order_by`, `group_by` takes a set of parameters of column header symbols,
1074
+ the &ldquo;grouping parameters&rdquo;, by which to sort the table into a set of groups that
1075
+ are equal with respect to values in those columns. In addition, those parameters
1076
+ can be followed by a series of hash-like parameters, the &ldquo;aggregating
1077
+ parameters&rdquo;, that indicate how any of the remaining, non-group columns are to be
1078
+ aggregated into a single value. The output table has one row for each group for
1079
+ which the grouping parameters are equal containing those columns and an
1080
+ aggregate column for each of the aggregating parameters.
1081
+
1082
+ For example, let&rsquo;s summarize the `trades` table by `:code` and `:price` again,
1083
+ and determine total shares, average price, and a few other features of each
1084
+ group:
1085
+
1086
+ tab1.group_by(:code, :date, price: :avg,
1087
+ shares: :sum, lp: :sum, qp: :sum,
1088
+ qp10: :all?) \
1089
+ .to_aoa { |f| f.format(avg_price: '0.5R') }
1090
+
1091
+ | Code | Date | Avg Price | Sum Shares | Sum Lp | Sum Qp | All QP10 |
1092
+ |------|------------|-----------|------------|--------|--------|----------|
1093
+ | P | 2016-11-01 | 7.60714 | 17396 | 2473 | 14923 | F |
1094
+ | P | 2016-11-02 | 7.61786 | 69047 | 9945 | 59102 | F |
1095
+ | S | 2016-11-01 | 7.58000 | 13011 | 1852 | 11159 | F |
1096
+
1097
+ After the grouping column parameters, `:code` and `:date`, there are several
1098
+ hash-like &ldquo;aggregating&rdquo; parameters where the key is the column to aggregate and
1099
+ the value is a symbol for one of several aggregating methods that
1100
+ `FatTable::Column` objects understand. For example, the `:avg` method is applied
1101
+ to the :price column so that the output shows the average price in each group.
1102
+ The `:shares`, `:lp`, and `:qp` columns are summed, and the `:any?` aggregate is
1103
+ applied to one of the boolean fields, that is, it is `true` if any of the values
1104
+ in that column are `true`. The column names in the output of the aggregated
1105
+ columns have the name of the aggregating method pre-pended to the column name.
1106
+
1107
+ Here is a list of all the aggregate methods available. If the description
1108
+ restricts the aggregate to particular column types, applying it to other types
1109
+ will raise an exception.
1110
+
1111
+ - **`first`:** the first non-nil item in the column,
1112
+ - **`last`:** the last non-nil item in the column,
1113
+ - **`rng`:** form a string of the form `"#{first}..#{last}"` to show the range of
1114
+ values in the column,
1115
+ - **`sum`:** for `Numeric` and `String` columns, apply &rsquo;+&rsquo; to all the non-nil
1116
+ values,
1117
+ - **`count`:** the number of non-nil values in the column,
1118
+ - **`min`:** for `Numeric`, `String`, and `DateTime` columns, return the smallest
1119
+ non-nil value in the column,
1120
+ - **`max`:** for `Numeric`, `String`, and `DateTime` columns, return the largest
1121
+ non-nil value in the column,
1122
+ - **`avg`:** for `Numeric` and `DateTime` columns, return the arithmetic mean of
1123
+ the non-nil values in the column; with respect to `Date` or `DateTime`
1124
+ objects, each is converted to a numeric Julian date, the average is
1125
+ calculated, and the result converted back to a `Date` or `DateTime` object,
1126
+ - **`var`:** for `Numeric` and `DateTime` columns, compute the sample variance of
1127
+ the non-nil values in the column, dates are converted to Julian date
1128
+ numbers as for the `:avg` aggregate,
1129
+ - **`pvar`:** for `Numeric` and `DateTime` columns, compute the population
1130
+ variance of the non-nil values in the column, dates are converted to Julian
1131
+ date numbers as for the `:avg` aggregate,
1132
+ - **`dev`:** for `Numeric` and `DateTime` columns, compute the sample standard
1133
+ deviation of the non-nil values in the column, dates are converted to
1134
+ Julian date numbers as for the `:avg` aggregate,
1135
+ - **`pdev`:** for `Numeric` and `DateTime` columns, compute the population
1136
+ standard deviation of the non-nil values in the column, dates are converted
1137
+ to numbers as for the `:avg` aggregate,
1138
+ - **`all?`:** for `Boolean` columns only, return true if all of the non-nil values
1139
+ in the column are true,
1140
+ - **`any?`:** for `Boolean` columns only, return true if any non-nil value in the
1141
+ column is true,
1142
+ - **`none?`:** for `Boolean` columns only, return true if no non-nil value in the
1143
+ column is true,
1144
+ - **`one?`:** for `Boolean` columns only, return true if exactly one non-nil value
1145
+ in the column is true,
1146
+
1147
+ Perhaps surprisingly, the `group_by` method ignores any groups in its input and
1148
+ results in no group boundaries in the output since each group formed by the
1149
+ implicit `order_by` on the grouping columns is collapsed into a single row.
1150
+
1151
+
1152
+ <a id="org6432f26"></a>
1153
+
1154
+ ### Join
1155
+
1156
+ 1. Join Types
1157
+
1158
+ So far, all the operations have operated on a single table. `FatTable` provides
1159
+ several `join` methods for combining two tables, each of which takes as
1160
+ parameters (1) a second table and (2) except in the case of `cross_join`, zero
1161
+ or more &ldquo;join expressions&rdquo;. In the descriptions below, `T1` is the table on
1162
+ which the method is called, `T2` is the table supplied as the first parameter
1163
+ `other`, and `R1` and `R2` are rows in their respective tables being considered
1164
+ for inclusion in the joined output table.
1165
+
1166
+ - **`join(other, *jexps)`:** Performs an &ldquo;inner join&rdquo; on the tables. For each row
1167
+ `R1` of `T1`, the joined table has a row for each row in `T2` that
1168
+ satisfies the join condition with `R1`.
1169
+
1170
+ - **`left_join(other, *jexps)`:** First, an inner join is performed. Then, for
1171
+ each row in `T1` that does not satisfy the join condition with any row in
1172
+ `T2`, a joined row is added with null values in columns of `T2`. Thus, the
1173
+ joined table always has at least one row for each row in `T1`.
1174
+
1175
+ - **`right_join(other, *jexps)`:** First, an inner join is performed. Then, for
1176
+ each row in `T2` that does not satisfy the join condition with any row in
1177
+ `T1`, a joined row is added with null values in columns of `T1`. This is
1178
+ the converse of a left join: the result table will always have a row for
1179
+ each row in `T2`.
1180
+
1181
+ - **`full_join(other, *jexps)`:** First, an inner join is performed. Then, for
1182
+ each row in `T1` that does not satisfy the join condition with any row in
1183
+ `T2`, a joined row is added with null values in columns of `T2`. Also, for
1184
+ each row of `T2` that does not satisfy the join condition with any row in
1185
+ `T1`, a joined row with null values in the columns of `T1` is added.
1186
+
1187
+ - **`cross_join(other)`:** For every possible combination of rows from `T1` and
1188
+ `T2` (i.e., a Cartesian product), the joined table will contain a row
1189
+ consisting of all columns in `T1` followed by all columns in `T2`. If the
1190
+ tables have `N` and `M` rows respectively, the joined table will have `N *
1191
+ M` rows.
1192
+
1193
+ 2. Join Expressions
1194
+
1195
+ For each of the join types, if no join expressions are given, the tables will be
1196
+ joined on columns having the same column header in both tables, and the join
1197
+ condition is satisfied when all the values in those columns are equal. If the
1198
+ join type is an inner join, this is a so-called &ldquo;natural&rdquo; join.
1199
+
1200
+ If the join expressions are one or more symbols, the join condition requires
1201
+ that the values of both tables are equal for all columns named by the symbols. A
1202
+ column that appears in both tables can be given without modification and will be
1203
+ assumed to require equality on that column. If an unmodified symbol is not a
1204
+ name that appears in both tables, an exception will be raised. Column names that
1205
+ are unique to the first table must have a `_a` appended to the column name and
1206
+ column names that are unique to the other table must have a `_b` appended to the
1207
+ column name. These disambiguated column names must come in pairs, one for the
1208
+ first table and one for the second, and they will imply a join condition that
1209
+ the columns must be equal on those columns. Several such symbol expressions will
1210
+ require that all such implied pairs are equal in order for the join condition to
1211
+ be met.
1212
+
1213
+ Finally, a join expression can be a string that contains an arbitrary ruby
1214
+ expression that will be evaluated for truthiness. Within the string, *all*
1215
+ column names must be disambiguated with the `_a` or `_b` modifiers whether they
1216
+ are common to both tables or not. As with `select` and `where` methods, the
1217
+ names of the columns in both tables (albeit disambiguated) are available as
1218
+ local variables within the expression, but the instance variables `@row` and
1219
+ `@group` are not.
1220
+
1221
+ 3. Join Examples
1222
+
1223
+ The following examples are taken from the [Postgresql tutorial](https://www.tutorialspoint.com/postgresql/postgresql_using_joins.htm), with some slight
1224
+ modifications. The examples will use the following two tables, which are also
1225
+ available in `ft_console` as `@tab_a` and `@tab_b`:
1226
+
1227
+ require 'fat_table'
1228
+
1229
+ tab_a_str = <<-EOS
1230
+ | Id | Name | Age | Address | Salary | Join Date |
1231
+ |----|-------|-----|------------|--------|------------|
1232
+ | 1 | Paul | 32 | California | 20000 | 2001-07-13 |
1233
+ | 3 | Teddy | 23 | Norway | 20000 | 2007-12-13 |
1234
+ | 4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13 |
1235
+ | 5 | David | 27 | Texas | 85000 | 2007-12-13 |
1236
+ | 2 | Allen | 25 | Texas | | 2005-07-13 |
1237
+ | 8 | Paul | 24 | Houston | 20000 | 2005-07-13 |
1238
+ | 9 | James | 44 | Norway | 5000 | 2005-07-13 |
1239
+ | 10 | James | 45 | Texas | 5000 | |
1240
+ EOS
1241
+
1242
+ tab_b_str = <<-EOS
1243
+ | Id | Dept | Emp Id |
1244
+ |----|-------------|--------|
1245
+ | 1 | IT Billing | 1 |
1246
+ | 2 | Engineering | 2 |
1247
+ | 3 | Finance | 7 |
1248
+ EOS
1249
+
1250
+ tab_a = FatTable.from_org_string(tab_a_str)
1251
+ tab_b = FatTable.from_org_string(tab_b_str)
1252
+
1253
+ 1. Inner Joins
1254
+
1255
+ With no join expression arguments, the tables are joined when their sole common
1256
+ field, `:id`, is equal in both tables. The result is the natural join of the
1257
+ two tables.
1258
+
1259
+ tab_a.join(tab_b).to_aoa
1260
+
1261
+ | Id | Name | Age | Address | Salary | Join Date | Dept | Emp Id |
1262
+ |----|-------|-----|------------|--------|------------|-------------|--------|
1263
+ | 1 | Paul | 32 | California | 20000 | 2001-07-13 | IT Billing | 1 |
1264
+ | 3 | Teddy | 23 | Norway | 20000 | 2007-12-13 | Finance | 7 |
1265
+ | 2 | Allen | 25 | Texas | | 2005-07-13 | Engineering | 2 |
1266
+
1267
+ But the natural join joined employee IDs in the first table and department IDs
1268
+ in the second table. To correct this, we need to explicitly state the columns we
1269
+ want to join on in each table by disambiguating them with `_a` and `_b`
1270
+ suffixes:
1271
+
1272
+ tab_a.join(tab_b, :id_a, :emp_id_b).to_aoa
1273
+
1274
+ | Id | Name | Age | Address | Salary | Join Date | Id B | Dept |
1275
+ |----|-------|-----|------------|--------|------------|------|-------------|
1276
+ | 1 | Paul | 32 | California | 20000 | 2001-07-13 | 1 | IT Billing |
1277
+ | 2 | Allen | 25 | Texas | | 2005-07-13 | 2 | Engineering |
1278
+
1279
+ Instead of using the disambiguated column names as symbols, we could also use a
1280
+ string containing a ruby expression. Within the expression, the column names
1281
+ should be treated as local variables:
1282
+
1283
+ tab_a.join(tab_b, 'id_a == emp_id_b').to_aoa
1284
+
1285
+ | Id | Name | Age | Address | Salary | Join Date | Id B | Dept | Emp Id |
1286
+ |----|-------|-----|------------|--------|------------|------|-------------|--------|
1287
+ | 1 | Paul | 32 | California | 20000 | 2001-07-13 | 1 | IT Billing | 1 |
1288
+ | 2 | Allen | 25 | Texas | | 2005-07-13 | 2 | Engineering | 2 |
1289
+
1290
+ 2. Left and Right Joins
1291
+
1292
+ In left join, all the rows of `tab_a` are included in the output, augmented by
1293
+ the matching columns of `tab_b` and augmented with nils where there is no match:
1294
+
1295
+ tab_a.left_join(tab_b, 'id_a == emp_id_b').to_aoa
1296
+
1297
+ | Id | Name | Age | Address | Salary | Join Date | Id B | Dept | Emp Id |
1298
+ |----|-------|-----|------------|--------|------------|------|-------------|--------|
1299
+ | 1 | Paul | 32 | California | 20000 | 2001-07-13 | 1 | IT Billing | 1 |
1300
+ | 3 | Teddy | 23 | Norway | 20000 | 2007-12-13 | | | |
1301
+ | 4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13 | | | |
1302
+ | 5 | David | 27 | Texas | 85000 | 2007-12-13 | | | |
1303
+ | 2 | Allen | 25 | Texas | | 2005-07-13 | 2 | Engineering | 2 |
1304
+ | 8 | Paul | 24 | Houston | 20000 | 2005-07-13 | | | |
1305
+ | 9 | James | 44 | Norway | 5000 | 2005-07-13 | | | |
1306
+ | 10 | James | 45 | Texas | 5000 | | | | |
1307
+
1308
+ In a right join, all the rows of `tab_b` are included in the output, augmented
1309
+ by the matching columns of `tab_a` and augmented with nils where there is no
1310
+ match:
1311
+
1312
+ tab_a.right_join(tab_b, 'id_a == emp_id_b').to_aoa
1313
+
1314
+ | Id | Name | Age | Address | Salary | Join Date | Id B | Dept | Emp Id |
1315
+ |----|-------|-----|------------|--------|------------|------|-------------|--------|
1316
+ | 1 | Paul | 32 | California | 20000 | 2001-07-13 | 1 | IT Billing | 1 |
1317
+ | 2 | Allen | 25 | Texas | | 2005-07-13 | 2 | Engineering | 2 |
1318
+ | | | | | | | 3 | Finance | 7 |
1319
+
1320
+ 3. Full Join
1321
+
1322
+ A full join combines the effects of a left join and a right join. All the rows
1323
+ from both tables are included in the output augmented by columns of the other
1324
+ table where the join expression is satisfied and augmented with nils otherwise.
1325
+
1326
+ tab_a.full_join(tab_b, 'id_a == emp_id_b').to_aoa
1327
+
1328
+ | Id | Name | Age | Address | Salary | Join Date | Id B | Dept | Emp Id |
1329
+ |----|-------|-----|------------|--------|------------|------|-------------|--------|
1330
+ | 1 | Paul | 32 | California | 20000 | 2001-07-13 | 1 | IT Billing | 1 |
1331
+ | 3 | Teddy | 23 | Norway | 20000 | 2007-12-13 | | | |
1332
+ | 4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13 | | | |
1333
+ | 5 | David | 27 | Texas | 85000 | 2007-12-13 | | | |
1334
+ | 2 | Allen | 25 | Texas | | 2005-07-13 | 2 | Engineering | 2 |
1335
+ | 8 | Paul | 24 | Houston | 20000 | 2005-07-13 | | | |
1336
+ | 9 | James | 44 | Norway | 5000 | 2005-07-13 | | | |
1337
+ | 10 | James | 45 | Texas | 5000 | | | | |
1338
+ | | | | | | | 3 | Finance | 7 |
1339
+
1340
+ 4. Cross Join
1341
+
1342
+ Finally, a cross join outputs every row of `tab_a` augmented with every row of
1343
+ `tab_b`, in other words, the Cartesian product of the two tables. If `tab_a` has
1344
+ `N` rows and `tab_b` has `M` rows, the output table will have `N * M` rows.
1345
+
1346
+ tab_a.cross_join(tab_b).to_aoa
1347
+
1348
+ | Id | Name | Age | Address | Salary | Join Date | Id B | Dept | Emp Id |
1349
+ |----|-------|-----|------------|--------|------------|------|-------------|--------|
1350
+ | 1 | Paul | 32 | California | 20000 | 2001-07-13 | 1 | IT Billing | 1 |
1351
+ | 1 | Paul | 32 | California | 20000 | 2001-07-13 | 2 | Engineering | 2 |
1352
+ | 1 | Paul | 32 | California | 20000 | 2001-07-13 | 3 | Finance | 7 |
1353
+ | 3 | Teddy | 23 | Norway | 20000 | 2007-12-13 | 1 | IT Billing | 1 |
1354
+ | 3 | Teddy | 23 | Norway | 20000 | 2007-12-13 | 2 | Engineering | 2 |
1355
+ | 3 | Teddy | 23 | Norway | 20000 | 2007-12-13 | 3 | Finance | 7 |
1356
+ | 4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13 | 1 | IT Billing | 1 |
1357
+ | 4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13 | 2 | Engineering | 2 |
1358
+ | 4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13 | 3 | Finance | 7 |
1359
+ | 5 | David | 27 | Texas | 85000 | 2007-12-13 | 1 | IT Billing | 1 |
1360
+ | 5 | David | 27 | Texas | 85000 | 2007-12-13 | 2 | Engineering | 2 |
1361
+ | 5 | David | 27 | Texas | 85000 | 2007-12-13 | 3 | Finance | 7 |
1362
+ | 2 | Allen | 25 | Texas | | 2005-07-13 | 1 | IT Billing | 1 |
1363
+ | 2 | Allen | 25 | Texas | | 2005-07-13 | 2 | Engineering | 2 |
1364
+ | 2 | Allen | 25 | Texas | | 2005-07-13 | 3 | Finance | 7 |
1365
+ | 8 | Paul | 24 | Houston | 20000 | 2005-07-13 | 1 | IT Billing | 1 |
1366
+ | 8 | Paul | 24 | Houston | 20000 | 2005-07-13 | 2 | Engineering | 2 |
1367
+ | 8 | Paul | 24 | Houston | 20000 | 2005-07-13 | 3 | Finance | 7 |
1368
+ | 9 | James | 44 | Norway | 5000 | 2005-07-13 | 1 | IT Billing | 1 |
1369
+ | 9 | James | 44 | Norway | 5000 | 2005-07-13 | 2 | Engineering | 2 |
1370
+ | 9 | James | 44 | Norway | 5000 | 2005-07-13 | 3 | Finance | 7 |
1371
+ | 10 | James | 45 | Texas | 5000 | | 1 | IT Billing | 1 |
1372
+ | 10 | James | 45 | Texas | 5000 | | 2 | Engineering | 2 |
1373
+ | 10 | James | 45 | Texas | 5000 | | 3 | Finance | 7 |
1374
+
1375
+
1376
+ <a id="org7d2857d"></a>
1377
+
1378
+ ### Set Operations
1379
+
1380
+ `FatTable` can perform several set operations on tables. In order for two tables
1381
+ to be used this way, they must have the same number of columns with the same
1382
+ types or an exception will be raised. We&rsquo;ll call two tables that qualify for
1383
+ combining with set operations &ldquo;set-compatible.&rdquo;
1384
+
1385
+ We&rsquo;ll use the following two set-compatible tables in the examples. They each
1386
+ have some duplicates and some group boundaries so you can see the effect of the
1387
+ set operations on duplicates and groups.
1388
+
1389
+ tab1.to_aoa
1390
+
1391
+ | Ref | Date | Code | Price | G10 | QP10 | Shares | Lp | Qp | Iplp | Ipqp |
1392
+ |------|------------|------|-------|-----|------|--------|------|-------|--------|--------|
1393
+ | T001 | 2016-11-01 | P | 7.7 | T | F | 100 | 14 | 86 | 0.2453 | 0.1924 |
1394
+ | T002 | 2016-11-01 | P | 7.75 | T | F | 200 | 28 | 172 | 0.2453 | 0.1924 |
1395
+ | T003 | 2016-11-01 | P | 7.5 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
1396
+ | T003 | 2016-11-01 | P | 7.5 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
1397
+ |------|------------|------|-------|-----|------|--------|------|-------|--------|--------|
1398
+ | T004 | 2016-11-01 | S | 7.55 | T | F | 6811 | 966 | 5845 | 0.2453 | 0.1924 |
1399
+ | T005 | 2016-11-01 | S | 7.5 | F | F | 4000 | 572 | 3428 | 0.2453 | 0.1924 |
1400
+ | T006 | 2016-11-01 | S | 7.6 | F | T | 1000 | 143 | 857 | 0.2453 | 0.1924 |
1401
+ | T006 | 2016-11-01 | S | 7.6 | F | T | 1000 | 143 | 857 | 0.2453 | 0.1924 |
1402
+ | T007 | 2016-11-01 | S | 7.65 | T | F | 200 | 28 | 172 | 0.2453 | 0.1924 |
1403
+ | T008 | 2016-11-01 | P | 7.65 | F | F | 2771 | 393 | 2378 | 0.2453 | 0.1924 |
1404
+ | T009 | 2016-11-01 | P | 7.6 | F | F | 9550 | 1363 | 8187 | 0.2453 | 0.1924 |
1405
+ |------|------------|------|-------|-----|------|--------|------|-------|--------|--------|
1406
+ | T010 | 2016-11-01 | P | 7.55 | F | T | 3175 | 451 | 2724 | 0.2453 | 0.1924 |
1407
+ | T011 | 2016-11-02 | P | 7.425 | T | F | 100 | 14 | 86 | 0.2453 | 0.1924 |
1408
+ | T012 | 2016-11-02 | P | 7.55 | F | F | 4700 | 677 | 4023 | 0.2453 | 0.1924 |
1409
+ | T012 | 2016-11-02 | P | 7.55 | F | F | 4700 | 677 | 4023 | 0.2453 | 0.1924 |
1410
+ | T013 | 2016-11-02 | P | 7.35 | T | T | 53100 | 7656 | 45444 | 0.2453 | 0.1924 |
1411
+ |------|------------|------|-------|-----|------|--------|------|-------|--------|--------|
1412
+ | T014 | 2016-11-02 | P | 7.45 | F | T | 5847 | 835 | 5012 | 0.2453 | 0.1924 |
1413
+ | T015 | 2016-11-02 | P | 7.75 | F | F | 500 | 72 | 428 | 0.2453 | 0.1924 |
1414
+ | T016 | 2016-11-02 | P | 8.25 | T | T | 100 | 14 | 86 | 0.2453 | 0.1924 |
1415
+
1416
+ tab2.to_aoa
1417
+
1418
+ | Ref | Date | Code | Price | G10 | QP10 | Shares | Lp | Qp | Iplp | Ipqp |
1419
+ |------|------------|------|-------|-----|------|--------|-------|------|--------|--------|
1420
+ | T003 | 2016-11-01 | P | 7.5 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
1421
+ | T003 | 2016-11-01 | P | 7.5 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
1422
+ | T017 | 2016-11-01 | P | 8.3 | F | T | 1801 | 1201 | 600 | 0.2453 | 0.1924 |
1423
+ |------|------------|------|-------|-----|------|--------|-------|------|--------|--------|
1424
+ | T018 | 2016-11-01 | S | 7.152 | T | F | 2516 | 2400 | 116 | 0.2453 | 0.1924 |
1425
+ | T018 | 2016-11-01 | S | 7.152 | T | F | 2516 | 2400 | 116 | 0.2453 | 0.1924 |
1426
+ | T006 | 2016-11-01 | S | 7.6 | F | T | 1000 | 143 | 857 | 0.2453 | 0.1924 |
1427
+ | T007 | 2016-11-01 | S | 7.65 | T | F | 200 | 28 | 172 | 0.2453 | 0.1924 |
1428
+ |------|------------|------|-------|-----|------|--------|-------|------|--------|--------|
1429
+ | T014 | 2016-11-02 | P | 7.45 | F | T | 5847 | 835 | 5012 | 0.2453 | 0.1924 |
1430
+ | T015 | 2016-11-02 | P | 7.75 | F | F | 500 | 72 | 428 | 0.2453 | 0.1924 |
1431
+ | T015 | 2016-11-02 | P | 7.75 | F | F | 500 | 72 | 428 | 0.2453 | 0.1924 |
1432
+ | T016 | 2016-11-02 | P | 8.25 | T | T | 100 | 14 | 86 | 0.2453 | 0.1924 |
1433
+ |------|------------|------|-------|-----|------|--------|-------|------|--------|--------|
1434
+ | T019 | 2017-01-15 | S | 8.75 | T | F | 300 | 175 | 125 | 0.2453 | 0.1924 |
1435
+ | T020 | 2017-01-19 | S | 8.25 | F | T | 700 | 615 | 85 | 0.2453 | 0.1924 |
1436
+ | T021 | 2017-01-23 | P | 7.16 | T | T | 12100 | 11050 | 1050 | 0.2453 | 0.1924 |
1437
+ | T021 | 2017-01-23 | P | 7.16 | T | T | 12100 | 11050 | 1050 | 0.2453 | 0.1924 |
1438
+
1439
+ 1. Unions
1440
+
1441
+ Two tables that are set-compatible can be combined with the `union` or
1442
+ `union_all` methods so that the rows of both tables appear in the output. In the
1443
+ output table, the headers of the receiver table are used. You can use `select`
1444
+ to change or re-order the headers if you prefer. The `union` method eliminates
1445
+ duplicate rows in the result table, the `union_all` method does not.
1446
+
1447
+ Any group boundaries in the input tables are destroyed by `union` but are
1448
+ preserved by `union_all`. In addition, `union_all` (but not `union`) adds a
1449
+ group boundary between the rows of the two input tables.
1450
+
1451
+ tab1.union(tab2).to_aoa
1452
+
1453
+ | Ref | Date | Code | Price | G10 | QP10 | Shares | Lp | Qp | Iplp | Ipqp |
1454
+ |------|------------|------|-------|-----|------|--------|-------|-------|--------|--------|
1455
+ | T001 | 2016-11-01 | P | 7.7 | T | F | 100 | 14 | 86 | 0.2453 | 0.1924 |
1456
+ | T002 | 2016-11-01 | P | 7.75 | T | F | 200 | 28 | 172 | 0.2453 | 0.1924 |
1457
+ | T003 | 2016-11-01 | P | 7.5 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
1458
+ | T004 | 2016-11-01 | S | 7.55 | T | F | 6811 | 966 | 5845 | 0.2453 | 0.1924 |
1459
+ | T005 | 2016-11-01 | S | 7.5 | F | F | 4000 | 572 | 3428 | 0.2453 | 0.1924 |
1460
+ | T006 | 2016-11-01 | S | 7.6 | F | T | 1000 | 143 | 857 | 0.2453 | 0.1924 |
1461
+ | T007 | 2016-11-01 | S | 7.65 | T | F | 200 | 28 | 172 | 0.2453 | 0.1924 |
1462
+ | T008 | 2016-11-01 | P | 7.65 | F | F | 2771 | 393 | 2378 | 0.2453 | 0.1924 |
1463
+ | T009 | 2016-11-01 | P | 7.6 | F | F | 9550 | 1363 | 8187 | 0.2453 | 0.1924 |
1464
+ | T010 | 2016-11-01 | P | 7.55 | F | T | 3175 | 451 | 2724 | 0.2453 | 0.1924 |
1465
+ | T011 | 2016-11-02 | P | 7.425 | T | F | 100 | 14 | 86 | 0.2453 | 0.1924 |
1466
+ | T012 | 2016-11-02 | P | 7.55 | F | F | 4700 | 677 | 4023 | 0.2453 | 0.1924 |
1467
+ | T013 | 2016-11-02 | P | 7.35 | T | T | 53100 | 7656 | 45444 | 0.2453 | 0.1924 |
1468
+ | T014 | 2016-11-02 | P | 7.45 | F | T | 5847 | 835 | 5012 | 0.2453 | 0.1924 |
1469
+ | T015 | 2016-11-02 | P | 7.75 | F | F | 500 | 72 | 428 | 0.2453 | 0.1924 |
1470
+ | T016 | 2016-11-02 | P | 8.25 | T | T | 100 | 14 | 86 | 0.2453 | 0.1924 |
1471
+ | T017 | 2016-11-01 | P | 8.3 | F | T | 1801 | 1201 | 600 | 0.2453 | 0.1924 |
1472
+ | T018 | 2016-11-01 | S | 7.152 | T | F | 2516 | 2400 | 116 | 0.2453 | 0.1924 |
1473
+ | T019 | 2017-01-15 | S | 8.75 | T | F | 300 | 175 | 125 | 0.2453 | 0.1924 |
1474
+ | T020 | 2017-01-19 | S | 8.25 | F | T | 700 | 615 | 85 | 0.2453 | 0.1924 |
1475
+ | T021 | 2017-01-23 | P | 7.16 | T | T | 12100 | 11050 | 1050 | 0.2453 | 0.1924 |
1476
+
1477
+ tab1.union_all(tab2).to_aoa
1478
+
1479
+ | Ref | Date | Code | Price | G10 | QP10 | Shares | Lp | Qp | Iplp | Ipqp |
1480
+ |------|------------|------|-------|-----|------|--------|-------|-------|--------|--------|
1481
+ | T001 | 2016-11-01 | P | 7.7 | T | F | 100 | 14 | 86 | 0.2453 | 0.1924 |
1482
+ | T002 | 2016-11-01 | P | 7.75 | T | F | 200 | 28 | 172 | 0.2453 | 0.1924 |
1483
+ | T003 | 2016-11-01 | P | 7.5 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
1484
+ | T003 | 2016-11-01 | P | 7.5 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
1485
+ |------|------------|------|-------|-----|------|--------|-------|-------|--------|--------|
1486
+ | T004 | 2016-11-01 | S | 7.55 | T | F | 6811 | 966 | 5845 | 0.2453 | 0.1924 |
1487
+ | T005 | 2016-11-01 | S | 7.5 | F | F | 4000 | 572 | 3428 | 0.2453 | 0.1924 |
1488
+ | T006 | 2016-11-01 | S | 7.6 | F | T | 1000 | 143 | 857 | 0.2453 | 0.1924 |
1489
+ | T006 | 2016-11-01 | S | 7.6 | F | T | 1000 | 143 | 857 | 0.2453 | 0.1924 |
1490
+ | T007 | 2016-11-01 | S | 7.65 | T | F | 200 | 28 | 172 | 0.2453 | 0.1924 |
1491
+ | T008 | 2016-11-01 | P | 7.65 | F | F | 2771 | 393 | 2378 | 0.2453 | 0.1924 |
1492
+ | T009 | 2016-11-01 | P | 7.6 | F | F | 9550 | 1363 | 8187 | 0.2453 | 0.1924 |
1493
+ |------|------------|------|-------|-----|------|--------|-------|-------|--------|--------|
1494
+ | T010 | 2016-11-01 | P | 7.55 | F | T | 3175 | 451 | 2724 | 0.2453 | 0.1924 |
1495
+ | T011 | 2016-11-02 | P | 7.425 | T | F | 100 | 14 | 86 | 0.2453 | 0.1924 |
1496
+ | T012 | 2016-11-02 | P | 7.55 | F | F | 4700 | 677 | 4023 | 0.2453 | 0.1924 |
1497
+ | T012 | 2016-11-02 | P | 7.55 | F | F | 4700 | 677 | 4023 | 0.2453 | 0.1924 |
1498
+ | T013 | 2016-11-02 | P | 7.35 | T | T | 53100 | 7656 | 45444 | 0.2453 | 0.1924 |
1499
+ |------|------------|------|-------|-----|------|--------|-------|-------|--------|--------|
1500
+ | T014 | 2016-11-02 | P | 7.45 | F | T | 5847 | 835 | 5012 | 0.2453 | 0.1924 |
1501
+ | T015 | 2016-11-02 | P | 7.75 | F | F | 500 | 72 | 428 | 0.2453 | 0.1924 |
1502
+ | T016 | 2016-11-02 | P | 8.25 | T | T | 100 | 14 | 86 | 0.2453 | 0.1924 |
1503
+ |------|------------|------|-------|-----|------|--------|-------|-------|--------|--------|
1504
+ | T003 | 2016-11-01 | P | 7.5 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
1505
+ | T003 | 2016-11-01 | P | 7.5 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
1506
+ | T017 | 2016-11-01 | P | 8.3 | F | T | 1801 | 1201 | 600 | 0.2453 | 0.1924 |
1507
+ |------|------------|------|-------|-----|------|--------|-------|-------|--------|--------|
1508
+ | T018 | 2016-11-01 | S | 7.152 | T | F | 2516 | 2400 | 116 | 0.2453 | 0.1924 |
1509
+ | T018 | 2016-11-01 | S | 7.152 | T | F | 2516 | 2400 | 116 | 0.2453 | 0.1924 |
1510
+ | T006 | 2016-11-01 | S | 7.6 | F | T | 1000 | 143 | 857 | 0.2453 | 0.1924 |
1511
+ | T007 | 2016-11-01 | S | 7.65 | T | F | 200 | 28 | 172 | 0.2453 | 0.1924 |
1512
+ |------|------------|------|-------|-----|------|--------|-------|-------|--------|--------|
1513
+ | T014 | 2016-11-02 | P | 7.45 | F | T | 5847 | 835 | 5012 | 0.2453 | 0.1924 |
1514
+ | T015 | 2016-11-02 | P | 7.75 | F | F | 500 | 72 | 428 | 0.2453 | 0.1924 |
1515
+ | T015 | 2016-11-02 | P | 7.75 | F | F | 500 | 72 | 428 | 0.2453 | 0.1924 |
1516
+ | T016 | 2016-11-02 | P | 8.25 | T | T | 100 | 14 | 86 | 0.2453 | 0.1924 |
1517
+ |------|------------|------|-------|-----|------|--------|-------|-------|--------|--------|
1518
+ | T019 | 2017-01-15 | S | 8.75 | T | F | 300 | 175 | 125 | 0.2453 | 0.1924 |
1519
+ | T020 | 2017-01-19 | S | 8.25 | F | T | 700 | 615 | 85 | 0.2453 | 0.1924 |
1520
+ | T021 | 2017-01-23 | P | 7.16 | T | T | 12100 | 11050 | 1050 | 0.2453 | 0.1924 |
1521
+ | T021 | 2017-01-23 | P | 7.16 | T | T | 12100 | 11050 | 1050 | 0.2453 | 0.1924 |
1522
+
1523
+ 2. Intersections
1524
+
1525
+ The `intersect` method returns a table having only rows common to both tables,
1526
+ eliminating any duplicate rows in the result.
1527
+
1528
+ tab1.intersect(tab2).to_aoa
1529
+
1530
+ | Ref | Date | Code | Price | G10 | QP10 | Shares | Lp | Qp | Iplp | Ipqp |
1531
+ |------|------------|------|-------|-----|------|--------|-----|------|--------|--------|
1532
+ | T003 | 2016-11-01 | P | 7.5 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
1533
+ | T006 | 2016-11-01 | S | 7.6 | F | T | 1000 | 143 | 857 | 0.2453 | 0.1924 |
1534
+ | T007 | 2016-11-01 | S | 7.65 | T | F | 200 | 28 | 172 | 0.2453 | 0.1924 |
1535
+ | T014 | 2016-11-02 | P | 7.45 | F | T | 5847 | 835 | 5012 | 0.2453 | 0.1924 |
1536
+ | T015 | 2016-11-02 | P | 7.75 | F | F | 500 | 72 | 428 | 0.2453 | 0.1924 |
1537
+ | T016 | 2016-11-02 | P | 8.25 | T | T | 100 | 14 | 86 | 0.2453 | 0.1924 |
1538
+
1539
+ With `intersect_all`, all the rows of the first table, including duplicates, are
1540
+ included in the result if they also occur in the second table. However,
1541
+ duplicates in the second table do not appear.
1542
+
1543
+ tab1.intersect_all(tab2).to_aoa
1544
+
1545
+ | Ref | Date | Code | Price | G10 | QP10 | Shares | Lp | Qp | Iplp | Ipqp |
1546
+ |------|------------|------|-------|-----|------|--------|-----|------|--------|--------|
1547
+ | T003 | 2016-11-01 | P | 7.5 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
1548
+ | T003 | 2016-11-01 | P | 7.5 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
1549
+ | T006 | 2016-11-01 | S | 7.6 | F | T | 1000 | 143 | 857 | 0.2453 | 0.1924 |
1550
+ | T006 | 2016-11-01 | S | 7.6 | F | T | 1000 | 143 | 857 | 0.2453 | 0.1924 |
1551
+ | T007 | 2016-11-01 | S | 7.65 | T | F | 200 | 28 | 172 | 0.2453 | 0.1924 |
1552
+ | T014 | 2016-11-02 | P | 7.45 | F | T | 5847 | 835 | 5012 | 0.2453 | 0.1924 |
1553
+ | T015 | 2016-11-02 | P | 7.75 | F | F | 500 | 72 | 428 | 0.2453 | 0.1924 |
1554
+ | T016 | 2016-11-02 | P | 8.25 | T | T | 100 | 14 | 86 | 0.2453 | 0.1924 |
1555
+
1556
+ As a result, it makes a difference which table is the receiver of the
1557
+ `intersect_all` method call and which is the argument. In other words, order of
1558
+ operation matters.
1559
+
1560
+ tab2.intersect_all(tab1).to_aoa
1561
+
1562
+ | Ref | Date | Code | Price | G10 | QP10 | Shares | Lp | Qp | Iplp | Ipqp |
1563
+ |------|------------|------|-------|-----|------|--------|-----|------|--------|--------|
1564
+ | T003 | 2016-11-01 | P | 7.5 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
1565
+ | T003 | 2016-11-01 | P | 7.5 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
1566
+ | T006 | 2016-11-01 | S | 7.6 | F | T | 1000 | 143 | 857 | 0.2453 | 0.1924 |
1567
+ | T007 | 2016-11-01 | S | 7.65 | T | F | 200 | 28 | 172 | 0.2453 | 0.1924 |
1568
+ | T014 | 2016-11-02 | P | 7.45 | F | T | 5847 | 835 | 5012 | 0.2453 | 0.1924 |
1569
+ | T015 | 2016-11-02 | P | 7.75 | F | F | 500 | 72 | 428 | 0.2453 | 0.1924 |
1570
+ | T015 | 2016-11-02 | P | 7.75 | F | F | 500 | 72 | 428 | 0.2453 | 0.1924 |
1571
+ | T016 | 2016-11-02 | P | 8.25 | T | T | 100 | 14 | 86 | 0.2453 | 0.1924 |
1572
+
1573
+ 3. Differences with Except
1574
+
1575
+ You can use the `except` method to delete from a table any rows that occur in
1576
+ another table, that is, compute the set difference between the tables.
1577
+
1578
+ tab1.except(tab2).to_aoa
1579
+
1580
+ | Ref | Date | Code | Price | G10 | QP10 | Shares | Lp | Qp | Iplp | Ipqp |
1581
+ |------|------------|------|-------|-----|------|--------|------|-------|--------|--------|
1582
+ | T001 | 2016-11-01 | P | 7.7 | T | F | 100 | 14 | 86 | 0.2453 | 0.1924 |
1583
+ | T002 | 2016-11-01 | P | 7.75 | T | F | 200 | 28 | 172 | 0.2453 | 0.1924 |
1584
+ | T004 | 2016-11-01 | S | 7.55 | T | F | 6811 | 966 | 5845 | 0.2453 | 0.1924 |
1585
+ | T005 | 2016-11-01 | S | 7.5 | F | F | 4000 | 572 | 3428 | 0.2453 | 0.1924 |
1586
+ | T008 | 2016-11-01 | P | 7.65 | F | F | 2771 | 393 | 2378 | 0.2453 | 0.1924 |
1587
+ | T009 | 2016-11-01 | P | 7.6 | F | F | 9550 | 1363 | 8187 | 0.2453 | 0.1924 |
1588
+ | T010 | 2016-11-01 | P | 7.55 | F | T | 3175 | 451 | 2724 | 0.2453 | 0.1924 |
1589
+ | T011 | 2016-11-02 | P | 7.425 | T | F | 100 | 14 | 86 | 0.2453 | 0.1924 |
1590
+ | T012 | 2016-11-02 | P | 7.55 | F | F | 4700 | 677 | 4023 | 0.2453 | 0.1924 |
1591
+ | T013 | 2016-11-02 | P | 7.35 | T | T | 53100 | 7656 | 45444 | 0.2453 | 0.1924 |
1592
+
1593
+ Like subtraction, though, the order of operands matters with set difference
1594
+ computed by `except`.
1595
+
1596
+ tab2.except(tab1).to_aoa
1597
+
1598
+ | Ref | Date | Code | Price | G10 | QP10 | Shares | Lp | Qp | Iplp | Ipqp |
1599
+ |------|------------|------|-------|-----|------|--------|-------|------|--------|--------|
1600
+ | T017 | 2016-11-01 | P | 8.3 | F | T | 1801 | 1201 | 600 | 0.2453 | 0.1924 |
1601
+ | T018 | 2016-11-01 | S | 7.152 | T | F | 2516 | 2400 | 116 | 0.2453 | 0.1924 |
1602
+ | T019 | 2017-01-15 | S | 8.75 | T | F | 300 | 175 | 125 | 0.2453 | 0.1924 |
1603
+ | T020 | 2017-01-19 | S | 8.25 | F | T | 700 | 615 | 85 | 0.2453 | 0.1924 |
1604
+ | T021 | 2017-01-23 | P | 7.16 | T | T | 12100 | 11050 | 1050 | 0.2453 | 0.1924 |
1605
+
1606
+ As with `intersect_all`, `except_all` includes any duplicates in the first,
1607
+ receiver table, but not those in the second, argument table.
1608
+
1609
+ tab1.except_all(tab2).to_aoa
1610
+
1611
+ | Ref | Date | Code | Price | G10 | QP10 | Shares | Lp | Qp | Iplp | Ipqp |
1612
+ |------|------------|------|-------|-----|------|--------|------|-------|--------|--------|
1613
+ | T001 | 2016-11-01 | P | 7.7 | T | F | 100 | 14 | 86 | 0.2453 | 0.1924 |
1614
+ | T002 | 2016-11-01 | P | 7.75 | T | F | 200 | 28 | 172 | 0.2453 | 0.1924 |
1615
+ | T004 | 2016-11-01 | S | 7.55 | T | F | 6811 | 966 | 5845 | 0.2453 | 0.1924 |
1616
+ | T005 | 2016-11-01 | S | 7.5 | F | F | 4000 | 572 | 3428 | 0.2453 | 0.1924 |
1617
+ | T008 | 2016-11-01 | P | 7.65 | F | F | 2771 | 393 | 2378 | 0.2453 | 0.1924 |
1618
+ | T009 | 2016-11-01 | P | 7.6 | F | F | 9550 | 1363 | 8187 | 0.2453 | 0.1924 |
1619
+ | T010 | 2016-11-01 | P | 7.55 | F | T | 3175 | 451 | 2724 | 0.2453 | 0.1924 |
1620
+ | T011 | 2016-11-02 | P | 7.425 | T | F | 100 | 14 | 86 | 0.2453 | 0.1924 |
1621
+ | T012 | 2016-11-02 | P | 7.55 | F | F | 4700 | 677 | 4023 | 0.2453 | 0.1924 |
1622
+ | T012 | 2016-11-02 | P | 7.55 | F | F | 4700 | 677 | 4023 | 0.2453 | 0.1924 |
1623
+ | T013 | 2016-11-02 | P | 7.35 | T | T | 53100 | 7656 | 45444 | 0.2453 | 0.1924 |
1624
+
1625
+ And, of course, the order of operands matters here as well.
1626
+
1627
+ tab2.except_all(tab1).to_aoa
1628
+
1629
+ | Ref | Date | Code | Price | G10 | QP10 | Shares | Lp | Qp | Iplp | Ipqp |
1630
+ |------|------------|------|-------|-----|------|--------|-------|------|--------|--------|
1631
+ | T017 | 2016-11-01 | P | 8.3 | F | T | 1801 | 1201 | 600 | 0.2453 | 0.1924 |
1632
+ | T018 | 2016-11-01 | S | 7.152 | T | F | 2516 | 2400 | 116 | 0.2453 | 0.1924 |
1633
+ | T018 | 2016-11-01 | S | 7.152 | T | F | 2516 | 2400 | 116 | 0.2453 | 0.1924 |
1634
+ | T019 | 2017-01-15 | S | 8.75 | T | F | 300 | 175 | 125 | 0.2453 | 0.1924 |
1635
+ | T020 | 2017-01-19 | S | 8.25 | F | T | 700 | 615 | 85 | 0.2453 | 0.1924 |
1636
+ | T021 | 2017-01-23 | P | 7.16 | T | T | 12100 | 11050 | 1050 | 0.2453 | 0.1924 |
1637
+ | T021 | 2017-01-23 | P | 7.16 | T | T | 12100 | 11050 | 1050 | 0.2453 | 0.1924 |
1638
+
1639
+
1640
+ <a id="org073a8b5"></a>
1641
+
1642
+ ### Uniq (aka Distinct)
1643
+
1644
+ The `uniq` method takes no arguments and simply removes any duplicate rows from
1645
+ the input table. The `distinct` method is an alias for `uniq`. Any groups in
1646
+ the input table are lost.
1647
+
1648
+ tab1.uniq.to_aoa
1649
+
1650
+ | Ref | Date | Code | Price | G10 | QP10 | Shares | Lp | Qp | Iplp | Ipqp |
1651
+ |------|------------|------|-------|-----|------|--------|------|-------|--------|--------|
1652
+ | T001 | 2016-11-01 | P | 7.7 | T | F | 100 | 14 | 86 | 0.2453 | 0.1924 |
1653
+ | T002 | 2016-11-01 | P | 7.75 | T | F | 200 | 28 | 172 | 0.2453 | 0.1924 |
1654
+ | T003 | 2016-11-01 | P | 7.5 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
1655
+ | T004 | 2016-11-01 | S | 7.55 | T | F | 6811 | 966 | 5845 | 0.2453 | 0.1924 |
1656
+ | T005 | 2016-11-01 | S | 7.5 | F | F | 4000 | 572 | 3428 | 0.2453 | 0.1924 |
1657
+ | T006 | 2016-11-01 | S | 7.6 | F | T | 1000 | 143 | 857 | 0.2453 | 0.1924 |
1658
+ | T007 | 2016-11-01 | S | 7.65 | T | F | 200 | 28 | 172 | 0.2453 | 0.1924 |
1659
+ | T008 | 2016-11-01 | P | 7.65 | F | F | 2771 | 393 | 2378 | 0.2453 | 0.1924 |
1660
+ | T009 | 2016-11-01 | P | 7.6 | F | F | 9550 | 1363 | 8187 | 0.2453 | 0.1924 |
1661
+ | T010 | 2016-11-01 | P | 7.55 | F | T | 3175 | 451 | 2724 | 0.2453 | 0.1924 |
1662
+ | T011 | 2016-11-02 | P | 7.425 | T | F | 100 | 14 | 86 | 0.2453 | 0.1924 |
1663
+ | T012 | 2016-11-02 | P | 7.55 | F | F | 4700 | 677 | 4023 | 0.2453 | 0.1924 |
1664
+ | T013 | 2016-11-02 | P | 7.35 | T | T | 53100 | 7656 | 45444 | 0.2453 | 0.1924 |
1665
+ | T014 | 2016-11-02 | P | 7.45 | F | T | 5847 | 835 | 5012 | 0.2453 | 0.1924 |
1666
+ | T015 | 2016-11-02 | P | 7.75 | F | F | 500 | 72 | 428 | 0.2453 | 0.1924 |
1667
+ | T016 | 2016-11-02 | P | 8.25 | T | T | 100 | 14 | 86 | 0.2453 | 0.1924 |
1668
+
1669
+
1670
+ <a id="orgd147303"></a>
1671
+
1672
+ ### Remove groups with degroup!
1673
+
1674
+ Finally, it is sometimes helpful to remove any group boundaries from a table.
1675
+ You can do this with `.degroup!`, which is the only operation that mutates its
1676
+ receiver table by removing its groups.
1677
+
1678
+ tab1.degroup!.to_aoa
1679
+
1680
+ | Ref | Date | Code | Price | G10 | QP10 | Shares | Lp | Qp | Iplp | Ipqp |
1681
+ |------|------------|------|-------|-----|------|--------|------|-------|--------|--------|
1682
+ | T001 | 2016-11-01 | P | 7.7 | T | F | 100 | 14 | 86 | 0.2453 | 0.1924 |
1683
+ | T002 | 2016-11-01 | P | 7.75 | T | F | 200 | 28 | 172 | 0.2453 | 0.1924 |
1684
+ | T003 | 2016-11-01 | P | 7.5 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
1685
+ | T003 | 2016-11-01 | P | 7.5 | F | T | 800 | 112 | 688 | 0.2453 | 0.1924 |
1686
+ | T004 | 2016-11-01 | S | 7.55 | T | F | 6811 | 966 | 5845 | 0.2453 | 0.1924 |
1687
+ | T005 | 2016-11-01 | S | 7.5 | F | F | 4000 | 572 | 3428 | 0.2453 | 0.1924 |
1688
+ | T006 | 2016-11-01 | S | 7.6 | F | T | 1000 | 143 | 857 | 0.2453 | 0.1924 |
1689
+ | T006 | 2016-11-01 | S | 7.6 | F | T | 1000 | 143 | 857 | 0.2453 | 0.1924 |
1690
+ | T007 | 2016-11-01 | S | 7.65 | T | F | 200 | 28 | 172 | 0.2453 | 0.1924 |
1691
+ | T008 | 2016-11-01 | P | 7.65 | F | F | 2771 | 393 | 2378 | 0.2453 | 0.1924 |
1692
+ | T009 | 2016-11-01 | P | 7.6 | F | F | 9550 | 1363 | 8187 | 0.2453 | 0.1924 |
1693
+ | T010 | 2016-11-01 | P | 7.55 | F | T | 3175 | 451 | 2724 | 0.2453 | 0.1924 |
1694
+ | T011 | 2016-11-02 | P | 7.425 | T | F | 100 | 14 | 86 | 0.2453 | 0.1924 |
1695
+ | T012 | 2016-11-02 | P | 7.55 | F | F | 4700 | 677 | 4023 | 0.2453 | 0.1924 |
1696
+ | T012 | 2016-11-02 | P | 7.55 | F | F | 4700 | 677 | 4023 | 0.2453 | 0.1924 |
1697
+ | T013 | 2016-11-02 | P | 7.35 | T | T | 53100 | 7656 | 45444 | 0.2453 | 0.1924 |
1698
+ | T014 | 2016-11-02 | P | 7.45 | F | T | 5847 | 835 | 5012 | 0.2453 | 0.1924 |
1699
+ | T015 | 2016-11-02 | P | 7.75 | F | F | 500 | 72 | 428 | 0.2453 | 0.1924 |
1700
+ | T016 | 2016-11-02 | P | 8.25 | T | T | 100 | 14 | 86 | 0.2453 | 0.1924 |
1701
+
1702
+
1703
+ <a id="org9f4d633"></a>
1704
+
1705
+ ## Formatting Tables
1706
+
1707
+ Besides creating and operating on tables, you may want to display the resulting
1708
+ table. `FatTable` seeks to provide a set of formatting directives that are the
1709
+ most common across many output media. It provides directives for alignment, for
1710
+ color, for adding currency symbols and grouping commas to numbers, for padding
1711
+ numbers, and for formatting dates and booleans.
1712
+
1713
+ In addition, you can add any number of footers to a table, which appear at the
1714
+ end of the table, and any number of group footers, which appear after each group
1715
+ in the table. These can be formatted independently of the table body.
1716
+
1717
+ If the target output medium does not support a formatting directive or the
1718
+ directive does not make sense, it is simply ignored. For example, you can output
1719
+ an `org-mode` table as a String, and since `org-mode` does not support colors,
1720
+ any color directives are ignored. Some of the output targets are not strings,
1721
+ but ruby data structures, and for them, things such as alignment are irrelevant.
1722
+
1723
+
1724
+ <a id="orgb7b2335"></a>
1725
+
1726
+ ### Available Formatters
1727
+
1728
+ `FatTable` supports the following output targets for its tables:
1729
+
1730
+ - **Text:** form the table with ACSII characters,
1731
+ - **Org:** form the table with ASCII characters but in the form used by Emacs
1732
+ org-mode for constructing tables,
1733
+ - **Term:** form the table with ANSI terminal codes and unicode characters,
1734
+ possibly including colored text and cell backgrounds,
1735
+ - **LaTeX:** form the table as input for LaTeX&rsquo;s longtable environment,
1736
+ - **Aoh:** output the table as a ruby data structure, building the table as an
1737
+ array of hashes, and
1738
+ - **Aoa:** output the table as a ruby data structure, building the table as an
1739
+ array of array,
1740
+
1741
+ These are all implemented by classes that inherit from `FatTable::Formatter`
1742
+ class by defining about a dozen methods that get called at various places during
1743
+ the construction of the output table. The idea is that more classes can be
1744
+ defined by adding additional classes.
1745
+
1746
+
1747
+ <a id="org4db9ae4"></a>
1748
+
1749
+ ### Table Locations
1750
+
1751
+ In the formatting methods, the table is divided into several &ldquo;locations&rdquo; for
1752
+ which separate formatting directives may be given. These locations are
1753
+ identified with the following symbols:
1754
+
1755
+ - **:header:** the first row of the output table containing the headers,
1756
+ - **:footer:** all rows of the table&rsquo;s footers,
1757
+ - **:gfooter:** all rows of the table&rsquo;s group footers,
1758
+ - **:body:** all the data rows of the table, that is, those that are neither part
1759
+ of the header, footers, or gfooters,
1760
+ - **:bfirst:** the first row of the table&rsquo;s body, and
1761
+ - **:gfirst:** the first row in each group in the table&rsquo;s body.
1762
+
1763
+
1764
+ <a id="orgd2128a3"></a>
1765
+
1766
+ ### Formatting Directives
1767
+
1768
+ The formatting methods explained in the next section all take formatting
1769
+ directives as strings in which letters and other characters signify what
1770
+ formatting applies. For example, we may apply the formatting directive `'R,$'`
1771
+ to numbers in a certain part of the table. Each of those characters, and in
1772
+ some cases a whole substring, is a single directive. They can appear in any
1773
+ order, so `'$R,'` and `',$R'` are equivalent.
1774
+
1775
+ Here is a list of all the formatting directives that apply to each cell type:
1776
+
1777
+ 1. String
1778
+
1779
+ For a string element, the following instructions are valid. Note that these can
1780
+ also be applied to all the other cell types as well since they are all converted
1781
+ to a string in forming the output.
1782
+
1783
+ - **u:** convert the element to all lowercase,
1784
+ - **U:** convert the element to all uppercase,
1785
+ - **t:** title case the element, that is, upcase the initial letter in
1786
+ each word and lower case the other letters
1787
+ - **B ~B:** make the element bold, or turn off bold
1788
+ - **I ~I:** make the element italic, or turn off italic
1789
+ - **R:** align the element on the right of the column
1790
+ - **L:** align the element on the left of the column
1791
+ - **C:** align the element in the center of the column
1792
+ - **c[color]:** render the element in the given color; the color can have
1793
+ the form fgcolor, fgcolor.bgcolor, or .bgcolor, to set the
1794
+ foreground or background colors respectively, and each of those can
1795
+ be an ANSI or X11 color name in addition to the special color,
1796
+ &rsquo;none&rsquo;, which keeps the terminal&rsquo;s default color.
1797
+ - **\_ ~\_:** underline the element, or turn off underline
1798
+ - **\* ~\*:** cause the element to blink, or turn off blink
1799
+
1800
+ For example, the directive `'tCc[red.yellow]'` would title-case the element,
1801
+ center it, and color it red on a yellow background. The directives that are
1802
+ boolean have negating forms so that, for example, if bold is turned on for all
1803
+ columns of a given type, it can be countermanded in formatting directives for
1804
+ particular columns.
1805
+
1806
+ 2. Numeric
1807
+
1808
+ For a numeric element, all the instructions valid for string are available, in
1809
+ addition to the following:
1810
+
1811
+ - **, ~,:** insert grouping commas, or do not insert grouping commas,
1812
+ - **$ ~$:** format the number as currency according to the locale, or not,
1813
+ - **m.n:** include at least m digits before the decimal point, padding on
1814
+ the left with zeroes as needed, and round the number to the n
1815
+ decimal places and include n digits after the decimal point,
1816
+ padding on the right with zeroes as needed,
1817
+ - **H:** convert the number (assumed to be in units of seconds) to `HH:MM:SS.ss`
1818
+ form. So a column that is the result of subtracting two :datetime forms
1819
+ will result in a :numeric expressed as seconds and can be displayed in
1820
+ hours, minutes, and seconds with this formatting instruction.
1821
+
1822
+ For example, the directive `'R5.0c[blue]'` would right-align the numeric
1823
+ element, pad it on the left with zeros, and color it blue.
1824
+
1825
+ 3. DateTime
1826
+
1827
+ For a `DateTime`, all the instructions valid for string are available, in
1828
+ addition to the following:
1829
+
1830
+ - **d[fmt]:** apply the format to a `Date` or a `DateTime` that is a whole day,
1831
+ that is that has no or zero hour, minute, and second components, where fmt
1832
+ is a valid format string for `Date#strftime`, otherwise, the datetime will
1833
+ be formatted as an ISO 8601 string, YYYY-MM-DD.
1834
+ - **D[fmt]:** apply the format to a datetime that has at least a non-zero hour
1835
+ component where fmt is a valid format string for Date#strftime, otherwise,
1836
+ the datetime will be formatted as an ISO 8601 string, YYYY-MM-DD.
1837
+
1838
+ For example, `'c[pink]d[%b %-d, %Y]C'`, would format a date element like &rsquo;Sep
1839
+ 22, 1957&rsquo;, center it, and color it pink.
1840
+
1841
+ 4. Boolean
1842
+
1843
+ For a boolean cell, all the instructions valid for string are available, in
1844
+ addition to the following:
1845
+
1846
+ - **Y:** print true as &rsquo;`Y`&rsquo; and false as &rsquo;`N`&rsquo;,
1847
+ - **T:** print true as &rsquo;`T`&rsquo; and false as &rsquo;`F`&rsquo;,
1848
+ - **X:** print true as &rsquo;`X`&rsquo; and false as an empty string &rsquo;&rsquo;,
1849
+ - **b[xxx,yyy]:** print true as the string given as `xxx` and false as the string
1850
+ given as `yyy`,
1851
+ - **c[tcolor,fcolor]:** color a true element with `tcolor` and a false element
1852
+ with `fcolor`. Each of the colors may be specified in the same manner as
1853
+ colors for strings described above.
1854
+
1855
+ For example, the directive &rsquo;`b[Yeppers,Nope]c[green.pink,red.pink]`&rsquo; would
1856
+ render a true boolean as &rsquo;`Yeppers`&rsquo; colored green on pink and render a false
1857
+ boolean as &rsquo;`Nope`&rsquo; colored red on pink. See [Yeppers](https://www.youtube.com/watch?v=oLdFFD8II8U) for additional information.
1858
+
1859
+ 5. NilClass
1860
+
1861
+ By default, `nil` elements are rendered as blank cells, but you can make them
1862
+ visible with the following, and in that case, all the formatting instructions
1863
+ valid for strings are also available:
1864
+
1865
+ - **n[niltext]:** render a `nil` item with the given niltext.
1866
+
1867
+ For example, you might want to use `'n[-]Cc[purple]'` to make nils visible as a
1868
+ centered purple hyphen.
1869
+
1870
+
1871
+ <a id="org947e8a4"></a>
1872
+
1873
+ ### Footers Methods
1874
+
1875
+ You can call the `footer` and `gfooter` methods on `Formatter` objects to add
1876
+ footers and group footers. Their signatures are:
1877
+
1878
+ - **`footer(label, *sum_cols, **agg_cols)`:** where `label` is a label to be
1879
+ placed in the first cell of the footer (unless that column is named as one
1880
+ of the `sum_cols` or `agg_cols`, in which case the label is ignored),
1881
+ `*sum_cols` are zero or more symbols for columns to be summed, and
1882
+ `**agg_cols` is zero or more hash-like parameters with a column symbol as a
1883
+ key and a symbol for an aggregate method as the value. This causes a
1884
+ table-wide header to be added at the bottom of the table applying the
1885
+ `:sum` aggregate to the `sum_cols` and the named aggregate method to the
1886
+ `agg_cols`. A table can have any number of footers attached, and they will
1887
+ appear at the bottom of the output table in the order they are given.
1888
+
1889
+ - **`gfooter(label, *sum_cols, **agg_cols)`:** where the parameters have the same
1890
+ meaning as for the `footer` method, but result in a footer for each group
1891
+ in the table rather than the table as a whole. These will appear in the
1892
+ output table just below each group.
1893
+
1894
+ There are also a number of convenience methods for adding common footers:
1895
+
1896
+ - **`sum_footer(*cols)`:** Add a footer summing the given columns with the label
1897
+ &rsquo;Total&rsquo;.
1898
+ - **`sum_gfooter(*cols)`:** Add a group footer summing the given columns with the
1899
+ label &rsquo;Group Total&rsquo;.
1900
+ - **`avg_footer(*cols)`:** Add a footer averaging the given columns with the label
1901
+ &rsquo;Average&rsquo;.
1902
+ - **`avg_gfooter(*cols)`:** Add a group footer averaging the given columns with the label
1903
+ &rsquo;Group Average&rsquo;.
1904
+ - **`min_footer(*cols)`:** Add a footer showing the minimum for the given columns
1905
+ with the label &rsquo;Minimum&rsquo;.
1906
+ - **`min_gfooter(*cols)`:** Add a group footer showing the minumum for the given
1907
+ columns with the label &rsquo;Group Minimum&rsquo;.
1908
+ - **`max_footer(*cols)`:** Add a footer showing the maximum for the given columns
1909
+ with the label &rsquo;Maximum&rsquo;.
1910
+ - **`max_gfooter(*cols)`:** Add a group footer showing the maximum for the given
1911
+ columns with the label &rsquo;Group Maximum&rsquo;.
1912
+
1913
+
1914
+ <a id="orgcef241a"></a>
1915
+
1916
+ ### Formatting Methods
1917
+
1918
+ You can call methods on `Formatter` objects to specify formatting directives
1919
+ for specific columns or types. There are two methods for doing so, `format_for`
1920
+ and `format`.
1921
+
1922
+ 1. Instantiating a Formatter
1923
+
1924
+ There are several ways to invoke the formatting methods on a table. First, you
1925
+ can instantiate a `XXXFormatter` object and feed it a table as a parameter.
1926
+ There is a Formatter subclass for each target output medium, for example,
1927
+ `AoaFormatter` will produce a ruby array of arrays. You can then call the
1928
+ `output` method on the `XXXFormatter`.
1929
+
1930
+ FatTable::AoaFormatter.new(tab_a).output
1931
+
1932
+ | Id | Name | Age | Address | Salary | Join Date |
1933
+ |----|-------|-----|------------|--------|------------|
1934
+ | 1 | Paul | 32 | California | 20000 | 2001-07-13 |
1935
+ | 3 | Teddy | 23 | Norway | 20000 | 2007-12-13 |
1936
+ | 4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13 |
1937
+ | 5 | David | 27 | Texas | 85000 | 2007-12-13 |
1938
+ | 2 | Allen | 25 | Texas | | 2005-07-13 |
1939
+ | 8 | Paul | 24 | Houston | 20000 | 2005-07-13 |
1940
+ | 9 | James | 44 | Norway | 5000 | 2005-07-13 |
1941
+ | 10 | James | 45 | Texas | 5000 | |
1942
+
1943
+ The `XXXFormatter.new` method yields the new instance to any block given, and
1944
+ you can call methods on it to affect the formatting of the output:
1945
+
1946
+ FatTable::AoaFormatter.new(tab_a) do |f|
1947
+ f.format(numeric: '0.0,R', id: '3.0C')
1948
+ end.output
1949
+
1950
+ | Id | Name | Age | Address | Salary | Join Date |
1951
+ |-----|-------|-----|------------|--------|------------|
1952
+ | 001 | Paul | 32 | California | 20,000 | 2001-07-13 |
1953
+ | 003 | Teddy | 23 | Norway | 20,000 | 2007-12-13 |
1954
+ | 004 | Mark | 25 | Rich-Mond | 65,000 | 2007-12-13 |
1955
+ | 005 | David | 27 | Texas | 85,000 | 2007-12-13 |
1956
+ | 002 | Allen | 25 | Texas | | 2005-07-13 |
1957
+ | 008 | Paul | 24 | Houston | 20,000 | 2005-07-13 |
1958
+ | 009 | James | 44 | Norway | 5,000 | 2005-07-13 |
1959
+ | 010 | James | 45 | Texas | 5,000 | |
1960
+
1961
+ 2. `FatTable` module-level method calls
1962
+
1963
+ The `FatTable` module provides a set of methods of the form `to_aoa`, `to_text`,
1964
+ etc., to access a `Formatter` without having to create an instance yourself.
1965
+ Without a block, they apply the default formatting to the table and call the
1966
+ `.output` method automatically:
1967
+
1968
+ FatTable.to_aoa(tab_a)
1969
+
1970
+ | Id | Name | Age | Address | Salary | Join Date |
1971
+ |----|-------|-----|------------|--------|------------|
1972
+ | 1 | Paul | 32 | California | 20000 | 2001-07-13 |
1973
+ | 3 | Teddy | 23 | Norway | 20000 | 2007-12-13 |
1974
+ | 4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13 |
1975
+ | 5 | David | 27 | Texas | 85000 | 2007-12-13 |
1976
+ | 2 | Allen | 25 | Texas | | 2005-07-13 |
1977
+ | 8 | Paul | 24 | Houston | 20000 | 2005-07-13 |
1978
+ | 9 | James | 44 | Norway | 5000 | 2005-07-13 |
1979
+ | 10 | James | 45 | Texas | 5000 | |
1980
+
1981
+ With a block, these methods yield a `Formatter` instance on which you can call
1982
+ formatting and footer methods. The `.output` method is called on the `Formatter`
1983
+ automatically after the block:
1984
+
1985
+ FatTable.to_aoa(tab_a) do |f|
1986
+ f.format(numeric: '0.0,R', id: '3.0C')
1987
+ end
1988
+
1989
+ | Id | Name | Age | Address | Salary | Join Date |
1990
+ |-----|-------|-----|------------|--------|------------|
1991
+ | 001 | Paul | 32 | California | 20,000 | 2001-07-13 |
1992
+ | 003 | Teddy | 23 | Norway | 20,000 | 2007-12-13 |
1993
+ | 004 | Mark | 25 | Rich-Mond | 65,000 | 2007-12-13 |
1994
+ | 005 | David | 27 | Texas | 85,000 | 2007-12-13 |
1995
+ | 002 | Allen | 25 | Texas | | 2005-07-13 |
1996
+ | 008 | Paul | 24 | Houston | 20,000 | 2005-07-13 |
1997
+ | 009 | James | 44 | Norway | 5,000 | 2005-07-13 |
1998
+ | 010 | James | 45 | Texas | 5,000 | |
1999
+
2000
+ 3. Calling methods on Table objects
2001
+
2002
+ Finally, you can call methods such as `to_aoa`, `to_text`, etc., directly on a
2003
+ Table:
2004
+
2005
+ tab_a.to_aoa
2006
+
2007
+ | Id | Name | Age | Address | Salary | Join Date |
2008
+ |----|-------|-----|------------|--------|------------|
2009
+ | 1 | Paul | 32 | California | 20000 | 2001-07-13 |
2010
+ | 3 | Teddy | 23 | Norway | 20000 | 2007-12-13 |
2011
+ | 4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13 |
2012
+ | 5 | David | 27 | Texas | 85000 | 2007-12-13 |
2013
+ | 2 | Allen | 25 | Texas | | 2005-07-13 |
2014
+ | 8 | Paul | 24 | Houston | 20000 | 2005-07-13 |
2015
+ | 9 | James | 44 | Norway | 5000 | 2005-07-13 |
2016
+ | 10 | James | 45 | Texas | 5000 | |
2017
+
2018
+ And you can supply a block to them as well to specify formatting or footers:
2019
+
2020
+ tab_a.to_aoa do |f|
2021
+ f.format(numeric: '0.0,R', id: '3.0C')
2022
+ f.sum_footer(:salary, :age)
2023
+ end
2024
+
2025
+ | Id | Name | Age | Address | Salary | Join Date |
2026
+ |-------|-------|-----|------------|---------|------------|
2027
+ | 001 | Paul | 32 | California | 20,000 | 2001-07-13 |
2028
+ | 003 | Teddy | 23 | Norway | 20,000 | 2007-12-13 |
2029
+ | 004 | Mark | 25 | Rich-Mond | 65,000 | 2007-12-13 |
2030
+ | 005 | David | 27 | Texas | 85,000 | 2007-12-13 |
2031
+ | 002 | Allen | 25 | Texas | | 2005-07-13 |
2032
+ | 008 | Paul | 24 | Houston | 20,000 | 2005-07-13 |
2033
+ | 009 | James | 44 | Norway | 5,000 | 2005-07-13 |
2034
+ | 010 | James | 45 | Texas | 5,000 | |
2035
+ |-------|-------|-----|------------|---------|------------|
2036
+ | Total | | 245 | | 220,000 | |
2037
+
2038
+
2039
+ <a id="org7b25866"></a>
2040
+
2041
+ ### The `format` and `format_for` methods
2042
+
2043
+ Formatters take only two kinds of methods, those that attach footers to a
2044
+ table, which are discussed in the next section, and those that specify
2045
+ formatting for table cells, which are the subject of this section.
2046
+
2047
+ To set formatting directives for all locations in a table at once, use the
2048
+ `format` method; to set formatting directives for a particular location in the
2049
+ table, use the `format_for` method, giving the location as the first parameter.
2050
+
2051
+ Other than that first parameter, the two methods take the same types of
2052
+ parameters. The remaining parameters are hash-like parameters that use either a
2053
+ column name or a type as the key and a string with the formatting directives to
2054
+ apply as the value. The following example says to set the formatting for all
2055
+ locations in the table and to format all numeric fields as strings that are
2056
+ rounded to whole numbers (the &rsquo;0.0&rsquo; part), that are right-aligned (the &rsquo;R&rsquo;
2057
+ part), and have grouping commas inserted (the &rsquo;,&rsquo; part). But the `:id` column is
2058
+ numeric, and the second parameter overrides the formatting for numerics in
2059
+ general and calls for the `:id` column to be padded to three digits with zeros
2060
+ on the left (the &rsquo;3.0&rsquo; part) and to be centered (the &rsquo;C&rsquo; part).
2061
+
2062
+ tab_a.to_aoa do |f|
2063
+ f.format(numeric: '0.0,R', id: '3.0C')
2064
+ end
2065
+
2066
+ | Id | Name | Age | Address | Salary | Join Date |
2067
+ |-----|-------|-----|------------|--------|------------|
2068
+ | 001 | Paul | 32 | California | 20,000 | 2001-07-13 |
2069
+ | 003 | Teddy | 23 | Norway | 20,000 | 2007-12-13 |
2070
+ | 004 | Mark | 25 | Rich-Mond | 65,000 | 2007-12-13 |
2071
+ | 005 | David | 27 | Texas | 85,000 | 2007-12-13 |
2072
+ | 002 | Allen | 25 | Texas | | 2005-07-13 |
2073
+ | 008 | Paul | 24 | Houston | 20,000 | 2005-07-13 |
2074
+ | 009 | James | 44 | Norway | 5,000 | 2005-07-13 |
2075
+ | 010 | James | 45 | Texas | 5,000 | |
2076
+
2077
+ The `numeric:` directive affected the `:age` and `:salary` columns and the `id:`
2078
+ directive affected only the `:id` column. All the other cells in the table had
2079
+ the default formatting applied.
2080
+
2081
+ 1. Location priority
2082
+
2083
+ Formatting for any given cell depends on its location in the table. The
2084
+ `format_for` method takes a location to which its formatting directive are
2085
+ restricted as the first argument. It can be one of the following:
2086
+
2087
+ - **`:header`:** directive apply only to the header row, that is the first row, of
2088
+ the output table,
2089
+
2090
+ - **`:footer`:** directives apply to all the footer rows of the output table,
2091
+ regardless of how many there are,
2092
+
2093
+ - **`gfooter`:** directives apply to all group footer rows of the output tables,
2094
+ regardless of how many there are,
2095
+
2096
+ - **`:body`:** directives apply to all rows in the body of the table unless the
2097
+ row is the first row in the table or in a group and separate directives for
2098
+ those have been given, in which case those directives apply,
2099
+
2100
+ - **`:gfirst`:** directives apply to the first row in each group in the body of
2101
+ the table, unless the row is also the first row in the table as a whole, in
2102
+ which case the `:bfirst` directives apply,
2103
+
2104
+ - **`:bfirst`:** directives apply to the first row in the body of the table.
2105
+
2106
+ If you give directives for `:body`, they are copied to `:bfirst` and `:gfirst`
2107
+ as well and can be overridden by directives for those locations.
2108
+
2109
+ Directives given to the `format` method apply the directives to all locations in
2110
+ the table, but they can be overridden by more specific directives given in a
2111
+ `format_for` directive.
2112
+
2113
+ 2. Type and Column priority
2114
+
2115
+ A directive based on type applies to all columns having that type unless
2116
+ overridden by a directive specific to a named column; a directive based on a
2117
+ column name applies only to cells in that column.
2118
+
2119
+ However, there is a twist. Since the end result of formatting is to convert all
2120
+ columns to strings, the formatting directives for the `:string` type applies to
2121
+ all columns. Likewise, since all columns may contain nils, the `nil:` type
2122
+ applies to nils in all columns regardless of the column&rsquo;s type.
2123
+
2124
+ require 'fat_table'
2125
+ tab_a.to_text do |f|
2126
+ f.format(string: 'R', id: '3.0C', salary: 'n[N/A]')
2127
+ end
2128
+
2129
+ +=====+=======+=====+============+========+============+
2130
+ | Id | Name | Age | Address | Salary | Join Date |
2131
+ +-----|-------|-----|------------|--------|------------+
2132
+ | 001 | Paul | 32 | California | 20000 | 2001-07-13 |
2133
+ | 003 | Teddy | 23 | Norway | 20000 | 2007-12-13 |
2134
+ | 004 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13 |
2135
+ | 005 | David | 27 | Texas | 85000 | 2007-12-13 |
2136
+ | 002 | Allen | 25 | Texas | N/A | 2005-07-13 |
2137
+ | 008 | Paul | 24 | Houston | 20000 | 2005-07-13 |
2138
+ | 009 | James | 44 | Norway | 5000 | 2005-07-13 |
2139
+ | 010 | James | 45 | Texas | 5000 | |
2140
+ +=====+=======+=====+============+========+============+
2141
+
2142
+ The `string: 'R'` directive causes all the cells to be right-aligned except
2143
+ `:id` which specifies centering for the `:id` column only. The `n[N/A]`
2144
+ directive for specifies how nil are displayed in the numeric column, `:salary`,
2145
+ but not for other nils, such as in the last row of the `:join_date` column.
2146
+
2147
+
2148
+ <a id="org62e325b"></a>
2149
+
2150
+ # Development
2151
+
2152
+ After checking out the repo, run \`bin/setup\` to install dependencies. Then, run
2153
+ \`rake spec\` to run the tests. You can also run \`bin/console\` for an interactive
2154
+ prompt that will allow you to experiment.
2155
+
2156
+ To install this gem onto your local machine, run \`bundle exec rake install\`. To
2157
+ release a new version, update the version number in \`version.rb\`, and then run
2158
+ \`bundle exec rake release\`, which will create a git tag for the version, push
2159
+ git commits and tags, and push the \`.gem\` file to
2160
+ [rubygems.org](<https://rubygems.org>).
2161
+
2162
+
2163
+ <a id="orgf51a2c9"></a>
2164
+
2165
+ # Contributing
2166
+
2167
+ Bug reports and pull requests are welcome on GitHub at
2168
+ <https://github.com/ddoherty03/fat_table>.