red_amber 0.1.2 → 0.1.5

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.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +21 -10
  3. data/CHANGELOG.md +162 -6
  4. data/Gemfile +3 -0
  5. data/README.md +89 -303
  6. data/benchmark/csv_load_penguins.yml +15 -0
  7. data/benchmark/drop_nil.yml +11 -0
  8. data/doc/DataFrame.md +840 -0
  9. data/doc/Vector.md +317 -0
  10. data/doc/image/arrow_table_new.png +0 -0
  11. data/doc/image/dataframe/assign.png +0 -0
  12. data/doc/image/dataframe/drop.png +0 -0
  13. data/doc/image/dataframe/pick.png +0 -0
  14. data/doc/image/dataframe/remove.png +0 -0
  15. data/doc/image/dataframe/rename.png +0 -0
  16. data/doc/image/dataframe/slice.png +0 -0
  17. data/doc/image/dataframe_model.png +0 -0
  18. data/doc/image/example_in_red_arrow.png +0 -0
  19. data/doc/image/tdr.png +0 -0
  20. data/doc/image/tdr_and_table.png +0 -0
  21. data/doc/image/tidy_data_in_TDR.png +0 -0
  22. data/doc/image/vector/binary_element_wise.png +0 -0
  23. data/doc/image/vector/unary_aggregation.png +0 -0
  24. data/doc/image/vector/unary_aggregation_w_option.png +0 -0
  25. data/doc/image/vector/unary_element_wise.png +0 -0
  26. data/doc/tdr.md +56 -0
  27. data/doc/tdr_ja.md +56 -0
  28. data/lib/red_amber/data_frame.rb +68 -35
  29. data/lib/red_amber/data_frame_displayable.rb +132 -0
  30. data/lib/red_amber/data_frame_helper.rb +64 -0
  31. data/lib/red_amber/data_frame_indexable.rb +38 -0
  32. data/lib/red_amber/data_frame_observation_operation.rb +83 -0
  33. data/lib/red_amber/data_frame_selectable.rb +34 -43
  34. data/lib/red_amber/data_frame_variable_operation.rb +133 -0
  35. data/lib/red_amber/vector.rb +58 -6
  36. data/lib/red_amber/vector_compensable.rb +68 -0
  37. data/lib/red_amber/vector_functions.rb +147 -68
  38. data/lib/red_amber/version.rb +1 -1
  39. data/lib/red_amber.rb +9 -1
  40. data/red_amber.gemspec +3 -6
  41. metadata +36 -9
  42. data/lib/red_amber/data_frame_output.rb +0 -116
