red_amber 0.2.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +133 -51
  3. data/.yardopts +2 -0
  4. data/CHANGELOG.md +203 -1
  5. data/Gemfile +2 -1
  6. data/LICENSE +1 -1
  7. data/README.md +61 -45
  8. data/benchmark/basic.yml +11 -4
  9. data/benchmark/combine.yml +3 -4
  10. data/benchmark/dataframe.yml +62 -0
  11. data/benchmark/group.yml +7 -1
  12. data/benchmark/reshape.yml +6 -2
  13. data/benchmark/vector.yml +63 -0
  14. data/doc/DataFrame.md +35 -12
  15. data/doc/DataFrame_Comparison.md +65 -0
  16. data/doc/SubFrames.md +11 -0
  17. data/doc/Vector.md +295 -1
  18. data/doc/yard-templates/default/fulldoc/html/css/common.css +6 -0
  19. data/lib/red_amber/data_frame.rb +537 -68
  20. data/lib/red_amber/data_frame_combinable.rb +776 -123
  21. data/lib/red_amber/data_frame_displayable.rb +248 -18
  22. data/lib/red_amber/data_frame_indexable.rb +122 -19
  23. data/lib/red_amber/data_frame_loadsave.rb +81 -10
  24. data/lib/red_amber/data_frame_reshaping.rb +216 -21
  25. data/lib/red_amber/data_frame_selectable.rb +781 -120
  26. data/lib/red_amber/data_frame_variable_operation.rb +561 -85
  27. data/lib/red_amber/group.rb +195 -21
  28. data/lib/red_amber/helper.rb +114 -32
  29. data/lib/red_amber/refinements.rb +206 -0
  30. data/lib/red_amber/subframes.rb +1066 -0
  31. data/lib/red_amber/vector.rb +435 -58
  32. data/lib/red_amber/vector_aggregation.rb +312 -0
  33. data/lib/red_amber/vector_binary_element_wise.rb +387 -0
  34. data/lib/red_amber/vector_selectable.rb +321 -69
  35. data/lib/red_amber/vector_unary_element_wise.rb +436 -0
  36. data/lib/red_amber/vector_updatable.rb +397 -24
  37. data/lib/red_amber/version.rb +2 -1
  38. data/lib/red_amber.rb +15 -1
  39. data/red_amber.gemspec +4 -3
  40. metadata +19 -11
  41. data/doc/image/dataframe/reshaping_DataFrames.png +0 -0
  42. data/lib/red_amber/vector_functions.rb +0 -294
