red_amber 0.3.0 → 0.4.0

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 +39 -20
  3. data/.yardopts +2 -0
  4. data/CHANGELOG.md +113 -0
  5. data/Gemfile +1 -1
  6. data/LICENSE +1 -1
  7. data/README.md +25 -26
  8. data/benchmark/basic.yml +2 -2
  9. data/benchmark/combine.yml +2 -2
  10. data/benchmark/dataframe.yml +2 -2
  11. data/benchmark/group.yml +2 -2
  12. data/benchmark/reshape.yml +2 -2
  13. data/benchmark/vector.yml +3 -0
  14. data/doc/DataFrame.md +32 -12
  15. data/doc/DataFrame_Comparison.md +65 -0
  16. data/doc/SubFrames.md +11 -0
  17. data/doc/Vector.md +207 -1
  18. data/doc/yard-templates/default/fulldoc/html/css/common.css +6 -0
  19. data/lib/red_amber/data_frame.rb +429 -75
  20. data/lib/red_amber/data_frame_combinable.rb +516 -66
  21. data/lib/red_amber/data_frame_displayable.rb +244 -14
  22. data/lib/red_amber/data_frame_indexable.rb +121 -18
  23. data/lib/red_amber/data_frame_loadsave.rb +78 -10
  24. data/lib/red_amber/data_frame_reshaping.rb +184 -14
  25. data/lib/red_amber/data_frame_selectable.rb +622 -66
  26. data/lib/red_amber/data_frame_variable_operation.rb +446 -34
  27. data/lib/red_amber/group.rb +187 -22
  28. data/lib/red_amber/helper.rb +70 -10
  29. data/lib/red_amber/refinements.rb +12 -5
  30. data/lib/red_amber/subframes.rb +1066 -0
  31. data/lib/red_amber/vector.rb +385 -11
  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 +217 -12
  35. data/lib/red_amber/vector_unary_element_wise.rb +436 -0
  36. data/lib/red_amber/vector_updatable.rb +278 -34
  37. data/lib/red_amber/version.rb +2 -1
  38. data/lib/red_amber.rb +13 -1
  39. data/red_amber.gemspec +2 -2
  40. metadata +13 -8
  41. data/doc/image/dataframe/reshaping_DataFrames.png +0 -0
  42. data/lib/red_amber/vector_functions.rb +0 -242
@@ -1,17 +1,94 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RedAmber
4
- # mix-ins for the class DataFrame
4
+ # Mix-in for the class DataFrame
5
5
  module DataFrameReshaping
6
- # Transpose a wide DataFrame.
6
+ # Create a transposed DataFrame for the wide (messy) DataFrame.
7
7
  #
8
- # @param key [Symbol] key of the index column
8
+ # @param key [Symbol]
9
+ # key of the index column
9
10
  # to transepose into keys.
10
11
  # If it is not specified, keys[0] is used.
11
- # @param name [Symbol] key name of transposed index column.
12
+ # @param name [Symbol]
13
+ # key name of transposed index column.
12
14
  # If it is not specified, :NAME is used.
13
15
  # If it already exists, :NAME1 or :NAME1.succ is used.