data/doc/Vector.md ADDED
@@ -0,0 +1,317 @@
1
+ # Vector
2
+
3
+ Class `RedAmber::Vector` represents a series of data in the DataFrame.
4
+
5
+ ## Constructor
6
+
7
+ ### Create from a column in a DataFrame
8
+
9
+ ```ruby
10
+ df = RedAmber::DataFrame.new(x: [1, 2, 3])
11
+ df[:x]
12
+ # =>
13
+ #<RedAmber::Vector(:uint8, size=3):0x000000000000f4ec>
14
+ [1, 2, 3]
15
+ ```
16
+
17
+ ### New from an Array
18
+
19
+ ```ruby
20
+ vector = RedAmber::Vector.new([1, 2, 3])
21
+ # =>
22
+ #<RedAmber::Vector(:uint8, size=3):0x000000000000f514>
23
+ [1, 2, 3]
24
+ ```
25
+
26
+ ## Properties
27
+
28
+ ### `to_s`
29
+
30
+ ### `values`, `to_a`, `entries`
31
+
32
+ ### `size`, `length`, `n_rows`, `nrow`
33
+
34
+ ### `type`
35
+
36
+ ### `boolean?`, `numeric?`, `string?`, `temporal?`
37
+
38
+ ### `type_class`
39
+
40
+ ### [ ] `each` (not impremented yet)
41
+
42
+ ### [ ] `chunked?` (not impremented yet)
43
+
44
+ ### [ ] `n_chunks` (not impremented yet)
45
+
46
+ ### [ ] `each_chunk` (not impremented yet)
47
+
48
+ ### `n_nils`, `n_nans`
49
+
50
+ - `n_nulls` is an alias of `n_nils`
51
+
52
+ ### `inspect(limit: 80)`
53
+
54
+ - `limit` sets size limit to display long array.
55
+
56
+ ```ruby
57
+ vector = RedAmber::Vector.new((1..50).to_a)
58
+ # =>
59
+ #<RedAmber::Vector(:uint8, size=50):0x000000000000f528>
60
+ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, ... ]
61
+ ```
62
+
63
+ ## Functions
64
+
65
+ ### Unary aggregations: `vector.func => scalar`
66
+
67
+ ![unary aggregation](doc/image/../../image/vector/unary_aggregation_w_option.png)
68
+
69
+ | Method |Boolean|Numeric|String|Options|Remarks|
70
+ | ----------- | --- | --- | --- | --- | --- |
71
+ | ✓ `all` | ✓ | | | ✓ ScalarAggregate| |
72
+ | ✓ `any` | ✓ | | | ✓ ScalarAggregate| |
73
+ | ✓ `approximate_median`| |✓| | ✓ ScalarAggregate| alias `median`|
74
+ | ✓ `count` | ✓ | ✓ | ✓ | ✓ Count | |
75
+ | ✓ `count_distinct`| ✓ | ✓ | ✓ | ✓ Count |alias `count_uniq`|
76
+ |[ ]`index` | [ ] | [ ] | [ ] |[ ] Index | |
77
+ | ✓ `max` | ✓ | ✓ | ✓ | ✓ ScalarAggregate| |
78
+ | ✓ `mean` | ✓ | ✓ | | ✓ ScalarAggregate| |
79
+ | ✓ `min` | ✓ | ✓ | ✓ | ✓ ScalarAggregate| |
80
+ | ✓ `min_max` | ✓ | ✓ | ✓ | ✓ ScalarAggregate| |
81
+ |[ ]`mode` | | [ ] | |[ ] Mode | |
82
+ | ✓ `product` | ✓ | ✓ | | ✓ ScalarAggregate| |
83
+ |[ ]`quantile`| | [ ] | |[ ] Quantile| |
84
+ | ✓ `sd ` | | ✓ | | |ddof: 1 at `stddev`|
85
+ | ✓ `stddev` | | ✓ | | ✓ Variance|ddof: 0 by default|
86
+ | ✓ `sum` | ✓ | ✓ | | ✓ ScalarAggregate| |
87
+ |[ ]`tdigest` | | [ ] | |[ ] TDigest | |
88
+ | ✓ `var `| | ✓ | | |ddof: 1 at `variance`<br>alias `unbiased_variance`|
89
+ | ✓ `variance`| | ✓ | | ✓ Variance|ddof: 0 by default|
90
+
91
+
92
+ Options can be used as follows.
93
+ See the [document of C++ function](https://arrow.apache.org/docs/cpp/compute.html) for detail.
94
+
95
+ ```ruby
96
+ double = RedAmber::Vector.new([1, 0/0.0, -1/0.0, 1/0.0, nil, ""])
97
+ #=>
98
+ #<RedAmber::Vector(:double, size=6):0x000000000000f910>
99
+ [1.0, NaN, -Infinity, Infinity, nil, 0.0]
100
+
101
+ double.count #=> 5
102
+ double.count(opts: {mode: :only_valid}) #=> 5, default
103
+ double.count(opts: {mode: :only_null}) #=> 1
104
+ double.count(opts: {mode: :all}) #=> 6
105
+
106
+ boolean = RedAmber::Vector.new([true, true, nil])
107
+ #=>
108
+ #<RedAmber::Vector(:boolean, size=3):0x000000000000f924>
109
+ [true, true, nil]
110
+
111
+ boolean.all #=> true
112
+ boolean.all(opts: {skip_nulls: true}) #=> true
113
+ boolean.all(opts: {skip_nulls: false}) #=> false
114
+ ```
115
+
116
+ ### Unary element-wise: `vector.func => vector`
117
+
118
+ ![unary element-wise](doc/image/../../image/vector/unary_element_wise.png)
119
+
120
+ | Method |Boolean|Numeric|String|Options|Remarks|
121
+ | ------------ | --- | --- | --- | --- | ----- |
122
+ | ✓ `-@` | | ✓ | | |as `-vector`|
123
+ | ✓ `negate` | | ✓ | | |`-@` |
124
+ | ✓ `abs` | | ✓ | | | |
125
+ |[ ]`acos` | | [ ] | | | |
126
+ |[ ]`asin` | | [ ] | | | |
127
+ | ✓ `atan` | | ✓ | | | |
128
+ | ✓ `bit_wise_not`| | (✓) | | |integer only|
129
+ | ✓ `ceil` | | ✓ | | | |
130
+ | ✓ `cos` | | ✓ | | | |
131
+ | ✓`fill_nil_backward`| ✓ | ✓ | ✓ | | |
132
+ | ✓`fill_nil_forward` | ✓ | ✓ | ✓ | | |
133
+ | ✓ `floor` | | ✓ | | | |
134
+ | ✓ `invert` | ✓ | | | |`!`, alias `not`|
135
+ |[ ]`ln` | | [ ] | | | |
136
+ |[ ]`log10` | | [ ] | | | |
137
+ |[ ]`log1p` | | [ ] | | | |
138
+ |[ ]`log2` | | [ ] | | | |
139
+ | ✓ `round` | | ✓ | | ✓ Round (:mode, :n_digits)| |
140
+ | ✓ `round_to_multiple`| | ✓ | | ✓ RoundToMultiple :mode, :multiple| multiple must be an Arrow::Scalar|
141
+ | ✓ `sign` | | ✓ | | | |
142
+ | ✓ `sin` | | ✓ | | | |
143
+ | ✓`sort_indexes`| ✓ | ✓ | ✓ |:order|alias `sort_indices`|
144
+ | ✓ `tan` | | ✓ | | | |
145
+ | ✓ `trunc` | | ✓ | | | |
146
+
147
+ ### Binary element-wise: `vector.func(vector) => vector`
148
+
149
+ ![binary element-wise](doc/image/../../image/vector/binary_element_wise.png)
150
+
151
+ | Method |Boolean|Numeric|String|Options|Remarks|
152
+ | ----------------- | --- | --- | --- | --- | ----- |
153
+ | ✓ `add` | | ✓ | | | `+` |
154
+ | ✓ `atan2` | | ✓ | | | |
155
+ | ✓ `and_kleene` | ✓ | | | | `&` |
156
+ | ✓ `and_org ` | ✓ | | | |`and` in Red Arrow|
157
+ | ✓ `and_not` | ✓ | | | | |
158
+ | ✓ `and_not_kleene`| ✓ | | | | |
159
+ | ✓ `bit_wise_and` | | (✓) | | |integer only|
160
+ | ✓ `bit_wise_or` | | (✓) | | |integer only|
161
+ | ✓ `bit_wise_xor` | | (✓) | | |integer only|
162
+ | ✓ `divide` | | ✓ | | | `/` |
163
+ | ✓ `equal` | ✓ | ✓ | ✓ | |`==`, alias `eq`|
164
+ | ✓ `greater` | ✓ | ✓ | ✓ | |`>`, alias `gt`|
165
+ | ✓ `greater_equal` | ✓ | ✓ | ✓ | |`>=`, alias `ge`|
166
+ | ✓ `is_finite` | | ✓ | | | |
167
+ | ✓ `is_inf` | | ✓ | | | |
168
+ | ✓ `is_na` | ✓ | ✓ | ✓ | | |
169
+ | ✓ `is_nan` | | ✓ | | | |
170
+ |[ ]`is_nil` | ✓ | ✓ | ✓ |[ ] Null|alias `is_null`|
171
+ | ✓ `is_valid` | ✓ | ✓ | ✓ | | |
172
+ | ✓ `less` | ✓ | ✓ | ✓ | |`<`, alias `lt`|
173
+ | ✓ `less_equal` | ✓ | ✓ | ✓ | |`<=`, alias `le`|
174
+ |[ ]`logb` | | [ ] | | | |
175
+ |[ ]`mod` | | [ ] | | | `%` |
176
+ | ✓ `multiply` | | ✓ | | | `*` |
177
+ | ✓ `not_equal` | ✓ | ✓ | ✓ | |`!=`, alias `ne`|
178
+ | ✓ `or_kleene` | ✓ | | | | `\|` |
179
+ | ✓ `or_org` | ✓ | | | |`or` in Red Arrow|
180
+ | ✓ `power` | | ✓ | | | `**` |
181
+ | ✓ `subtract` | | ✓ | | | `-` |
182
+ | ✓ `shift_left` | | (✓) | | |`<<`, integer only|
183
+ | ✓ `shift_right` | | (✓) | | |`>>`, integer only|
184
+ | ✓ `xor` | ✓ | | | | `^` |
185
+
186
+ ### `uniq`
187
+
188
+ Returns a new array with distinct elements.
189
+
190
+ (Not impremented functions)
191
+
192
+ ### `tally` and `value_counts`
193
+
194
+ Compute counts of unique elements and return a Hash.
195
+
196
+ It returns almost same result as Ruby's tally. These methods consider NaNs are same.
197
+
198
+ ```ruby
199
+ array = [0.0/0, Float::NAN]
200
+ array.tally #=> {NaN=>1, NaN=>1}
201
+
202
+ vector = RedAmber::Vector.new(array)
203
+ vector.tally #=> {NaN=>2}
204
+ vector.value_counts #=> {NaN=>2}
205
+ ```
206
+
207
+ ### `sort_indexes`, `sort_indices`, `array_sort_indices`
208
+
209
+ ### [ ] `sort`, `sort_by`
210
+ ### [ ] argmin, argmax
211
+ ### [ ] (array functions)
212
+ ### [ ] (strings functions)
213
+ ### [ ] (temporal functions)
214
+ ### [ ] (conditional functions)
215
+ ### [ ] (index functions)
216
+ ### [ ] (other functions)
217
+
218
+ ## Coerce (not impremented)
219
+
220
+ ## Update vector's value
221
+ ### `replace_with(booleans, replacements)` => vector
222
+
223
+ - Accepts Vector, Array, Arrow::Array for booleans and replacements.
224
+ - Replacements can accept scalar
225
+ - Booleans specifies the position of replacement in true.
226
+ - Replacements specifies the vaues to be replaced.
227
+ - The number of true in booleans must be equal to the length of replacement
228
+
229
+ ```ruby
230
+ vector = RedAmber::Vector.new([1, 2, 3])
231
+ booleans = [true, false, true]
232
+ replacemants = [4, 5]
233
+ vector.replace_with(booleans, replacemants)
234
+ # =>
235
+ #<RedAmber::Vector(:uint8, size=3):0x000000000001ee10>
236
+ [4, 2, 5]
237
+ ```
238
+
239
+ - Scalar value in replacements can be broadcasted.
240
+
241
+ ```ruby
242
+ replacemant = 0
243
+ vector.replace_with(booleans, replacement)
244
+ # =>
245
+ #<RedAmber::Vector(:uint8, size=3):0x000000000001ee10>
246
+ [0, 2, 0]
247
+ ```
248
+
249
+ - Returned data type is automatically up-casted by replacement.
250
+
251
+ ```ruby
252
+ replacement = 1.0
253
+ vector.replace_with(booleans, replacement)
254
+ # =>
255
+ #<RedAmber::Vector(:double, size=3):0x0000000000025d78>
256
+ [1.0, 2.0, 1.0]
257
+ ```
258
+
259
+ - Position of nil in booleans is replaced with nil.
260
+
261
+ ```ruby
262
+ booleans = [true, false, nil]
263
+ replacemant = -1
264
+ vec.replace_with(booleans, replacement)
265
+ =>
266
+ #<RedAmber::Vector(:int8, size=3):0x00000000000304d0>
267
+ [-1, 2, nil]
268
+ ```
269
+
270
+ - Replacemants can have nil in it.
271
+
272
+ ```ruby
273
+ booleans = [true, false, true]
274
+ replacemants = [nil]
275
+ vec.replace_with(booleans, replacemants)
276
+ =>
277
+ #<RedAmber::Vector(:int8, size=3):0x00000000000304d0>
278
+ [nil, 2, nil]
279
+ ```
280
+
281
+ - If no replacemants specified, it is same as to specify nil.
282
+
283
+ ```ruby
284
+ booleans = [true, false, true]
285
+ vec.replace_with(booleans)
286
+ =>
287
+ #<RedAmber::Vector(:int8, size=3):0x00000000000304d0>
288
+ [nil, 2, nil]
289
+ ```
290
+
291
+ - An example to replace 'NA' to nil.
292
+
293
+ ```ruby
294
+ vector = RedAmber::Vector.new(['A', 'B', 'NA'])
295
+ vector.replace_with(vector == 'NA', nil)
296
+ # =>
297
+ #<RedAmber::Vector(:string, size=3):0x000000000000f8ac>
298
+ ["A", "B", nil]
299
+ ```
300
+
301
+ ### `fill_nil_forward`, `fill_nil_backward` => vector
302
+
303
+ Propagate the last valid observation forward (or backward).
304
+ Or preserve nil if all previous values are nil or at the end.
305
+
306
+ ```ruby
307
+ integer = RedAmber::Vector.new([0, 1, nil, 3, nil])
308
+ integer.fill_nil_forward
309
+ # =>
310
+ #<RedAmber::Vector(:uint8, size=5):0x000000000000f960>
311
+ [0, 1, 1, 3, 3]
312
+
313
+ integer.fill_nil_backward
314
+ # =>
315
+ #<RedAmber::Vector(:uint8, size=5):0x000000000000f974>
316
+ [0, 1, 3, 3, nil]
317
+ ```
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
data/doc/image/tdr.png ADDED
Binary file
Binary file
Binary file
data/doc/tdr.md ADDED
@@ -0,0 +1,56 @@
1
+ # TDR (Transposed DataFrame Representation)
2
+
3
+ ([Japanese version](tdr_ja.md) of this document is available)
4
+
5
+ TDR is a presentation style of 2D data. It shows columnar vector values in *row Vector* and observations in *column* just like a **transposed** table.
6
+
7
+ ![TDR Image](image/tdr.png)
8
+
9
+ Row-oriented data table (1) and columnar data table (2) have different data allocation in memory within a context of Arrow Columnar Format. But they have the same data placement (in rows and columns) in our brain.
10
+
11
+ TDR (3) is a logical concept of data placement to transpose rows and columns in a columnar table (2).
12
+
13
+ ![TDR and Table Image](image/tdr_and_table.png)
14
+
15
+ TDR is not an implementation in software but a logical image in our mind.
16
+
17
+ TDR is consistent with the 'transposed' tidy data concept. The only thing we should do is not to use the positional words 'row' and 'column'.
18
+
19
+ ![tidy data in TDR](image/tidy_data_in_TDR.png)
20
+
21
+ TDR is one of a simple way to create DataFrame object in many libraries. For example, we can initalize Arrow::Table in Red Arrow like the right below and get table as left.
22
+
23
+ ![Arrow Table New](image/arrow_table_new.png)
24
+
25
+ We are using TDR style code naturally. For other example:
26
+ - Ruby: Daru::DataFrame, Rover::DataFrame accept same arguments.
27
+ - Python: similar style in Pandas for pd.DataFrame(data_in_dict)
28
+ - R: similar style in tidyr for tibble(x = 1:3, y = c("A", "B", "C"))
29
+
30
+ There are other ways to initialize data frame, but they are not intuitive.
31
+
32
+ ## Table and TDR API
33
+
34
+ The API based on TDR is draft and RedAmber is a small experiment to test the TDR concept. The following is a comparison of Table and TDR (draft).
35
+
36
+ | |Basic Table|Transposed DataFrame|Comment for TDR|
37
+ |-----------|---------|------------|---|
38
+ |name in TDR|`Table`|`TDR`|**T**ransposed **D**ataFrame **R**epresentation|
39
+ |variable |located in a column|a key and a `Vector` in lateral|select by keys|
40
+ |observation|located in a row|sliced in vertical|select by indices|
41
+ |number of variables|n_columns etc. |`n_keys` |`n_cols` is available as an alias|
42
+ |number of observations|n_rows etc. |`size` |`n_rows` is available as an alias|
43
+ |shape |[n_rows, n_columns] |`shape`=`[size, n_keys]` |same order as Table|
44
+ |Select variables|select, filter, [ ], etc.|`pick` or `[keys]` |accepts arguments or a block|
45
+ |Reject variables|drop, etc.|`drop` |accepts arguments or a block|
46
+ |Select observations|slice, [ ], iloc, etc.|`slice` or `[indices]` |accepts arguments or a block|
47
+ |Reject observations|drop, etc.|`remove` |accepts arguments or a block|
48
+ |Add variables|mutate, assign, etc.|`assign` |accepts arguments or a block|
49
+ |update variables|transmute, [ ]=, etc.|`assign` |accepts arguments or a block|
50
+ |inner join| inner_join(a,b)<br>merge(a, b, how='inner')|`a.inner_join(b)` |with a option on:|
51
+ |left join| left_join(a,b)<br>merge(a, b, how='left')|`a.join(b)` |naturally join from bottom<br>with a option on:|
52
+ |right join| right_join(a,b))<br>merge(a, b, how='right')|`b.join(a)` |naturally join from bottom<br>with a option on:|
53
+
54
+ ## Q and A for TDR
55
+
56
+ (Not prepared yet)
data/doc/tdr_ja.md ADDED
@@ -0,0 +1,56 @@
1
+ # TDR (Transposed DataFrame Representation)
2
+
3
+ ([英語版](tdr.md) もあります)
4
+
5
+ TDR は、2次元のデータの表現方法につけた名前です。TDR では下の図のように同じ型のデータに key というラベルをつけて横に並べ、それらを縦に積み重ねてデータを表現します。
6
+
7
+ ![TDR Image](image/tdr.png)
8
+
9
+ Arrow Columnar Format では、csv のような従来の行指向データ(1)に対して、列方向に連続したデータ(2)を取り扱います。この行、列という言葉は私たちの脳内イメージを規定していて、データフレームの構造といえば(1)または(2)のような形を思い浮かべることでしょう。しかし、本質は連続したデータの配置にあるので、我々の頭の中では(3)のように行と列を入れ替えて考えてもいいはずです。
10
+
11
+ ![TDR and Table Image](image/tdr_and_table.png)
12
+
13
+ 大事なことは、TDR は頭の中の論理的なイメージであって、実装上のアーキテクチャではないということです。
14
+
15
+ TDR は、整然データ(tidy data)の考え方とも矛盾しません。TDR における整然データは行と列を入れ替えた形で全く同じデータを表しています。一つだけ気をつけることは、混乱を避けるため、位置や方向に関するワードである行(row)や列(column)を避けるべきであるということです。
16
+
17
+ ![tidy data in TDR](image/tidy_data_in_TDR.png)
18
+
19
+ TDR は、現時点でも2次元データを楽に初期化できる記法で、ごく自然に使われています。例えば、Red Arrow ではArrow::Table を初期化する際に下の図の右のように書けます。
20
+
21
+ ![Arrow Table New](image/arrow_table_new.png)
22
+
23
+ これはごく自然な書き方ですが、この形は TDR の形と一致しています。その他の例として:
24
+ - Ruby: Daru::DataFrame, Rover::DataFrame でも上と同じように書けます。
25
+ - Python: Pandas で pd.DataFrame(data_in_dict) のように dict を使う場合が同じです。
26
+ - R: tidyr で tibble(x = 1:3, y = c("A", "B", "C")) のように書けます。
27
+
28
+ それぞれのライブラリーで、データフレームを初期化するやり方はこれだけではありませんが、他の方法は少し回りくどいような印象があります。
29
+
30
+ TDR で考えた方がちょっぴりうまくいくというのは単なる仮説ですが、その理由は「この惑星では横書きでコードを書く」からではないかと私は考えています。
31
+
32
+ ## Table and TDR API
33
+
34
+ TDR に基づいた API はまだ暫定板の段階であり、RedAmber は TDR の実験の場であると考えています。下記の表に TDR と行x列形式の Table のAPIの比較を示します(暫定版)。
35
+
36
+ | |従来の Table|Transposed DataFrame|TDRに対するコメント|
37
+ |-----------|---------|------------|---|
38
+ |TDRでの呼称|`Table`|`TDR`|**T**ransposed **D**ataFrame **R**epresentationの略|
39
+ |変数 |列に配置|`variables`<br>key と `Vector` として横方向に配置|key で選択|
40
+ |観測 |行に配置|`observations`<br>縦方向に切った一つ一つはslice|index や `slice` メソッドで選択|
41
+ |変数(列)の数|ncol, n_columns など |`n_keys` |`n_cols` をエイリアスとして設定|
42
+ |観測(行)の数|nrow, n_rows など |`size` |`n_rows` をエイリアスとして設定|
43
+ |形状 |[nrow, ncol] |`shape`=`[size, n_keys]` |行, 列の順番は同じ|
44
+ |変数(列)の選択|select, filter, [ ], など|`pick` or `[keys]` |引数またはブロックで指定|
45
+ |変数(列)の削除|drop, など|`drop` |引数またはブロックで指定|
46
+ |観測(行)の選択|slice, [ ], iloc, など|`slice` or `[indices]` |引数またはブロックで指定|
47
+ |観測(行)の削除|drop, など|`remove` |引数またはブロックで指定|
48
+ |変数(列)の追加|mutate, assign, など|`assign` |引数またはブロックで指定|
49
+ |変数(列)の更新|transmute, [ ]=, など|`assign` |引数またはブロックで指定|
50
+ |内部結合| inner_join(a,b)<br>merge(a, b, how='inner')|`a.inner_join(b)` |オプション on:|
51
+ |左結合| left_join(a,b)<br>merge(a, b, how='left')|`a.join(b)` |自然に下にくっつける<br>オプション on:|
52
+ |右結合| right_join(a,b))<br>merge(a, b, how='right')|`b.join(a)` |自然に下にくっつける<br>オプション on:|
53
+
54
+ ## Q and A for TDR
55
+
56
+ (作成中)
@@ -5,19 +5,23 @@ module RedAmber
5
5
  # @table : holds Arrow::Table object