@@ -0,0 +1,436 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Available functions in Arrow are shown by `Arrow::Function.all.map(&:name)`
4
+ # reference: https://arrow.apache.org/docs/cpp/compute.html
5
+
6
+ module RedAmber
7
+ # Representing a series of data.
8
+ class Vector
9
+ class << self
10
+ private
11
+
12
+ # @!macro [attach] define_unary_element_wise
13
+ # [Unary element-wise function] Returns a Vector.
14
+ #
15
+ def define_unary_element_wise(function)
16
+ define_method(function) do |**options|
17
+ datum = exec_func_unary(function, options)
18
+ Vector.create(datum.value)
19
+ end
20
+ end
21
+ end
22
+
23
+ # @!macro array_sort_options
24
+ # @param order [:ascending, :descending]
25
+ # ascending: Arrange values in increasing order.
26
+ # descending: Arrange values in decreasing order.
27
+
28
+ # rubocop:disable Layout/LineLength
29
+
30
+ # @!macro round_mode
31
+ # @param round_mode [:down, :up, :towards_zero, :towards_infinity, :half_down, :half_up, :half_towards_zero, :half_towards_infinity, :half_to_even, :half_to_odd]
32
+ # Rounding and tie-breaking mode.
33
+ # - down: Round to nearest integer less than or equal in magnitude (aka “floor”).
34
+ # - up: Round to nearest integer greater than or equal in magnitude (aka “ceil”).
35
+ # - towards_zero: Get the integral part without fractional digits (aka “trunc”).
36
+ # - towards_infinity: Round negative values with :down rule and positive values
37
+ # with :up rule (aka “away from zero”).
38
+ # - half_down: Round ties with :down rule (also called
39
+ # “round half towards negative infinity”).
40
+ # - half_up: Round ties with :up rule (also called
41
+ # “round half towards positive infinity”).
42
+ # - half_towards_zero: Round ties with :towards_zero rule (also called
43
+ # “round half away from infinity”).
44
+ # - half_towards_infinity: Round ties with :towards_infinity rule (also called
45
+ # “round half away from zero”).
46
+ # - half_to_even: Round ties to nearest even integer.
47
+ # - half_to_odd: Round ties to nearest odd integer.
48
+
49
+ # rubocop:enable Layout/LineLength
50
+
51
+ # Calculate the absolute value of self element-wise.
52
+ #
53
+ # Results will wrap around on integer overflow.
54
+ # @return [Vector]
55
+ # abs of each element of self.
56
+ #
57
+ define_unary_element_wise :abs
58
+
59
+ # Compute the inverse cosine of self element-wise.
60
+ #
61
+ # NaN is returned for invalid input values.
62
+ # @return [Vector]
63
+ # acos of each element of self.
64
+ #
65
+ define_unary_element_wise :acos
66
+
67
+ # Compute the inverse sine of self element-wise.
68
+ #
69
+ # NaN is returned for invalid input values.
70
+ # @return [Vector]
71
+ # asin of each element of self.
72
+ #
73
+ define_unary_element_wise :asin
74
+
75
+ # Return the indices that would sort self.
76
+ #
77
+ # Computes indices Vector that define a stable sort of self.
78
+ # By default, nils are considered greater than any other value
79
+ # and are therefore sorted at the end of the Vector.
80
+ # For floating-point types, NaNs are considered greater than any
81
+ # other non-nil value, but smaller than nil.
82
+ # @!method array_sort_indices(order: :ascending)
83
+ # @macro array_sort_options
84
+ # @return [Vector]
85
+ # sort indices of self.
86
+ #
87
+ define_unary_element_wise :array_sort_indices
88
+ alias_method :sort_indexes, :array_sort_indices
89
+ alias_method :sort_indices, :array_sort_indices
90
+ alias_method :sort_index, :array_sort_indices
91
+
92
+ # Compute the inverse tangent of self element-wise.
93
+ #
94
+ # the return value is in the range [-pi/2, pi/2].
95
+ # For a full return range [-pi, pi], see {.atan2} .
96
+ # @return [Vector]
97
+ # atan of each element of self.
98
+ #
99
+ define_unary_element_wise :atan
100
+
101
+ # Bit-wise negate by element-wise.
102
+ #
103
+ # nil values reeturn nil.
104
+ # @return [Vector]
105
+ # bit wise not of each element of self.
106
+ #
107
+ define_unary_element_wise :bit_wise_not
108
+
109
+ # Round up to the nearest integer.
110
+ #
111
+ # Compute the smallest integer value not less in magnitude than each element.
112
+ # @return [Vector]
113
+ # ceil of each element of self.
114
+ # @example
115
+ # double = Vector.new([15.15, 2.5, 3.5, -4.5, -5.5])
116
+ # double.ceil
117
+ #
118
+ # # =>
119
+ # #<RedAmber::Vector(:double, size=5):0x000000000000cd00>
120
+ # [16.0, 3.0, 4.0, -4.0, -5.0]
121
+ #
122
+ define_unary_element_wise :ceil
123
+
124
+ # Compute the cosine of self element-wise.
125
+ #
126
+ # NaN is returned for invalid input values.
127
+ # @return [Vector]
128
+ # cos of each element of self.
129
+ #
130
+ define_unary_element_wise :cos
131
+
132
+ # Compute cumulative sum over the numeric Vector.
133
+ #
134
+ # @note Self must be numeric.
135
+ # @note Return error for integer overflow.
136
+ # @return [Vector]
137
+ # cumulative sum of self.
138
+ #
139
+ define_unary_element_wise :cumulative_sum_checked
140
+
141
+ # Compute cumulative sum over the numeric Vector.
142
+ #
143
+ # @note Self must be numeric.
144
+ # @note Try to cast to Int64 if integer overflow occured.
145
+ # @return [Vector]
146
+ # cumulative sum of self.
147
+ #
148
+ def cumsum
149
+ cumulative_sum_checked
150
+ rescue Arrow::Error::Invalid
151
+ Vector.create(Arrow::Int64Array.new(data)).cumulative_sum_checked
152
+ end
153
+
154
+ # Carry non-nil values backward to fill nil slots.
155
+ #
156
+ # Propagate next valid value backward to previous nil values.
157
+ # Or nothing if all next values are nil.
158
+ # @return [Vector]
159
+ # a Vector which filled nil backward.
160
+ # @example
161
+ # integer = Vector.new([0, 1, nil, 3, nil])
162
+ # integer.fill_nil_backward
163
+ #
164
+ # # =>
165
+ # #<RedAmber::Vector(:uint8, size=5):0x000000000000f974>
166
+ # [0, 1, 3, 3, nil]
167
+ #
168
+ define_unary_element_wise :fill_null_backward
169
+ alias_method :fill_nil_backward, :fill_null_backward
170
+
171
+ # Carry non-nil values forward to fill nil slots.
172
+ #
173
+ # Propagate last valid value backward to next nil values.
174
+ # Or nothing if all previous values are nil.
175
+ # @return [Vector]
176
+ # a Vector which filled nil forward.
177
+ # @example
178
+ # integer = Vector.new([0, 1, nil, 3, nil])
179
+ # integer.fill_nil_forward
180
+ #
181
+ # # =>
182
+ # #<RedAmber::Vector(:uint8, size=5):0x000000000000f960>
183
+ # [0, 1, 1, 3, 3]
184
+ #
185
+ define_unary_element_wise :fill_null_forward
186
+ alias_method :fill_nil_forward, :fill_null_forward
187
+
188
+ # Round down to the nearest integer.
189
+ #
190
+ # Compute the largest integer value not greater in magnitude than each element.
191
+ # @return [Vector]
192
+ # floor of each element of self.
193
+ # @example
194
+ # double = Vector.new([15.15, 2.5, 3.5, -4.5, -5.5])
195
+ # double.floor
196
+ #
197
+ # # =>
198
+ # #<RedAmber::Vector(:double, size=5):0x000000000000cd14>
199
+ # [15.0, 2.0, 3.0, -5.0, -6.0]
200
+ #
201
+ define_unary_element_wise :floor
202
+
203
+ # Return true if value is finite.
204
+ #
205
+ # For each input value, emit true if the value is finite.
206
+ # (i.e. neither NaN, inf, nor -inf).
207
+ # @return [Vector]
208
+ # boolean Vector wheather each element is finite.
209
+ #
210
+ define_unary_element_wise :is_finite
211
+
212
+ # Return true if value is infinity.
213
+ #
214
+ # For each input value, emit true if the value is infinite (inf or -inf).
215
+ # @return [Vector]
216
+ # boolean Vector wheather each element is inf.
217
+ #
218
+ define_unary_element_wise :is_inf
219
+
220
+ # return true if value is nil or NaN.
221
+ #
222
+ # For each input value, emit true if the value is nil or NaN.
223
+ # @return [Vector]
224
+ # boolean Vector wheather each element is na.
225
+ #
226
+ def is_na # rubocop:disable Naming/PredicateName
227
+ numeric? ? (is_nil | is_nan) : is_nil
228
+ end
229
+
230
+ # Return true if NaN.
231
+ #
232
+ # For each input value, emit true if the value is NaN.
233
+ # @return [Vector]
234
+ # boolean Vector wheather each element is nan.
235
+ #
236
+ define_unary_element_wise :is_nan
237
+
238
+ # Return true if nil.
239
+ #
240
+ # @note Arrow::NullOptions is not supported yet.
241
+ # For each input value, emit true if the value is nil.
242
+ # @return [Vector]
243
+ # boolean Vector wheather each element is null.
244
+ #
245
+ define_unary_element_wise :is_null
246
+ alias_method :is_nil, :is_null
247
+
248
+ # Return true if non-nil.
249
+ #
250
+ # For each input value, emit true if the value is valid (i.e. non-nil).
251
+ # @return [Vector]
252
+ # boolean Vector wheather each element is valid.
253
+ #
254
+ define_unary_element_wise :is_valid
255
+
256
+ # Compute natural logarithm.
257
+ #
258
+ # Non-positive values return -inf or NaN. Nil values return nil.
259
+ # @return [Vector]
260
+ # natural logarithm of each element of self.
261
+ #
262
+ define_unary_element_wise :ln
263
+
264
+ # Compute base 10 logarithm.
265
+ #
266
+ # Non-positive values return -inf or NaN. Nil values return nil.
267
+ # @return [Vector]
268
+ # base 10 logarithm of each element of self.
269
+ #
270
+ define_unary_element_wise :log10
271
+
272
+ # Compute natural log of (1+x).
273
+ #
274
+ # Non-positive values return -inf or NaN. Nil values return nil.
275
+ # This function may be more precise than log(1 + x) for x close to zero.
276
+ # @return [Vector]
277
+ # natural log of (each element + 1) of self.
278
+ #
279
+ define_unary_element_wise :log1p
280
+
281
+ # Compute base 2 logarithm.
282
+ #
283
+ # Non-positive values return -inf or NaN. Nil values return nil.
284
+ # @return [Vector]
285
+ # base 2 logarithm of each element of self.
286
+ #
287
+ define_unary_element_wise :log2
288
+
289
+ # Round to a given precision.
290
+ #
291
+ # Options are used to control the number of digits and rounding mode.
292
+ # Default behavior is to round to the nearest integer and
293
+ # use half-to-even rule to break ties.
294
+ # @!method round(n_digits: 0, round_mode: :half_to_even)
295
+ # @param n_digits [Integer]
296
+ # Rounding precision (number of digits to round to).
297
+ # @macro round_mode
298
+ # @return [Vector]
299
+ # round of each element of self.
300
+ # @example
301
+ # double = Vector.new([15.15, 2.5, 3.5, -4.5, -5.5])
302
+ # double.round
303
+ # # or double.round(n_digits: 0, mode: :half_to_even)
304
+ #
305
+ # # =>
306
+ # #<RedAmber::Vector(:double, size=5):0x000000000000cd28>
307
+ # [15.0, 2.0, 4.0, -4.0, -6.0]
308
+ #
309
+ # double.round(mode: :towards_infinity)
310
+ #
311
+ # # =>
312
+ # #<RedAmber::Vector(:double, size=5):0x000000000000cd3c>
313
+ # [16.0, 3.0, 4.0, -5.0, -6.0]
314
+ #
315
+ # double.round(mode: :half_up)
316
+ #
317
+ # # =>
318
+ # #<RedAmber::Vector(:double, size=5):0x000000000000cd50>
319
+ # [15.0, 3.0, 4.0, -4.0, -5.0]
320
+ #
321
+ # double.round(mode: :half_towards_zero)
322
+ #
323
+ # # =>
324
+ # #<RedAmber::Vector(:double, size=5):0x000000000000cd64>
325
+ # [15.0, 2.0, 3.0, -4.0, -5.0]
326
+ #
327
+ # double.round(mode: :half_towards_infinity)
328
+ #
329
+ # # =>
330
+ # #<RedAmber::Vector(:double, size=5):0x000000000000cd78>
331
+ # [15.0, 3.0, 4.0, -5.0, -6.0]
332
+ #
333
+ # double.round(mode: :half_to_odd)
334
+ #
335
+ # # =>
336
+ # #<RedAmber::Vector(:double, size=5):0x000000000000cd8c>
337
+ # [15.0, 3.0, 3.0, -5.0, -5.0]
338
+ #
339
+ # double.round(n_digits: 1)
340
+ #
341
+ # # =>
342
+ # #<RedAmber::Vector(:double, size=5):0x000000000000cda0>
343
+ # [15.2, 2.5, 3.5, -4.5, -5.5]
344
+ #
345
+ # double.round(n_digits: -1)
346
+ #
347
+ # # =>
348
+ # #<RedAmber::Vector(:double, size=5):0x000000000000cdb4>
349
+ # [20.0, 0.0, 0.0, -0.0, -10.0]
350
+ #
351
+ define_unary_element_wise :round
352
+
353
+ # Round to a given multiple.
354
+ #
355
+ # Options are used to control the rounding multiple and rounding mode.
356
+ # Default behavior is to round to the nearest integer and
357
+ # use half-to-even rule to break ties.
358
+ # @!method round_to_multiple(multiple: 1.0, round_mode: :half_to_even)
359
+ # @param multiple [Float, Integer]
360
+ # Rounding scale (multiple to round to).
361
+ # Should be a positive numeric scalar of a type compatible with the argument
362
+ # to be rounded. The cast kernel is used to convert the rounding multiple
363
+ # to match the result type.
364
+ # @macro round_mode
365
+ # @return [Vector]
366
+ # round to multiple of each element of self.
367
+ #
368
+ def round_to_multiple(multiple: 1.0, round_mode: :half_to_even)
369
+ datum = exec_func_unary(:round_to_multiple,
370
+ multiple: Arrow::DoubleScalar.new(multiple),
371
+ round_mode: round_mode)
372
+ Vector.create(datum.value)
373
+ end
374
+
375
+ # Get the signedness of the arguments element-wise.
376
+ #
377
+ # Output is any of (-1,1) for nonzero inputs and 0 for zero input.
378
+ # NaN values return NaN. Integral values return signedness as Int8 and
379
+ # floating-point values return it with the same type as the input values.
380
+ # @return [Vector]
381
+ # sign of each element of self.
382
+ #
383
+ define_unary_element_wise :sign
384
+
385
+ # Compute the sine of self element-wise.
386
+ #
387
+ # NaN is returned for invalid input values.
388
+ # @return [Vector]
389
+ # sine of each element of self.
390
+ #
391
+ define_unary_element_wise :sin
392
+
393
+ # Compute the tangent of self element-wise.
394
+ #
395
+ # NaN is returned for invalid input values.
396
+ # @return [Vector]
397
+ # tangent of each element of self.
398
+ #
399
+ define_unary_element_wise :tan
400
+
401
+ # Compute the integral part
402
+ #
403
+ # Compute the nearest integer not greater in magnitude than each element.
404
+ # @return [Vector]
405
+ # trunc of each element of self.
406
+ #
407
+ define_unary_element_wise :trunc
408
+
409
+ # Compute unique elements
410
+ #
411
+ # Return an array with distinct values. Nils in the input are ignored.
412
+ # @return [Vector]
413
+ # uniq element of self.
414
+ #
415
+ define_unary_element_wise :unique
416
+ alias_method :uniq, :unique
417
+
418
+ # Invert boolean values
419
+ #
420
+ # @return [Vector]
421
+ # not of each element of self.
422
+ #
423
+ define_unary_element_wise :invert
424
+ alias_method :'!', :invert # rubocop:disable Lint/SymbolConversion
425
+ alias_method :not, :invert
426
+
427
+ # Negate the argument element-wise
428
+ #
429
+ # Results will wrap around on integer overflow.
430
+ # @return [Vector]
431
+ # negate of each element of self.
432
+ #
433
+ define_unary_element_wise :negate
434
+ alias_method :'-@', :negate # rubocop:disable Lint/SymbolConversion
435
+ end
436
+ end