14
- # @return [DataFrame] trnsposed DataFrame
16
+ # @return [DataFrame]
17
+ # trnsposed DataFrame
18
+ #
19
+ # @example Transpose a DataFrame without options
20
+ #
21
+ # import_cars
22
+ #
23
+ # # =>
24
+ # #<RedAmber::DataFrame : 5 x 6 Vectors, 0x000000000000d520>
25
+ # Year Audi BMW BMW_MINI Mercedes-Benz VW
26
+ # <int64> <int64> <int64> <int64> <int64> <int64>
27
+ # 0 2017 28336 52527 25427 68221 49040
28
+ # 1 2018 26473 50982 25984 67554 51961
29
+ # 2 2019 24222 46814 23813 66553 46794
30
+ # 3 2020 22304 35712 20196 57041 36576
31
+ # 4 2021 22535 35905 18211 51722 35215
32
+ #
33
+ # import_cars.transpose
34
+ #
35
+ # # =>
36
+ # #<RedAmber::DataFrame : 5 x 6 Vectors, 0x0000000000010a2c>
37
+ # NAME 2017 2018 2019 2020 2021
38
+ # <string> <uint32> <uint32> <uint32> <uint16> <uint16>
39
+ # 0 Audi 28336 26473 24222 22304 22535
40
+ # 1 BMW 52527 50982 46814 35712 35905
41
+ # 2 BMW_MINI 25427 25984 23813 20196 18211
42
+ # 3 Mercedes-Benz 68221 67554 66553 57041 51722
43
+ # 4 VW 49040 51961 46794 36576 35215
44
+ #
45
+ # The leftmost column is created by original keys and
46
+ # `:NAME` is automatically used for the column name.
47
+ #
48
+ # @example Transpose a DataFrame with `:name` option
49
+ #
50
+ # import_cars.transpose(name: :Manufacturer)
51
+ #
52
+ # # =>
53
+ # #<RedAmber::DataFrame : 5 x 6 Vectors, 0x0000000000010a2c>
54
+ # Manufacturer 2017 2018 2019 2020 2021
55
+ # <string> <uint32> <uint32> <uint32> <uint16> <uint16>
56
+ # 0 Audi 28336 26473 24222 22304 22535
57
+ # 1 BMW 52527 50982 46814 35712 35905
58
+ # 2 BMW_MINI 25427 25984 23813 20196 18211
59
+ # 3 Mercedes-Benz 68221 67554 66553 57041 51722
60
+ # 4 VW 49040 51961 46794 36576 35215
61
+ #
62
+ # `:name` option can specify column name.
63
+ #
64
+ # @example Transpose a DataFrame by the :key in the middle of the DataFrame
65
+ #
66
+ # import_cars_middle = import_cars.pick(1..2, 0, 3..)
67
+ #
68
+ # # =>
69
+ # #<RedAmber::DataFrame : 5 x 6 Vectors, 0x000000000000f244>
70
+ # Audi BMW Year BMW_MINI Mercedes-Benz VW
71
+ # <int64> <int64> <int64> <int64> <int64> <int64>
72
+ # 0 28336 52527 2017 25427 68221 49040
73
+ # 1 26473 50982 2018 25984 67554 51961
74
+ # 2 24222 46814 2019 23813 66553 46794
75
+ # 3 22304 35712 2020 20196 57041 36576
76
+ # 4 22535 35905 2021 18211 51722 35215
77
+ #
78
+ # import_cars_middle.transpose(key: :Year)
79
+ #
80
+ # # =>
81
+ # #<RedAmber::DataFrame : 5 x 6 Vectors, 0x0000000000010a2c>
82
+ # NAME 2017 2018 2019 2020 2021
83
+ # <string> <uint32> <uint32> <uint32> <uint16> <uint16>
84
+ # 0 Audi 28336 26473 24222 22304 22535
85
+ # 1 BMW 52527 50982 46814 35712 35905
86
+ # 2 BMW_MINI 25427 25984 23813 20196 18211
87
+ # 3 Mercedes-Benz 68221 67554 66553 57041 51722
88
+ # 4 VW 49040 51961 46794 36576 35215
89
+ #
90
+ # @since 0.2.0
91
+ #
15
92
  def transpose(key: keys.first, name: :NAME)
16
93
  unless keys.include?(key)
17
94
  raise DataFrameArgumentError, "Self does not include: #{key}"
@@ -31,12 +108,67 @@ module RedAmber
31
108
  DataFrame.new(hash)
32
109
  end
33
110
 
34
- # Reshape wide DataFrame to a longer DataFrame.
111
+ # Create a 'long' (may be tidy) DataFrame from a 'wide' DataFrame.
112
+ #
113
+ # @param keep_keys [<Symbol>]
114
+ # keys to keep.
115
+ # @param name [Symbol, String]
116
+ # a new key name of the column which is come from key names.
117
+ # @param value [Symbol, String]
118
+ # a new key name of the column which is come from values.
119
+ # @return [DataFrame]
120
+ # long DataFrame.
121
+ #
122
+ # @example `to_long` without options
123
+ #
124
+ # import_cars
125
+ #
126
+ # # =>
127
+ # #<RedAmber::DataFrame : 5 x 6 Vectors, 0x000000000000d520>
128
+ # Year Audi BMW BMW_MINI Mercedes-Benz VW
129
+ # <int64> <int64> <int64> <int64> <int64> <int64>
130
+ # 0 2017 28336 52527 25427 68221 49040
131
+ # 1 2018 26473 50982 25984 67554 51961
132
+ # 2 2019 24222 46814 23813 66553 46794
133
+ # 3 2020 22304 35712 20196 57041 36576
134
+ # 4 2021 22535 35905 18211 51722 35215
135
+ #
136
+ # import_cars.to_long(:Year)
137
+ #
138
+ # # =>
139
+ # #<RedAmber::DataFrame : 25 x 3 Vectors, 0x0000000000011864>
140
+ # Year NAME VALUE
141
+ # <uint16> <string> <uint32>
142
+ # 0 2017 Audi 28336
143
+ # 1 2017 BMW 52527
144
+ # 2 2017 BMW_MINI 25427
145
+ # 3 2017 Mercedes-Benz 68221
146
+ # 4 2017 VW 49040
147
+ # : : : :
148
+ # 22 2021 BMW_MINI 18211
149
+ # 23 2021 Mercedes-Benz 51722
150
+ # 24 2021 VW 35215
151
+ #
152
+ # @example `to_long` with options `:name` and `:value`
153
+ #
154
+ # import_cars.to_long(:Year, name: :Manufacturer, value: :Num_of_imported)
155
+ #
156
+ # # =>
157
+ # #<RedAmber::DataFrame : 25 x 3 Vectors, 0x000000000001359c>
158
+ # Year Manufacturer Num_of_imported
159
+ # <uint16> <string> <uint32>
160
+ # 0 2017 Audi 28336
161
+ # 1 2017 BMW 52527
162
+ # 2 2017 BMW_MINI 25427
163
+ # 3 2017 Mercedes-Benz 68221
164
+ # 4 2017 VW 49040
165
+ # : : : :
166
+ # 22 2021 BMW_MINI 18211
167
+ # 23 2021 Mercedes-Benz 51722
168
+ # 24 2021 VW 35215
169
+ #
170
+ # @since 0.2.0
35
171
  #