6
6
  class DataFrame
7
7
  # mix-in
8
+ include DataFrameDisplayable
9
+ include DataFrameHelper
10
+ include DataFrameIndexable
8
11
  include DataFrameSelectable
9
- include DataFrameOutput
12
+ include DataFrameObservationOperation
13
+ include DataFrameVariableOperation
10
14
 
11
15
  def initialize(*args)
12
- # DataFrame.new, DataFrame.new([]), DataFrame.new({}), DataFrame.new(nil)
13
- # returns empty DataFrame
14
- @table = Arrow::Table.new({}, [])
16
+ @variables = @keys = @vectors = @types = @data_types = nil
15
17
  # bug in gobject-introspection: ruby-gnome/ruby-gnome#1472
16
18
  # [Arrow::Table] == [nil] shows ArgumentError
17
19
  # temporary use yoda condition to workaround
18
- return if args.empty? || args == [[]] || args == [{}] || [nil] == args
19
-
20
- if args.size > 1
20
+ if args.empty? || args == [[]] || args == [{}] || [nil] == args
21
+ # DataFrame.new, DataFrame.new([]), DataFrame.new({}), DataFrame.new(nil)
22
+ # returns empty DataFrame
23
+ @table = Arrow::Table.new({}, [])
24
+ elsif args.size > 1
21
25
  @table = Arrow::Table.new(*args)
