daru_lite 0.3.3 → 0.3.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 (150) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +60 -0
  3. data/README.md +1 -1
  4. data/lib/daru_lite/category.rb +1 -1
  5. data/lib/daru_lite/data_frame/duplicatable.rb +7 -1
  6. data/lib/daru_lite/data_frame/fetchable.rb +1 -1
  7. data/lib/daru_lite/data_frame/iterable.rb +5 -2
  8. data/lib/daru_lite/data_frame/sortable.rb +13 -5
  9. data/lib/daru_lite/dataframe.rb +52 -11
  10. data/lib/daru_lite/date_time/index.rb +2 -2
  11. data/lib/daru_lite/index/categorical_index.rb +62 -0
  12. data/lib/daru_lite/index/multi_index.rb +24 -6
  13. data/lib/daru_lite/maths/statistics/dataframe.rb +9 -5
  14. data/lib/daru_lite/maths/statistics/vector.rb +1 -1
  15. data/lib/daru_lite/version.rb +1 -1
  16. data/lib/daru_lite.rb +1 -0
  17. metadata +54 -284
  18. data/.github/ISSUE_TEMPLATE/bug_report.md +0 -38
  19. data/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
  20. data/.github/dependabot.yml +0 -11
  21. data/.github/workflows/ci.yml +0 -33
  22. data/.gitignore +0 -10
  23. data/.rspec +0 -2
  24. data/.rubocop.yml +0 -27
  25. data/.rubocop_todo.yml +0 -139
  26. data/CONTRIBUTING.md +0 -47
  27. data/Gemfile +0 -2
  28. data/Rakefile +0 -69
  29. data/ReleasePolicy.md +0 -20
  30. data/benchmarks/TradeoffData.csv +0 -65
  31. data/benchmarks/csv_reading.rb +0 -22
  32. data/benchmarks/dataframe_creation.rb +0 -39
  33. data/benchmarks/db_loading.rb +0 -33
  34. data/benchmarks/duplicating.rb +0 -45
  35. data/benchmarks/group_by.rb +0 -32
  36. data/benchmarks/joining.rb +0 -52
  37. data/benchmarks/row_access.rb +0 -41
  38. data/benchmarks/row_assign.rb +0 -36
  39. data/benchmarks/sorting.rb +0 -51
  40. data/benchmarks/statistics.rb +0 -28
  41. data/benchmarks/vector_access.rb +0 -31
  42. data/benchmarks/vector_assign.rb +0 -42
  43. data/benchmarks/where_clause.rb +0 -48
  44. data/benchmarks/where_vs_filter.rb +0 -28
  45. data/daru_lite.gemspec +0 -54
  46. data/daru_lite_test-shm +0 -0
  47. data/daru_lite_test-wal +0 -0
  48. data/images/README.md +0 -5
  49. data/images/con0.png +0 -0
  50. data/images/con1.png +0 -0
  51. data/images/init0.png +0 -0
  52. data/images/init1.png +0 -0
  53. data/images/man0.png +0 -0
  54. data/images/man1.png +0 -0
  55. data/images/man2.png +0 -0
  56. data/images/man3.png +0 -0
  57. data/images/man4.png +0 -0
  58. data/images/man5.png +0 -0
  59. data/images/man6.png +0 -0
  60. data/profile/_base.rb +0 -23
  61. data/profile/df_to_a.rb +0 -10
  62. data/profile/filter.rb +0 -13
  63. data/profile/joining.rb +0 -13
  64. data/profile/sorting.rb +0 -12
  65. data/profile/vector_each_with_index.rb +0 -9
  66. data/profile/vector_new.rb +0 -9
  67. data/spec/accessors/array_wrapper_spec.rb +0 -3
  68. data/spec/category_spec.rb +0 -1741
  69. data/spec/core/group_by_spec.rb +0 -655
  70. data/spec/core/merge_spec.rb +0 -179
  71. data/spec/core/query_spec.rb +0 -347
  72. data/spec/daru_lite_spec.rb +0 -22
  73. data/spec/data_frame/aggregatable_example.rb +0 -65
  74. data/spec/data_frame/buildable_example.rb +0 -109
  75. data/spec/data_frame/calculatable_example.rb +0 -135
  76. data/spec/data_frame/convertible_example.rb +0 -180
  77. data/spec/data_frame/duplicatable_example.rb +0 -111
  78. data/spec/data_frame/fetchable_example.rb +0 -481
  79. data/spec/data_frame/filterable_example.rb +0 -456
  80. data/spec/data_frame/indexable_example.rb +0 -221
  81. data/spec/data_frame/iterable_example.rb +0 -465
  82. data/spec/data_frame/joinable_example.rb +0 -131
  83. data/spec/data_frame/missable_example.rb +0 -47
  84. data/spec/data_frame/pivotable_example.rb +0 -297
  85. data/spec/data_frame/queryable_example.rb +0 -92
  86. data/spec/data_frame/setable_example.rb +0 -482
  87. data/spec/data_frame/sortable_example.rb +0 -350
  88. data/spec/dataframe_spec.rb +0 -1306
  89. data/spec/date_time/data_spec.rb +0 -197
  90. data/spec/date_time/date_time_index_helper_spec.rb +0 -72
  91. data/spec/date_time/index_spec.rb +0 -588
  92. data/spec/date_time/offsets_spec.rb +0 -465
  93. data/spec/extensions/which_dsl_spec.rb +0 -38
  94. data/spec/fixtures/bank2.dat +0 -200
  95. data/spec/fixtures/boolean_converter_test.csv +0 -5
  96. data/spec/fixtures/countries.json +0 -7794
  97. data/spec/fixtures/duplicates.csv +0 -32
  98. data/spec/fixtures/eciresults.html +0 -394
  99. data/spec/fixtures/empties.dat +0 -2
  100. data/spec/fixtures/empty_rows_test.csv +0 -17
  101. data/spec/fixtures/macau.html +0 -3691
  102. data/spec/fixtures/macd_data.csv +0 -150
  103. data/spec/fixtures/matrix_test.csv +0 -100
  104. data/spec/fixtures/moneycontrol.html +0 -6812
  105. data/spec/fixtures/music_data.tsv +0 -2501
  106. data/spec/fixtures/repeated_fields.csv +0 -7
  107. data/spec/fixtures/sales-funnel.csv +0 -18
  108. data/spec/fixtures/scientific_notation.csv +0 -4
  109. data/spec/fixtures/string_converter_test.csv +0 -5
  110. data/spec/fixtures/strings.dat +0 -2
  111. data/spec/fixtures/test_xls.xls +0 -0
  112. data/spec/fixtures/test_xls_2.xls +0 -0
  113. data/spec/fixtures/url_test.txt~ +0 -0
  114. data/spec/fixtures/valid_markup.html +0 -62
  115. data/spec/fixtures/wiki_climate.html +0 -1243
  116. data/spec/fixtures/wiki_table_info.html +0 -631
  117. data/spec/formatters/table_formatter_spec.rb +0 -137
  118. data/spec/helpers_spec.rb +0 -8
  119. data/spec/index/categorical_index_spec.rb +0 -221
  120. data/spec/index/index_spec.rb +0 -438
  121. data/spec/index/multi_index_spec.rb +0 -713
  122. data/spec/io/io_spec.rb +0 -357
  123. data/spec/io/sql_data_source_spec.rb +0 -48
  124. data/spec/iruby/dataframe_spec.rb +0 -170
  125. data/spec/iruby/helpers_spec.rb +0 -49
  126. data/spec/iruby/multi_index_spec.rb +0 -37
  127. data/spec/iruby/vector_spec.rb +0 -105
  128. data/spec/maths/arithmetic/dataframe_spec.rb +0 -148
  129. data/spec/maths/arithmetic/vector_spec.rb +0 -165
  130. data/spec/maths/statistics/dataframe_spec.rb +0 -178
  131. data/spec/maths/statistics/vector_spec.rb +0 -756
  132. data/spec/monkeys_spec.rb +0 -42
  133. data/spec/shared/vector_display_spec.rb +0 -213
  134. data/spec/spec_helper.rb +0 -87
  135. data/spec/support/database_helper.rb +0 -29
  136. data/spec/support/matchers.rb +0 -5
  137. data/spec/vector/aggregatable_example.rb +0 -27
  138. data/spec/vector/calculatable_example.rb +0 -82
  139. data/spec/vector/convertible_example.rb +0 -126
  140. data/spec/vector/duplicatable_example.rb +0 -48
  141. data/spec/vector/fetchable_example.rb +0 -463
  142. data/spec/vector/filterable_example.rb +0 -165
  143. data/spec/vector/indexable_example.rb +0 -201
  144. data/spec/vector/iterable_example.rb +0 -111
  145. data/spec/vector/joinable_example.rb +0 -25
  146. data/spec/vector/missable_example.rb +0 -88
  147. data/spec/vector/queryable_example.rb +0 -91
  148. data/spec/vector/setable_example.rb +0 -300
  149. data/spec/vector/sortable_example.rb +0 -242
  150. data/spec/vector_spec.rb +0 -599
