daru_lite 0.3.4 → 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 (161) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +84 -0
  3. data/README.md +16 -1
  4. data/lib/daru_lite/category.rb +1 -1
  5. data/lib/daru_lite/core/group_by.rb +0 -4
  6. data/lib/daru_lite/core/merge.rb +2 -2
  7. data/lib/daru_lite/data_frame/calculatable.rb +12 -6
  8. data/lib/daru_lite/data_frame/duplicatable.rb +0 -14
  9. data/lib/daru_lite/data_frame/iterable.rb +2 -2
  10. data/lib/daru_lite/data_frame/missable.rb +0 -9
  11. data/lib/daru_lite/dataframe.rb +34 -9
  12. data/lib/daru_lite/date_time/index.rb +4 -4
  13. data/lib/daru_lite/index/multi_index.rb +1 -2
  14. data/lib/daru_lite/maths/statistics/dataframe.rb +2 -4
  15. data/lib/daru_lite/maths/statistics/vector.rb +3 -8
  16. data/lib/daru_lite/monkeys/array.rb +4 -0
  17. data/lib/daru_lite/monkeys/hash.rb +3 -0
  18. data/lib/daru_lite/monkeys/matrix.rb +3 -0
  19. data/lib/daru_lite/monkeys/range.rb +4 -0
  20. data/lib/daru_lite/vector/missable.rb +0 -55
  21. data/lib/daru_lite/vector/sortable.rb +2 -2
  22. data/lib/daru_lite/vector.rb +3 -9
  23. data/lib/daru_lite/version.rb +1 -1
  24. data/lib/daru_lite.rb +8 -10
  25. metadata +54 -287
  26. data/.github/ISSUE_TEMPLATE/bug_report.md +0 -38
  27. data/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
  28. data/.github/dependabot.yml +0 -11
  29. data/.github/workflows/ci.yml +0 -33
  30. data/.gitignore +0 -10
  31. data/.rspec +0 -2
  32. data/.rubocop.yml +0 -27
  33. data/.rubocop_todo.yml +0 -139
  34. data/CONTRIBUTING.md +0 -47
  35. data/Gemfile +0 -2
  36. data/Rakefile +0 -69
  37. data/ReleasePolicy.md +0 -20
  38. data/benchmarks/TradeoffData.csv +0 -65
  39. data/benchmarks/csv_reading.rb +0 -22
  40. data/benchmarks/dataframe_creation.rb +0 -39
  41. data/benchmarks/db_loading.rb +0 -33
  42. data/benchmarks/duplicating.rb +0 -45
  43. data/benchmarks/group_by.rb +0 -32
  44. data/benchmarks/joining.rb +0 -52
  45. data/benchmarks/row_access.rb +0 -41
  46. data/benchmarks/row_assign.rb +0 -36
  47. data/benchmarks/sorting.rb +0 -51
  48. data/benchmarks/statistics.rb +0 -28
  49. data/benchmarks/vector_access.rb +0 -31
  50. data/benchmarks/vector_assign.rb +0 -42
  51. data/benchmarks/where_clause.rb +0 -48
  52. data/benchmarks/where_vs_filter.rb +0 -28
  53. data/daru_lite.gemspec +0 -54
  54. data/daru_lite_test-shm +0 -0
  55. data/daru_lite_test-wal +0 -0
  56. data/images/README.md +0 -5
  57. data/images/con0.png +0 -0
  58. data/images/con1.png +0 -0
  59. data/images/init0.png +0 -0
  60. data/images/init1.png +0 -0
  61. data/images/man0.png +0 -0
  62. data/images/man1.png +0 -0
  63. data/images/man2.png +0 -0
  64. data/images/man3.png +0 -0
  65. data/images/man4.png +0 -0
  66. data/images/man5.png +0 -0
  67. data/images/man6.png +0 -0
  68. data/lib/daru_lite/accessors/mdarray_wrapper.rb +0 -7
  69. data/lib/daru_lite/monkeys/m_d_array.rb +0 -7
  70. data/lib/daru_lite/monkeys/object.rb +0 -7
  71. data/profile/_base.rb +0 -23
  72. data/profile/df_to_a.rb +0 -10
  73. data/profile/filter.rb +0 -13
  74. data/profile/joining.rb +0 -13
  75. data/profile/sorting.rb +0 -12
  76. data/profile/vector_each_with_index.rb +0 -9
  77. data/profile/vector_new.rb +0 -9
  78. data/spec/accessors/array_wrapper_spec.rb +0 -3
  79. data/spec/category_spec.rb +0 -1767
  80. data/spec/core/group_by_spec.rb +0 -655
  81. data/spec/core/merge_spec.rb +0 -179
  82. data/spec/core/query_spec.rb +0 -347
  83. data/spec/daru_lite_spec.rb +0 -22
  84. data/spec/data_frame/aggregatable_example.rb +0 -65
  85. data/spec/data_frame/buildable_example.rb +0 -109
  86. data/spec/data_frame/calculatable_example.rb +0 -135
  87. data/spec/data_frame/convertible_example.rb +0 -180
  88. data/spec/data_frame/duplicatable_example.rb +0 -134
  89. data/spec/data_frame/fetchable_example.rb +0 -510
  90. data/spec/data_frame/filterable_example.rb +0 -456
  91. data/spec/data_frame/indexable_example.rb +0 -221
  92. data/spec/data_frame/iterable_example.rb +0 -481
  93. data/spec/data_frame/joinable_example.rb +0 -131
  94. data/spec/data_frame/missable_example.rb +0 -47
  95. data/spec/data_frame/pivotable_example.rb +0 -297
  96. data/spec/data_frame/queryable_example.rb +0 -92
  97. data/spec/data_frame/setable_example.rb +0 -513
  98. data/spec/data_frame/sortable_example.rb +0 -368
  99. data/spec/dataframe_spec.rb +0 -1342
  100. data/spec/date_time/data_spec.rb +0 -197
  101. data/spec/date_time/date_time_index_helper_spec.rb +0 -72
  102. data/spec/date_time/index_spec.rb +0 -588
  103. data/spec/date_time/offsets_spec.rb +0 -465
  104. data/spec/extensions/which_dsl_spec.rb +0 -38
  105. data/spec/fixtures/bank2.dat +0 -200
  106. data/spec/fixtures/boolean_converter_test.csv +0 -5
  107. data/spec/fixtures/countries.json +0 -7794
  108. data/spec/fixtures/duplicates.csv +0 -32
  109. data/spec/fixtures/eciresults.html +0 -394
  110. data/spec/fixtures/empties.dat +0 -2
  111. data/spec/fixtures/empty_rows_test.csv +0 -17
  112. data/spec/fixtures/macau.html +0 -3691
  113. data/spec/fixtures/macd_data.csv +0 -150
  114. data/spec/fixtures/matrix_test.csv +0 -100
  115. data/spec/fixtures/moneycontrol.html +0 -6812
  116. data/spec/fixtures/music_data.tsv +0 -2501
  117. data/spec/fixtures/repeated_fields.csv +0 -7
  118. data/spec/fixtures/sales-funnel.csv +0 -18
  119. data/spec/fixtures/scientific_notation.csv +0 -4
  120. data/spec/fixtures/string_converter_test.csv +0 -5
  121. data/spec/fixtures/strings.dat +0 -2
  122. data/spec/fixtures/test_xls.xls +0 -0
  123. data/spec/fixtures/test_xls_2.xls +0 -0
  124. data/spec/fixtures/url_test.txt~ +0 -0
  125. data/spec/fixtures/valid_markup.html +0 -62
  126. data/spec/fixtures/wiki_climate.html +0 -1243
  127. data/spec/fixtures/wiki_table_info.html +0 -631
  128. data/spec/formatters/table_formatter_spec.rb +0 -137
  129. data/spec/helpers_spec.rb +0 -8
  130. data/spec/index/categorical_index_spec.rb +0 -289
  131. data/spec/index/index_spec.rb +0 -438
  132. data/spec/index/multi_index_spec.rb +0 -752
  133. data/spec/io/io_spec.rb +0 -357
  134. data/spec/io/sql_data_source_spec.rb +0 -48
  135. data/spec/iruby/dataframe_spec.rb +0 -170
  136. data/spec/iruby/helpers_spec.rb +0 -49
  137. data/spec/iruby/multi_index_spec.rb +0 -37
  138. data/spec/iruby/vector_spec.rb +0 -105
  139. data/spec/maths/arithmetic/dataframe_spec.rb +0 -148
  140. data/spec/maths/arithmetic/vector_spec.rb +0 -165
  141. data/spec/maths/statistics/dataframe_spec.rb +0 -191
  142. data/spec/maths/statistics/vector_spec.rb +0 -756
  143. data/spec/monkeys_spec.rb +0 -42
  144. data/spec/shared/vector_display_spec.rb +0 -213
  145. data/spec/spec_helper.rb +0 -87
  146. data/spec/support/database_helper.rb +0 -29
  147. data/spec/support/matchers.rb +0 -5
  148. data/spec/vector/aggregatable_example.rb +0 -27
  149. data/spec/vector/calculatable_example.rb +0 -82
  150. data/spec/vector/convertible_example.rb +0 -126
  151. data/spec/vector/duplicatable_example.rb +0 -48
  152. data/spec/vector/fetchable_example.rb +0 -463
  153. data/spec/vector/filterable_example.rb +0 -165
  154. data/spec/vector/indexable_example.rb +0 -201
  155. data/spec/vector/iterable_example.rb +0 -111
  156. data/spec/vector/joinable_example.rb +0 -25
  157. data/spec/vector/missable_example.rb +0 -88
  158. data/spec/vector/queryable_example.rb +0 -91
  159. data/spec/vector/setable_example.rb +0 -300
  160. data/spec/vector/sortable_example.rb +0 -242
  161. data/spec/vector_spec.rb +0 -599