22
26
  else
23
27
  arg = args[0]
@@ -39,56 +43,71 @@ module RedAmber
39
43
 
40
44
  attr_reader :table
41
45
 
46
+ def to_arrow
47
+ table
48
+ end
49
+
42
50
  def save(output, options = {})
43
51
  @table.save(output, options)
44
52
  end
45
53
 
46
- # Properties ===
47
- def n_rows
54
+ def size
48
55
  @table.n_rows
49
56
  end
50
- alias_method :nrow, :n_rows
51
- alias_method :size, :n_rows
52
- alias_method :length, :n_rows
57
+ alias_method :n_rows, :size
58
+ alias_method :n_obs, :size
53
59
 
54
- def n_columns
60
+ def n_keys
55
61
  @table.n_columns
56
62
  end
57
- alias_method :ncol, :n_columns
58
- alias_method :width, :n_columns
63
+ alias_method :n_cols, :n_keys
64
+ alias_method :n_vars, :n_keys
59
65
 
60
66
  def shape
61
- [n_rows, n_columns]
67
+ [size, n_keys]
68
+ end
69
+
70
+ def variables
71
+ @variables || @variables = init_instance_vars(:variables)
72
+ end
73
+ alias_method :vars, :variables
74
+
75
+ def keys
76
+ @keys || @keys = init_instance_vars(:keys)
77
+ end
78
+ alias_method :column_names, :keys
79
+ alias_method :var_names, :keys
80
+
81
+ def key?(key)
82
+ keys.include?(key.to_sym)
62
83
  end