@@ -1,713 +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 "#subset" 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
- context "multiple indexes" do
556
- subject { idx.subset :b, :one }
557
-
558
- it { is_expected.to be_a described_class }
559
- its(:size) { is_expected.to eq 2 }
560
- its(:to_a) { is_expected.to eq [[:bar], [:foo]] }
561
- end
562
-
563
- context "multiple positional indexes" do
564
- subject { idx.subset 0, 1 }
565
-
566
- it { is_expected.to be_a described_class }
567
- its(:size) { is_expected.to eq 2 }
568
- its(:to_a) { is_expected.to eq [[:b, :one, :bar], [:b, :two, :bar]] }
569
- end
570
-
571
- context "multiple tuple indexes" do
572
- subject { idx.subset [:b,:two,:bar], [:b,:one,:foo] }
573
-
574
- it { is_expected.to be_a described_class }
575
- its(:size) { is_expected.to eq 2 }
576
- its(:to_a) { is_expected.to eq [[:b,:two,:bar], [:b,:one,:foo]] }
577
- end
578
-
579
- # TODO: Checks for invalid indexes
580
- end
581
-
582
- context "at" do
583
- let(:idx) do
584
- described_class.from_tuples([
585
- [:b, :one, :bar],
586
- [:b, :two, :bar],
587
- [:b, :two, :baz],
588
- [:b, :one, :foo]
589
- ])
590
- end
591
-
592
- context "single position" do
593
- it { expect(idx.at 2).to eq [:b, :two, :baz] }
594
- end
595
-
596
- context "multiple positions" do
597
- subject { idx.at 1, 2 }
598
-
599
- it { is_expected.to be_a described_class }
600
- its(:size) { is_expected.to eq 2 }
601
- its(:to_a) { is_expected.to eq [[:b, :two, :bar],
602
- [:b, :two, :baz]] }
603
- end
604
-
605
- context "range" do
606
- subject { idx.at 1..2 }
607
-
608
- it { is_expected.to be_a described_class }
609
- its(:size) { is_expected.to eq 2 }
610
- its(:to_a) { is_expected.to eq [[:b, :two, :bar],
611
- [:b, :two, :baz]] }
612
- end
613
-
614
- context "range with negative integers" do
615
- subject { idx.at 1..-2 }
616
-
617
- it { is_expected.to be_a described_class }
618
- its(:size) { is_expected.to eq 2 }
619
- its(:to_a) { is_expected.to eq [[:b, :two, :bar],
620
- [:b, :two, :baz]] }
621
- end
622
-
623
- context "rangle with single element" do
624
- subject { idx.at 1..1 }
625
-
626
- it { is_expected.to be_a described_class }
627
- its(:size) { is_expected.to eq 1 }
628
- its(:to_a) { is_expected.to eq [[:b, :two, :bar]] }
629
- end
630
-
631
- context "invalid position" do
632
- it { expect { idx.at 4 }.to raise_error IndexError }
633
- end
634
-
635
- context "invalid positions" do
636
- it { expect { idx.at 2, 4 }.to raise_error IndexError }
637
- end
638
- end
639
-
640
- context "#add" do
641
- let(:idx) do
642
- described_class.from_tuples [
643
- [:a, :one, :bar],
644
- [:a, :two, :bar],
645
- [:b, :two, :baz],
646
- [:b, :one, :foo]
647
- ]
648
- end
649
-
650
- context "single index" do
651
- subject { idx.add :b, :two, :baz }
652
-
653
- its(:to_a) { is_expected.to eq [
654
- [:a, :one, :bar],
655
- [:a, :two, :bar],
656
- [:b, :two, :baz],
657
- [:b, :one, :foo],
658
- [:b, :two, :baz]] }
659
- end
660
- end
661
-
662
- context "#valid?" do
663
- let(:idx) do
664
- described_class.from_tuples [
665
- [:a, :one, :bar],
666
- [:a, :two, :bar],
667
- [:b, :two, :baz],
668
- [:b, :one, :foo]
669
- ]
670
- end
671
-
672
- context "single index" do
673
- it { expect(idx.valid? :a, :one, :bar).to eq true }
674
- it { expect(idx.valid? :b, :two, :three).to eq false }
675
- end
676
-
677
- context "multiple indexes" do
678
- it { expect(idx.valid? :a, :one).to eq true }
679
- it { expect(idx.valid? :a, :three).to eq false }
680
- end
681
- end
682
-
683
- context '#to_df' do
684
- let(:idx) do
685
- described_class.from_tuples([
686
- %w[a one bar],
687
- %w[a two bar],
688
- %w[b two baz],
689
- %w[b one foo]
690
- ]).tap { |idx| idx.name = %w[col1 col2 col3] }
691
- end
692
-
693
- subject { idx.to_df }
694
- it { is_expected.to eq DaruLite::DataFrame.new(
695
- 'col1' => %w[a a b b],
696
- 'col2' => %w[one two two one],
697
- 'col3' => %w[bar bar baz foo]
698
- )}
699
- end
700
-
701
- describe '#delete_at' do
702
- subject { index.delete_at(3)}
703
-
704
- let(:index) do
705
- DaruLite::MultiIndex.from_tuples([[:a, :one], [:a, :two], [:b, :one], [:b, :two], [:c, :one]])
706
- end
707
- let(:expected_index) do
708
- DaruLite::MultiIndex.from_tuples([[:a, :one], [:a, :two], [:b, :one], [:c, :one]])
709
- end
710
-
711
- it { is_expected.to eq(expected_index) }
712
- end
713
- end