36
- # @param keep_keys [Array] keys to keep.
37
- # @param name [Symbol, String] key of the column which is come **from values**.
38
- # @param value [Symbol, String] key of the column which is come **from values**.
39
- # @return [DataFrame] long DataFrame.
40
172
  def to_long(*keep_keys, name: :NAME, value: :VALUE)
41
173
  warn('[Info] No key to keep is specified.') if keep_keys.empty?
42
174
 
@@ -73,13 +205,51 @@ module RedAmber
73
205
  DataFrame.new(hash)
74
206
  end
75
207
 
76
- # Reshape long DataFrame to a wide DataFrame.
208
+ # Create a 'wide' (may be messy) DataFrame from a 'long' DataFrame.
77
209
  #
78
210
  # @param name [Symbol, String]
79
- # key of the column which will be expanded **to key names**.
211
+ # a new key name of the columnwhich will be expanded to key names.
80
212
  # @param value [Symbol, String]
81
- # key of the column which will be expanded **to values**.
82
- # @return [DataFrame] wide DataFrame.
213
+ # a new key name of the column which will be expanded to values.
214
+ # @return [DataFrame]
215
+ # wide DataFrame.
216
+ #
217
+ # @example `to_wide` without options
218
+ #
219
+ # import_cars_long = import_cars.to_long(:Year)
220
+ #
221
+ # # =>
222
+ # #<RedAmber::DataFrame : 25 x 3 Vectors, 0x0000000000011864>
223
+ # Year NAME VALUE
224
+ # <uint16> <string> <uint32>
225
+ # 0 2017 Audi 28336
226
+ # 1 2017 BMW 52527
227
+ # 2 2017 BMW_MINI 25427
228
+ # 3 2017 Mercedes-Benz 68221
229
+ # 4 2017 VW 49040
230
+ # : : : :
231
+ # 22 2021 BMW_MINI 18211
232
+ # 23 2021 Mercedes-Benz 51722
233
+ # 24 2021 VW 35215
234
+ #
235
+ # import_cars_long.to_wide
236
+ # # or same as `import_cars_long.to_wide(name: :NAME, value: VALUE)`
237
+ #
238
+ # # =>
239
+ # #<RedAmber::DataFrame : 5 x 6 Vectors, 0x000000000000d520>
240
+ # Year Audi BMW BMW_MINI Mercedes-Benz VW
241
+ # <int64> <int64> <int64> <int64> <int64> <int64>
242
+ # 0 2017 28336 52527 25427 68221 49040
243
+ # 1 2018 26473 50982 25984 67554 51961
244
+ # 2 2019 24222 46814 23813 66553 46794
245
+ # 3 2020 22304 35712 20196 57041 36576
246
+ # 4 2021 22535 35905 18211 51722 35215
247
+ #
248
+ # Columns other than `NAME` and `VALUE` (it is `Year` for this case) will be
249
+ # automatically processed and do not need to specify.
250
+ #
251
+ # @since 0.2.0
252
+ #
83
253
  def to_wide(name: :NAME, value: :VALUE)
84
254
  name = name.to_sym
85
255
  unless keys.include?(name)