84
+ alias_method :has_key?, :key?
63
85
 
64
- def column_names
65
- @table.columns.map { |column| column.name.to_sym }
86
+ def key_index(key)
87
+ keys.find_index(key.to_sym)
66
88
  end
67
- alias_method :keys, :column_names
68
- alias_method :header, :column_names
89
+ alias_method :find_index, :key_index
90
+ alias_method :index, :key_index
69
91
 
70
92
  def types
71
- @table.columns.map do |column|
72
- column.data_type.to_s.to_sym
73
- end
93
+ @types || @types = @table.columns.map { |column| column.data.value_type.nick.to_sym }
74
94
  end
75
95
 
76
- def data_types
77
- @table.columns.map do |column|
78
- column.data_type.class
79
- end
96
+ def type_classes
97
+ @data_types || @data_types = @table.columns.map { |column| column.data_type.class }
80
98
  end
81
99
 
82
100
  def vectors
83
- @table.columns.map do |column|
84
- Vector.new(column.data)
85
- end
101
+ @vectors || @vectors = init_instance_vars(:vectors)
102
+ end
103
+
104
+ def indexes
105
+ 0...size
86
106
  end
107
+ alias_method :indices, :indexes
87
108
 
88
109
  def to_h
89
- @table.columns.each_with_object({}) do |column, result|
90
- result[column.name.to_sym] = column.entries
91
- end
110
+ variables.transform_values(&:to_a)
92
111
  end
93
112
 
94
113
  def to_a
@@ -107,13 +126,27 @@ module RedAmber
107
126
  end
108
127
 
109
128
  def empty?
110
- @table.columns.empty?
129
+ variables.empty?
111
130
  end
112
131
 
113
132
  def to_rover
114
133
  Rover::DataFrame.new(to_h)
115
134
  end
116
135
 
117
- # def to_parquet() end
136
+ private
137
+
138
+ # initialize @variable, @keys, @vectors and return one of them
139
+ def init_instance_vars(var)
140
+ ary = @table.columns.each_with_object([{}, [], []]) do |column, (variables, keys, vectors)|
141
+ v = Vector.new(column.data)
142
+ k = column.name.to_sym
143
+ v.key = k
144
+ variables[k] = v
145
+ keys << k
146
+ vectors << v
147
+ end
148
+ @variables, @keys, @vectors = ary
149
+ ary[%i[variables keys vectors].index(var)]
150
+ end
118
151
  end
119
152
  end