@@ -1,752 +0,0 @@
1
- require 'spec_helper.rb'
2
-
3
- describe DaruLite::MultiIndex do
4
- let(:index) { described_class.from_tuples(index_tuples) }
5
- let(:index_tuples) do
6
- [
7
- [:a, :one, :bar],
8
- [:a, :one, :baz],
9
- [:a, :two, :bar],
10
- [:a, :two, :baz],
11
- [:b, :one, :bar],
12
- [:b, :two, :bar],
13
- [:b, :two, :baz],
14
- [:b, :one, :foo],
15
- [:c, :one, :bar],
16
- [:c, :one, :baz],
17
- [:c, :two, :foo],
18
- [:c, :two, :bar]
19
- ]
20
- end
21
-
22
- context ".initialize" do
23
- it "accepts labels and levels as arguments" do
24
- mi = DaruLite::MultiIndex.new(
25
- levels: [[:a,:b,:c], [:one, :two]],
26
- labels: [[0,0,1,1,2,2], [0,1,0,1,0,1]])
27
-
28
- expect(mi[:a, :two]).to eq(1)
29
- end
30
-
31
- it "raises error for wrong number of labels or levels" do
32
- expect {
33
- DaruLite::MultiIndex.new(
34
- levels: [[:a,:a,:b,:b,:c,:c], [:one, :two]],
35
- labels: [[0,0,1,1,2,2]])
36
- }.to raise_error
37
- end
38
-
39
- context "create an MultiIndex with name" do
40
- context 'if no name is set' do
41
- subject { DaruLite::MultiIndex.new(
42
- levels: [[:a,:b,:c], [:one, :two]],
43
- labels: [[0,0,1,1,2,2], [0,1,0,1,0,1]]) }
44
- its(:name) { is_expected.to be_nil }
45
- end
46
-
47
- context 'correctly return the MultiIndex name' do
48
- subject { DaruLite::MultiIndex.new(
49
- levels: [[:a,:b,:c], [:one, :two]],
50
- labels: [[0,0,1,1,2,2], [0,1,0,1,0,1]], name: ['n1', 'n2']) }
51
- its(:name) { is_expected.to eq ['n1', 'n2'] }
52
- end
53
-
54
- context "set new MultiIndex name" do
55
- subject {
56
- DaruLite::MultiIndex.new(
57
- levels: [[:a,:b,:c], [:one, :two]],
58
- labels: [[0,0,1,1,2,2], [0,1,0,1,0,1]], name: ['n1', 'n2']) }
59
- before(:each) { subject.name = ['k1', 'k2'] }
60
- its(:name) { is_expected.to eq ['k1', 'k2'] }
61
- end
62
-
63
- context "set new MultiIndex name having empty string" do
64
- subject {
65
- DaruLite::MultiIndex.new(
66
- levels: [[:a,:b,:c], [:one, :two]],
67
- labels: [[0,0,1,1,2,2], [0,1,0,1,0,1]], name: ['n1', 'n2']) }
68
- before { subject.name = ['k1', ''] }
69
- its(:name) { is_expected.to eq ['k1', ''] }
70
- end
71
-
72
- it "raises SizeError for wrong number of name" do
73
- error_msg = "\'names\' and \'levels\' should be of same size. Size of the \'name\' array is 2 and size of the MultiIndex \'levels\' and \'labels\' is 3.\nIf you do not want to set name for particular level (say level \'i\') then put empty string on index \'i\' of the \'name\' Array."
74
- expect { index.name = ['n1', 'n2'] }.to raise_error(SizeError, error_msg)
75
-
76
- error_msg = "'names' and 'levels' should be of same size. Size of the 'name' array is 0 and size of the MultiIndex 'levels' and 'labels' is 3.\nIf you do not want to set name for particular level (say level 'i') then put empty string on index 'i' of the 'name' Array."
77
- expect { index.name = [ ] }.to raise_error(SizeError, error_msg)
78
-
79
- error_msg = "'names' and 'levels' should be of same size. Size of the 'name' array is 1 and size of the MultiIndex 'levels' and 'labels' is 3.\nIf you do not want to set name for particular level (say level 'i') then put empty string on index 'i' of the 'name' Array."
80
- expect { index.name = [''] }.to raise_error(SizeError, error_msg)
81
-
82
- error_msg = "'names' and 'levels' should be of same size. Size of the 'name' array is 4 and size of the MultiIndex 'levels' and 'labels' is 3."
83
- expect { index.name = ['n1', 'n2', 'n3', 'n4'] }.to raise_error(SizeError, error_msg)
84
- end
85
- end
86
- end
87
-
88
- context ".from_tuples" do
89
- it "creates 2 layer MultiIndex from tuples" do
90
- tuples = [
91
- [:a, :one],
92
- [:a, :two],
93
- [:b, :one],
94
- [:b, :two],
95
- [:c, :one],
96
- [:c, :two]
97
- ]
98
- mi = DaruLite::MultiIndex.from_tuples(tuples)
99
- expect(mi.levels).to eq([[:a, :b, :c], [:one,:two]])
100
- expect(mi.labels).to eq([[0,0,1,1,2,2], [0,1,0,1,0,1]])
101
- end
102
-
103
- it "creates a triple layer MultiIndex from tuples" do
104
- expect(index.levels).to eq([[:a,:b,:c], [:one, :two],[:bar,:baz,:foo]])
105
- expect(index.labels).to eq([
106
- [0,0,0,0,1,1,1,1,2,2,2,2],
107
- [0,0,1,1,0,1,1,0,0,0,1,1],
108
- [0,1,0,1,0,0,1,2,0,1,2,0]
109
- ])
110
- end
111
- end
112
-
113
- context '.try_from_tuples' do
114
- it 'creates MultiIndex, if there are tuples' do
115
- tuples = [
116
- [:a, :one],
117
- [:a, :two],
118
- [:b, :one],
119
- [:b, :two],
120
- [:c, :one],
121
- [:c, :two]
122
- ]
123
- mi = DaruLite::MultiIndex.try_from_tuples(tuples)
124
- expect(mi).to be_a DaruLite::MultiIndex
125
- end
126
-
127
- it 'returns nil, if MultiIndex can not be created' do
128
- mi = DaruLite::MultiIndex.try_from_tuples([:a, :b, :c])
129
- expect(mi).to be_nil
130
- end
131
- end
132
-
133
- context "#size" do
134
- it "returns size of MultiIndex" do
135
- expect(index.size).to eq(12)
136
- end
137
- end
138
-
139
- context "#[]" do
140
- it "returns the row number when specifying the complete tuple" do
141
- expect(index[:a, :one, :baz]).to eq(1)
142
- end
143
-
144
- it "returns the row number when specifying last tuple part as an array" do
145
- expect(index[:a, :one, [:baz]]).to eq(1)
146
- end
147
-
148
- it "returns the row numbers when specifying multiple tuples" do
149
- expect(index[[:a, :one, :baz], [:b, :two, :bar]]).to eq([1, 5])
150
- end
151
-
152
- it "returns MultiIndex when specifying incomplete tuple" do
153
- expect(index[:b]).to eq(DaruLite::MultiIndex.from_tuples([
154
- [:b,:one,:bar],
155
- [:b,:two,:bar],
156
- [:b,:two,:baz],
157
- [:b,:one,:foo]
158
- ]))
159
- expect(index[:b, :one]).to eq(DaruLite::MultiIndex.from_tuples([
160
- [:b,:one,:bar],
161
- [:b,:one,:foo]
162
- ]))
163
- # TODO: Return DaruLite::Index if a single layer of indexes is present.
164
- end
165
-
166
- it "returns MultiIndex when specifying wholly numeric ranges" do
167
- expect(index[3..6]).to eq(DaruLite::MultiIndex.from_tuples([
168
- [:a,:two,:baz],
169
- [:b,:one,:bar],
170
- [:b,:two,:bar],
171
- [:b,:two,:baz]
172
- ]))
173
- end
174
-
175
- it "raises error when specifying invalid index" do
176
- expect { index[:a, :three] }.to raise_error IndexError
177
- expect { index[:a, :one, :xyz] }.to raise_error IndexError
178
- expect { index[:x] }.to raise_error IndexError
179
- expect { index[:x, :one] }.to raise_error IndexError
180
- expect { index[:x, :one, :bar] }.to raise_error IndexError
181
- end
182
-
183
- it "works with numerical first levels" do
184
- mi = DaruLite::MultiIndex.from_tuples([
185
- [2000, 'M'],
186
- [2000, 'F'],
187
- [2001, 'M'],
188
- [2001, 'F']
189
- ])
190
-
191
- expect(mi[2000]).to eq(DaruLite::MultiIndex.from_tuples([
192
- [2000, 'M'],
193
- [2000, 'F']
194
- ]))
195
-
196
- expect(mi[2000,'M']).to eq(0)
197
- end
198
- end
199
-
200
- describe "#to_a" do
201
- subject { index.to_a }
202
-
203
- it { is_expected.to eq(index_tuples) }
204
-
205
- it 'the returns array is not a variable of the index' do
206
- expect { subject << [:d, :one, :bar] }.not_to change { index.to_a }
207
- end
208
- end
209
-
210
- context "#include?" do
211
- it "checks if a completely specified tuple exists" do
212
- expect(index.include?([:a,:one,:bar])).to eq(true)
213
- end
214
-
215
- it "checks if a top layer incomplete tuple exists" do
216
- expect(index.include?([:a])).to eq(true)
217
- end
218
-
219
- it "checks if a middle layer incomplete tuple exists" do
220
- expect(index.include?([:a, :one])).to eq(true)
221
- end
222
-
223
- it "checks for non-existence of completely specified tuple" do
224
- expect(index.include?([:b, :two, :foo])).to eq(false)
225
- end
226
-
227
- it "checks for non-existence of a top layer incomplete tuple" do
228
- expect(index.include?([:d])).to eq(false)
229
- end
230
-
231
- it "checks for non-existence of a middle layer incomplete tuple" do
232
- expect(index.include?([:c, :three])).to eq(false)
233
- end
234
- end
235
-
236
- context "#key" do
237
- it "returns the tuple of the specified number" do
238
- expect(index.key(3)).to eq([:a,:two,:baz])
239
- end
240
-
241
- it "returns nil for non-existent pointer number" do
242
- expect {
243
- index.key(100)
244
- }.to raise_error ArgumentError
245
- end
246
- end
247
-
248
- context "#to_a" do
249
- it "returns tuples as an Array" do
250
- expect(index.to_a).to eq(index_tuples)
251
- end
252
- end
253
-
254
- context "#dup" do
255
- it "completely duplicates the object" do
256
- duplicate = index.dup
257
- expect(duplicate) .to eq(index)
258
- expect(duplicate.object_id).to_not eq(index.object_id)
259
- end
260
- end
261
-
262
- context "#inspect" do
263
- context 'small index' do
264
- subject { index.inspect }
265
-
266
- it do
267
- is_expected.to eq %Q{
268
- |#<DaruLite::MultiIndex(12x3)>
269
- | a one bar
270
- | baz
271
- | two bar
272
- | baz
273
- | b one bar
274
- | two bar
275
- | baz
276
- | one foo
277
- | c one bar
278
- | baz
279
- | two foo
280
- | bar
281
- }.unindent
282
- end
283
- end
284
-
285
- context 'large index' do
286
- subject {
287
- DaruLite::MultiIndex.from_tuples(
288
- (1..100).map { |i| %w[a b c].map { |c| [i, c] } }.flatten(1)
289
- )
290
- }
291
-
292
- its(:inspect) { is_expected.to eq %Q{
293
- |#<DaruLite::MultiIndex(300x2)>
294
- | 1 a
295
- | b
296
- | c
297
- | 2 a
298
- | b
299
- | c
300
- | 3 a
301
- | b
302
- | c
303
- | 4 a
304
- | b
305
- | c
306
- | 5 a
307
- | b
308
- | c
309
- | 6 a
310
- | b
311
- | c
312
- | 7 a
313
- | b
314
- | ... ...
315
- }.unindent
316
- }
317
- end
318
-
319
- context 'multi index with name' do
320
- subject {
321
- DaruLite::MultiIndex.new(
322
- levels: [[:a,:b,:c],[:one,:two],[:bar, :baz, :foo]],
323
- labels: [
324
- [0,0,0,0,1,1,1,1,2,2,2,2],
325
- [0,0,1,1,0,1,1,0,0,0,1,1],
326
- [0,1,0,1,0,0,1,2,0,1,2,0]], name: ['n1', 'n2', 'n3'])
327
- }
328
-
329
- its(:inspect) { is_expected.to start_with %Q{
330
- |#<DaruLite::MultiIndex(12x3)>
331
- | n1 n2 n3
332
- }.unindent
333
- }
334
- end
335
-
336
- context 'multi index with name having empty string' do
337
- subject {
338
- DaruLite::MultiIndex.new(
339
- levels: [[:a,:b,:c],[:one,:two],[:bar, :baz, :foo]],
340
- labels: [
341
- [0,0,0,0,1,1,1,1,2,2,2,2],
342
- [0,0,1,1,0,1,1,0,0,0,1,1],
343
- [0,1,0,1,0,0,1,2,0,1,2,0]], name: ['n1', 'n2', 'n3'])
344
- }
345
- before { subject.name = ['n1', '', 'n3'] }
346
-
347
- its(:inspect) { is_expected.to start_with %Q{
348
- |#<DaruLite::MultiIndex(12x3)>
349
- | n1 n3
350
- }.unindent
351
- }
352
- end
353
-
354
- end
355
-
356
- context "#==" do
357
- it "returns false for unequal MultiIndex comparisons" do
358
- mi1 = DaruLite::MultiIndex.from_tuples([
359
- [:a, :one, :bar],
360
- [:a, :two, :baz],
361
- [:b, :one, :foo],
362
- [:b, :two, :bar]
363
- ])
364
- mi2 = DaruLite::MultiIndex.from_tuples([
365
- [:a, :two, :bar],
366
- [:b, :one, :foo],
367
- [:a, :one, :baz],
368
- [:b, :two, :baz]
369
- ])
370
-
371
- expect(mi1 == mi2).to eq(false)
372
- end
373
- end
374
-
375
- context "#values" do
376
- it "returns an array of indices in order" do
377
- mi = DaruLite::MultiIndex.from_tuples([
378
- [:a, :one, :bar],
379
- [:a, :two, :baz],
380
- [:b, :one, :foo],
381
- [:b, :two, :bar]
382
- ])
383
-
384
- expect(mi.values).to eq([0,1,2,3])
385
- end
386
- end
387
-
388
- context "#|" do
389
- before do
390
- @mi1 = DaruLite::MultiIndex.from_tuples([
391
- [:a, :one, :bar],
392
- [:a, :two, :baz],
393
- [:b, :one, :foo],
394
- [:b, :two, :bar]
395
- ])
396
- @mi2 = DaruLite::MultiIndex.from_tuples([
397
- [:a, :two, :bar],
398
- [:b, :one, :foo],
399
- [:a, :one, :baz],
400
- [:b, :two, :baz]
401
- ])
402
- end
403
-
404
- it "returns a union of two MultiIndex objects" do
405
- expect(@mi1 | @mi2).to eq(DaruLite::MultiIndex.new(
406
- levels: [[:a, :b], [:one, :two], [:bar, :baz, :foo]],
407
- labels: [
408
- [0, 0, 1, 1, 0, 0, 1],
409
- [0, 1, 0, 1, 1, 0, 1],
410
- [0, 1, 2, 0, 0, 1, 1]
411
- ])
412
- )
413
- end
414
- end
415
-
416
- context "#&" do
417
- before do
418
- @mi1 = DaruLite::MultiIndex.from_tuples([
419
- [:a, :one],
420
- [:a, :two],
421
- [:b, :two]
422
- ])
423
- @mi2 = DaruLite::MultiIndex.from_tuples([
424
- [:a, :two],
425
- [:b, :one],
426
- [:b, :three]
427
- ])
428
- end
429
-
430
- it "returns the intersection of two MI objects" do
431
- expect(@mi1 & @mi2).to eq(DaruLite::MultiIndex.from_tuples([
432
- [:a, :two],
433
- ]))
434
- end
435
- end
436
-
437
- context "#empty?" do
438
- it "returns true if nothing present in MultiIndex" do
439
- expect(DaruLite::MultiIndex.new(labels: [[]], levels: [[]]).empty?).to eq(true)
440
- end
441
- end
442
-
443
- context "#drop_left_level" do
444
- it "drops the leftmost level" do
445
- expect(
446
- DaruLite::MultiIndex.from_tuples([
447
- [:c,:one,:bar],
448
- [:c,:one,:baz],
449
- [:c,:two,:foo],
450
- [:c,:two,:bar]
451
- ]).drop_left_level).to eq(
452
- DaruLite::MultiIndex.from_tuples([
453
- [:one,:bar],
454
- [:one,:baz],
455
- [:two,:foo],
456
- [:two,:bar]
457
- ])
458
- )
459
- end
460
- end
461
-
462
- context 'other forms of tuple list representation' do
463
- let(:index) {
464
- DaruLite::MultiIndex.from_tuples [
465
- [:a,:one,:bar],
466
- [:a,:one,:baz],
467
- [:a,:two,:bar],
468
- [:a,:two,:baz],
469
- [:b,:one,:bar],
470
- [:b,:two,:bar],
471
- [:b,:two,:baz],
472
- [:b,:one,:foo],
473
- [:c,:one,:bar],
474
- [:c,:one,:baz],
475
- [:c,:two,:foo],
476
- [:c,:two,:bar]
477
- ]
478
- }
479
-
480
- context '#sparse_tuples' do
481
- subject { index.sparse_tuples }
482
-
483
- it { is_expected.to eq [
484
- [:a ,:one,:bar],
485
- [nil, nil,:baz],
486
- [nil,:two,:bar],
487
- [nil, nil,:baz],
488
- [:b ,:one,:bar],
489
- [nil,:two,:bar],
490
- [nil, nil,:baz],
491
- [nil,:one,:foo],
492
- [:c ,:one,:bar],
493
- [nil, nil,:baz],
494
- [nil,:two,:foo],
495
- [nil, nil,:bar]
496
- ]}
497
- end
498
- end
499
-
500
- context "#pos" do
501
- let(:idx) do
502
- described_class.from_tuples([
503
- [:b,:one,:bar],
504
- [:b,:two,:bar],
505
- [:b,:two,:baz],
506
- [:b,:one,:foo]
507
- ])
508
- end
509
-
510
- context "single index" do
511
- it { expect(idx.pos :b, :one, :bar).to eq 0 }
512
- end
513
-
514
- context "multiple indexes" do
515
- subject { idx.pos :b, :one }
516
-
517
- it { is_expected.to be_a Array }
518
- its(:size) { is_expected.to eq 2 }
519
- it { is_expected.to eq [0, 3] }
520
- end
521
-
522
- context "single positional index" do
523
- it { expect(idx.pos 0).to eq 0 }
524
- end
525
-
526
- context "multiple positional indexes" do
527
- subject { idx.pos 0, 1 }
528
-
529
- it { is_expected.to be_a Array }
530
- its(:size) { is_expected.to eq 2 }
531
- it { is_expected.to eq [0, 1] }
532
- end
533
-
534
- context "multiple tuple indexes" do
535
- subject { idx.pos [:b,:two,:bar], [:b,:one,:foo] }
536
-
537
- it { is_expected.to be_a Array }
538
- its(:size) { is_expected.to eq 2 }
539
- it { is_expected.to eq [1, 3] }
540
- end
541
-
542
- # TODO: Add specs for IndexError
543
- end
544
-
545
- context "#positions_for" do
546
- let(:idx) do
547
- described_class.from_tuples([
548
- [:b, :one, :bar],
549
- [:b, :two, :bar],
550
- [:b, :two, :baz],
551
- [:b, :one, :foo]
552
- ])
553
- end
554
-
555
- it "returns all matching positions for a partial key" do
556
- expect(idx.positions_for([:b, :one])).to eq([0, 3])
557
- end
558
-
559
- it "returns a single-element array for a full unique tuple" do
560
- expect(idx.positions_for([:b, :one, :bar])).to eq([0])
561
- end
562
-
563
- it "returns every position for duplicate tuples" do
564
- dup_idx = described_class.from_tuples([
565
- [:weighted_count, 'Wave'],
566
- [:weighted_count, 'Wave'],
567
- [:percentage, 'Wave'],
568
- [:percentage, 'Wave']
569
- ])
570
-
571
- expect(dup_idx.positions_for([:weighted_count, 'Wave'])).to eq([0, 1])
572
- expect(dup_idx.positions_for([:weighted_count])).to eq([0, 1])
573
- end
574
-
575
- it "raises IndexError for a non-existent key" do
576
- expect { idx.positions_for([:z]) }.to raise_error(IndexError)
577
- end
578
-
579
- it "raises IndexError (not NoMethodError) for a key longer than the levels" do
580
- expect { idx.positions_for([:b, :one, :bar, :extra]) }.to raise_error(IndexError)
581
- end
582
- end
583
-
584
- context "#subset" do
585
- let(:idx) do
586
- described_class.from_tuples([
587
- [:b, :one, :bar],
588
- [:b, :two, :bar],
589
- [:b, :two, :baz],
590
- [:b, :one, :foo]
591
- ])
592
- end
593
-
594
- context "multiple indexes" do
595
- subject { idx.subset :b, :one }
596
-
597
- it { is_expected.to be_a described_class }
598
- its(:size) { is_expected.to eq 2 }
599
- its(:to_a) { is_expected.to eq [[:bar], [:foo]] }
600
- end
601
-
602
- context "multiple positional indexes" do
603
- subject { idx.subset 0, 1 }
604
-
605
- it { is_expected.to be_a described_class }
606
- its(:size) { is_expected.to eq 2 }
607
- its(:to_a) { is_expected.to eq [[:b, :one, :bar], [:b, :two, :bar]] }
608
- end
609
-
610
- context "multiple tuple indexes" do
611
- subject { idx.subset [:b,:two,:bar], [:b,:one,:foo] }
612
-
613
- it { is_expected.to be_a described_class }
614
- its(:size) { is_expected.to eq 2 }
615
- its(:to_a) { is_expected.to eq [[:b,:two,:bar], [:b,:one,:foo]] }
616
- end
617
-
618
- # TODO: Checks for invalid indexes
619
- end
620
-
621
- context "at" do
622
- let(:idx) do
623
- described_class.from_tuples([
624
- [:b, :one, :bar],
625
- [:b, :two, :bar],
626
- [:b, :two, :baz],
627
- [:b, :one, :foo]
628
- ])
629
- end
630
-
631
- context "single position" do
632
- it { expect(idx.at 2).to eq [:b, :two, :baz] }
633
- end
634
-
635
- context "multiple positions" do
636
- subject { idx.at 1, 2 }
637
-
638
- it { is_expected.to be_a described_class }
639
- its(:size) { is_expected.to eq 2 }
640
- its(:to_a) { is_expected.to eq [[:b, :two, :bar],
641
- [:b, :two, :baz]] }
642
- end
643
-
644
- context "range" do
645
- subject { idx.at 1..2 }
646
-
647
- it { is_expected.to be_a described_class }
648
- its(:size) { is_expected.to eq 2 }
649
- its(:to_a) { is_expected.to eq [[:b, :two, :bar],
650
- [:b, :two, :baz]] }
651
- end
652
-
653
- context "range with negative integers" do
654
- subject { idx.at 1..-2 }
655
-
656
- it { is_expected.to be_a described_class }
657
- its(:size) { is_expected.to eq 2 }
658
- its(:to_a) { is_expected.to eq [[:b, :two, :bar],
659
- [:b, :two, :baz]] }
660
- end
661
-
662
- context "rangle with single element" do
663
- subject { idx.at 1..1 }
664
-
665
- it { is_expected.to be_a described_class }
666
- its(:size) { is_expected.to eq 1 }
667
- its(:to_a) { is_expected.to eq [[:b, :two, :bar]] }
668
- end
669
-
670
- context "invalid position" do
671
- it { expect { idx.at 4 }.to raise_error IndexError }
672
- end
673
-
674
- context "invalid positions" do
675
- it { expect { idx.at 2, 4 }.to raise_error IndexError }
676
- end
677
- end
678
-
679
- context "#add" do
680
- let(:idx) do
681
- described_class.from_tuples [
682
- [:a, :one, :bar],
683
- [:a, :two, :bar],
684
- [:b, :two, :baz],
685
- [:b, :one, :foo]
686
- ]
687
- end
688
-
689
- context "single index" do
690
- subject { idx.add :b, :two, :baz }
691
-
692
- its(:to_a) { is_expected.to eq [
693
- [:a, :one, :bar],
694
- [:a, :two, :bar],
695
- [:b, :two, :baz],
696
- [:b, :one, :foo],
697
- [:b, :two, :baz]] }
698
- end
699
- end
700
-
701
- context "#valid?" do
702
- let(:idx) do
703
- described_class.from_tuples [
704
- [:a, :one, :bar],
705
- [:a, :two, :bar],
706
- [:b, :two, :baz],
707
- [:b, :one, :foo]
708
- ]
709
- end
710
-
711
- context "single index" do
712
- it { expect(idx.valid? :a, :one, :bar).to eq true }
713
- it { expect(idx.valid? :b, :two, :three).to eq false }
714
- end
715
-
716
- context "multiple indexes" do
717
- it { expect(idx.valid? :a, :one).to eq true }
718
- it { expect(idx.valid? :a, :three).to eq false }
719
- end
720
- end
721
-
722
- context '#to_df' do
723
- let(:idx) do
724
- described_class.from_tuples([
725
- %w[a one bar],
726
- %w[a two bar],
727
- %w[b two baz],
728
- %w[b one foo]
729
- ]).tap { |idx| idx.name = %w[col1 col2 col3] }
730
- end
731
-
732
- subject { idx.to_df }
733
- it { is_expected.to eq DaruLite::DataFrame.new(
734
- 'col1' => %w[a a b b],
735
- 'col2' => %w[one two two one],
736
- 'col3' => %w[bar bar baz foo]
737
- )}
738
- end
739
-
740
- describe '#delete_at' do
741
- subject { index.delete_at(3)}
742
-
743
- let(:index) do
744
- DaruLite::MultiIndex.from_tuples([[:a, :one], [:a, :two], [:b, :one], [:b, :two], [:c, :one]])
745
- end
746
- let(:expected_index) do
747
- DaruLite::MultiIndex.from_tuples([[:a, :one], [:a, :two], [:b, :one], [:c, :one]])
748
- end
749
-
750
- it { is_expected.to eq(expected_index) }
751
- end
752
- end