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,300 +0,0 @@
1
- shared_examples_for 'a setable Vector' do |dtype|
2
- describe "#[]=" do
3
- context DaruLite::Index do
4
- let(:vector) do
5
- DaruLite::Vector.new(
6
- [1,2,3,4,5],
7
- name: :yoga,
8
- index: [:yoda, :anakin, :obi, :padme, :r2d2],
9
- dtype:
10
- )
11
- end
12
-
13
- it "assigns at the specified index" do
14
- vector[:yoda] = 666
15
- expect(vector[:yoda]).to eq(666)
16
- end
17
-
18
- it "assigns at the specified Integer index" do
19
- vector[0] = 666
20
- expect(vector[:yoda]).to eq(666)
21
- end
22
-
23
- it "sets dtype to Array if a nil is assigned" do
24
- vector[0] = nil
25
- expect(vector.dtype).to eq(:array)
26
- end
27
-
28
- context 'mixed index Vector' do
29
- let(:vector) do
30
- DaruLite::Vector.new([1, 2, 3, 4], index: ['a', :a, 0, 66])
31
- end
32
-
33
- it "assigns correctly" do
34
- vector['a'] = 666
35
- expect(vector['a']).to eq(666)
36
-
37
- vector[0] = 666
38
- expect(vector[0]).to eq(666)
39
-
40
- vector[3] = 666
41
- expect(vector[3]).to eq(666)
42
-
43
- expect(vector).to eq(
44
- DaruLite::Vector.new(
45
- [666, 2, 666, 666],
46
- index: ['a', :a, 0, 66])
47
- )
48
- end
49
- end
50
- end
51
-
52
- context DaruLite::MultiIndex do
53
- let(:tuples) do
54
- [
55
- [:a,:one,:bar],
56
- [:a,:one,:baz],
57
- [:a,:two,:bar],
58
- [:a,:two,:baz],
59
- [:b,:one,:bar],
60
- [:b,:two,:bar],
61
- [:b,:two,:baz],
62
- [:b,:one,:foo],
63
- [:c,:one,:bar],
64
- [:c,:one,:baz],
65
- [:c,:two,:foo],
66
- [:c,:two,:bar]
67
- ]
68
- end
69
- let(:multi_index) { DaruLite::MultiIndex.from_tuples(tuples) }
70
- let(:vector) do
71
- DaruLite::Vector.new(
72
- Array.new(12) { |i| i },
73
- index: multi_index,
74
- dtype:,
75
- name: :mi_vector
76
- )
77
- end
78
-
79
- it "assigns all lower layer indices when specified a first layer index" do
80
- vector[:b] = 69
81
- expect(vector).to eq(
82
- DaruLite::Vector.new([0, 1, 2, 3, 69, 69, 69, 69, 8, 9, 10, 11],
83
- index: multi_index,
84
- name: :top_layer_assignment,
85
- dtype:
86
- )
87
- )
88
- end
89
-
90
- it "assigns all lower indices when specified first and second layer index" do
91
- vector[:b, :one] = 69
92
- expect(vector).to eq(
93
- DaruLite::Vector.new([0, 1, 2, 3, 69, 5, 6, 69, 8, 9, 10, 11],
94
- index: multi_index,
95
- name: :second_layer_assignment,
96
- dtype:
97
- )
98
- )
99
- end
100
-
101
- it "assigns just the precise value when specified complete tuple" do
102
- vector[:b, :one, :foo] = 69
103
- expect(vector).to eq(
104
- DaruLite::Vector.new([0, 1, 2, 3, 4, 5, 6, 69, 8, 9, 10, 11],
105
- index: multi_index,
106
- name: :precise_assignment,
107
- dtype:
108
- )
109
- )
110
- end
111
-
112
- it "assigns correctly when numeric index" do
113
- vector[7] = 69
114
- expect(vector).to eq(
115
- DaruLite::Vector.new([0, 1, 2, 3, 4, 5, 6, 69, 8, 9, 10, 11],
116
- index: multi_index,
117
- name: :precise_assignment,
118
- dtype:
119
- )
120
- )
121
- end
122
-
123
- it "fails predictably on unknown index" do
124
- expect { vector[:d] = 69 }.to raise_error(IndexError)
125
- expect { vector[:b, :three] = 69 }.to raise_error(IndexError)
126
- expect { vector[:b, :two, :test] = 69 }.to raise_error(IndexError)
127
- end
128
- end
129
-
130
- context DaruLite::CategoricalIndex do
131
- subject { vector }
132
-
133
- context "non-numerical index" do
134
- let (:idx) { DaruLite::CategoricalIndex.new [:a, :b, :a, :a, :c] }
135
- let (:vector) { DaruLite::Vector.new 'a'..'e', index: idx }
136
-
137
- context "single category" do
138
- context "multiple instances" do
139
- before { vector[:a] = 'x' }
140
-
141
- its(:size) { is_expected.to eq 5 }
142
- its(:to_a) { is_expected.to eq ['x', 'b', 'x', 'x', 'e'] }
143
- its(:index) { is_expected.to eq idx }
144
- end
145
-
146
- context "single instance" do
147
- before { vector[:b] = 'x' }
148
-
149
- its(:size) { is_expected.to eq 5 }
150
- its(:to_a) { is_expected.to eq ['a', 'x', 'c', 'd', 'e'] }
151
- its(:index) { is_expected.to eq idx }
152
- end
153
- end
154
-
155
- context "multiple categories" do
156
- before { vector[:a, :c] = 'x' }
157
-
158
- its(:size) { is_expected.to eq 5 }
159
- its(:to_a) { is_expected.to eq ['x', 'b', 'x', 'x', 'x'] }
160
- its(:index) { is_expected.to eq idx }
161
- end
162
-
163
- context "multiple positional indexes" do
164
- before { vector[0, 1, 2] = 'x' }
165
-
166
- its(:size) { is_expected.to eq 5 }
167
- its(:to_a) { is_expected.to eq ['x', 'x', 'x', 'd', 'e'] }
168
- its(:index) { is_expected.to eq idx }
169
- end
170
-
171
- context "single positional index" do
172
- before { vector[1] = 'x' }
173
-
174
- its(:size) { is_expected.to eq 5 }
175
- its(:to_a) { is_expected.to eq ['a', 'x', 'c', 'd', 'e'] }
176
- its(:index) { is_expected.to eq idx }
177
- end
178
-
179
- context "invalid category" do
180
- it { expect { vector[:x] = 'x' }.to raise_error IndexError }
181
- end
182
-
183
- context "invalid positional index" do
184
- it { expect { vector[30] = 'x'}.to raise_error IndexError }
185
- end
186
- end
187
-
188
- context "numerical index" do
189
- let (:idx) { DaruLite::CategoricalIndex.new [1, 1, 2, 2, 3] }
190
- let (:vector) { DaruLite::Vector.new 'a'..'e', index: idx }
191
-
192
- context "single category" do
193
- before { vector[1] = 'x' }
194
-
195
- its(:size) { is_expected.to eq 5 }
196
- its(:to_a) { is_expected.to eq ['x', 'x', 'c', 'd', 'e'] }
197
- its(:index) { is_expected.to eq idx }
198
- end
199
-
200
- context "multiple categories" do
201
- before { vector[1, 2] = 'x' }
202
-
203
- its(:size) { is_expected.to eq 5 }
204
- its(:to_a) { is_expected.to eq ['x', 'x', 'x', 'x', 'e'] }
205
- its(:index) { is_expected.to eq idx }
206
- end
207
- end
208
- end
209
- end
210
-
211
- describe "#set_at" do
212
- context DaruLite::Index do
213
- let (:idx) { DaruLite::Index.new [1, 0, :c] }
214
- let (:dv) { DaruLite::Vector.new ['a', 'b', 'c'], index: idx }
215
-
216
- context "single position" do
217
- subject { dv }
218
- before { dv.set_at [1], 'x' }
219
-
220
- its(:to_a) { is_expected.to eq ['a', 'x', 'c'] }
221
- end
222
-
223
- context "multiple positions" do
224
- subject { dv }
225
- before { dv.set_at [0, 2], 'x' }
226
-
227
- its(:to_a) { is_expected.to eq ['x', 'b', 'x'] }
228
- end
229
-
230
- context "invalid position" do
231
- it { expect { dv.set_at [3], 'x' }.to raise_error IndexError }
232
- end
233
-
234
- context "invalid positions" do
235
- it { expect { dv.set_at [2, 3], 'x' }.to raise_error IndexError }
236
- end
237
- end
238
-
239
- context DaruLite::MultiIndex do
240
- let(:idx) do
241
- DaruLite::MultiIndex.from_tuples [
242
- [:a,:one,:bar],
243
- [:a,:one,:baz],
244
- [:b,:two,:bar],
245
- [:a,:two,:baz],
246
- ]
247
- end
248
- let(:dv) { DaruLite::Vector.new 1..4, index: idx }
249
-
250
- context "single position" do
251
- subject { dv }
252
- before { dv.set_at [1], 'x' }
253
-
254
- its(:to_a) { is_expected.to eq [1, 'x', 3, 4] }
255
- end
256
-
257
- context "multiple positions" do
258
- subject { dv }
259
- before { dv.set_at [2, 3], 'x' }
260
-
261
- its(:to_a) { is_expected.to eq [1, 2, 'x', 'x'] }
262
- end
263
-
264
- context "invalid position" do
265
- it { expect { dv.set_at [4], 'x' }.to raise_error IndexError }
266
- end
267
-
268
- context "invalid positions" do
269
- it { expect { dv.set_at [2, 4], 'x' }.to raise_error IndexError }
270
- end
271
- end
272
-
273
- context DaruLite::CategoricalIndex do
274
- let (:idx) { DaruLite::CategoricalIndex.new [:a, 1, 1, :a, :c] }
275
- let (:dv) { DaruLite::Vector.new 'a'..'e', index: idx }
276
-
277
- context "multiple positional indexes" do
278
- subject { dv }
279
- before { dv.set_at [0, 1, 2], 'x' }
280
-
281
- its(:to_a) { is_expected.to eq ['x', 'x', 'x', 'd', 'e'] }
282
- end
283
-
284
- context "single positional index" do
285
- subject { dv }
286
- before { dv.set_at [1], 'x' }
287
-
288
- its(:to_a) { is_expected.to eq ['a', 'x', 'c', 'd', 'e'] }
289
- end
290
-
291
- context "invalid position" do
292
- it { expect { dv.set_at [5], 'x' }.to raise_error IndexError }
293
- end
294
-
295
- context "invalid positions" do
296
- it { expect { dv.set_at [2, 5], 'x' }.to raise_error IndexError }
297
- end
298
- end
299
- end
300
- end
@@ -1,242 +0,0 @@
1
- shared_examples_for 'a sortable Vector' do |dtype|
2
- describe "#reorder!" do
3
- subject { vector_with_dtype.reorder!([3, 2, 1, 0]) }
4
-
5
- let(:vector_with_dtype) do
6
- DaruLite::Vector.new(
7
- [1, 2, 3, 4],
8
- index: [:a, :b, :c, :d],
9
- dtype:
10
- )
11
- end
12
- let(:arranged_vector) do
13
- DaruLite::Vector.new([4,3,2,1], index: [:d, :c, :b, :a], dtype:)
14
- end
15
-
16
- before { subject }
17
-
18
- it "rearranges with passed order" do
19
- expect(vector_with_dtype).to eq arranged_vector
20
- end
21
-
22
- it "doesn't change dtype" do
23
- expect(vector_with_dtype.data.class).to eq arranged_vector.data.class
24
- end
25
- end
26
-
27
-
28
- context "#sort" do
29
- context DaruLite::Index do
30
- let(:vector) do
31
- DaruLite::Vector.new([33, 2, 15, 332, 1], name: :dv, index: [:a, :b, :c, :d, :e])
32
- end
33
-
34
- it "sorts the vector with defaults and returns a new vector, preserving indexing" do
35
- expect(vector.sort).to eq(
36
- DaruLite::Vector.new([1,2,15,33,332], name: :dv, index: [:e, :b, :c, :a, :d])
37
- )
38
- end
39
-
40
- it "sorts the vector in descending order" do
41
- expect(vector.sort(ascending: false)).to eq(
42
- DaruLite::Vector.new([332,33,15,2,1], name: :dv, index: [:d, :a, :c, :b, :e])
43
- )
44
- end
45
-
46
- context 'when a block is passed' do
47
- subject { vector.sort { |a,b| a.length <=> b.length } }
48
-
49
- let(:vector) { DaruLite::Vector.new ["My Jazz Guitar", "Jazz", "My", "Guitar"] }
50
-
51
- it "sorts vector accordingly" do
52
- expect(subject).to eq(
53
- DaruLite::Vector.new(["My", "Jazz", "Guitar", "My Jazz Guitar"], index: [2, 1, 3, 0])
54
- )
55
- end
56
- end
57
-
58
- context 'when vector contains nils and numeric data' do
59
- let(:vector) { DaruLite::Vector.new([22, 4, nil, 111, nil, 2]) }
60
-
61
- it "places nils near the beginning of the vector when sorting ascendingly" do
62
- expect(vector.sort).to eq(
63
- DaruLite::Vector.new([nil, nil, 2, 4, 22, 111], index: [2, 4, 5, 1, 0, 3])
64
- )
65
- end if dtype == :array
66
-
67
- it "places nils near the beginning of the vector when sorting descendingly" do
68
- expect(vector.sort(ascending: false)).to eq(
69
- DaruLite::Vector.new [111, 22, 4, 2, nil, nil], index: [3, 0, 1, 5, 4, 2]
70
- )
71
- end
72
- end
73
-
74
- context 'when vector contains nils and non-numeric data' do
75
- let(:vector) { DaruLite::Vector.new(['a','b', nil, 'aa', '1234', nil]) }
76
-
77
- it "correctly sorts vector in ascending order" do
78
- expect(vector.sort(ascending: true)).to eq(
79
- DaruLite::Vector.new([nil, nil, '1234', 'a', 'aa', 'b'], index: [2, 5, 4, 0, 3, 1])
80
- )
81
- end
82
-
83
- it "correctly sorts vector in descending order" do
84
- expect(vector.sort(ascending: false)).to eq(
85
- DaruLite::Vector.new(['b', 'aa', 'a', '1234', nil, nil], index: [1, 3, 0, 4, 5, 2])
86
- )
87
- end
88
- end
89
- end
90
-
91
- context DaruLite::MultiIndex do
92
- let(:multi_index) do
93
- DaruLite::MultiIndex.from_tuples([
94
- [:a, :one, :foo],
95
- [:a, :two, :bar],
96
- [:b, :one, :bar],
97
- [:b, :two, :baz],
98
- [:b, :three, :bar]
99
- ])
100
- end
101
- let(:vector) do
102
- DaruLite::Vector.new(
103
- [44, 22, 111, 0, -56],
104
- index: multi_index,
105
- name: :unsorted,
106
- dtype:
107
- )
108
- end
109
-
110
- it "sorts vector" do
111
- mi_asc = DaruLite::MultiIndex.from_tuples([
112
- [:b, :three, :bar],
113
- [:b, :two, :baz],
114
- [:a, :two, :bar],
115
- [:a, :one, :foo],
116
- [:b, :one, :bar]
117
- ])
118
- expect(vector.sort).to eq(
119
- DaruLite::Vector.new([-56,0,22,44,111], index: mi_asc, name: :ascending, dtype:)
120
- )
121
- end
122
-
123
- it "sorts in descending" do
124
- mi_dsc = DaruLite::MultiIndex.from_tuples([
125
- [:b, :one, :bar],
126
- [:a, :one, :foo],
127
- [:a, :two, :bar],
128
- [:b, :two, :baz],
129
- [:b, :three, :bar]
130
- ])
131
- expect(vector.sort(ascending: false)).to eq(
132
- DaruLite::Vector.new([111, 44, 22, 0, -56], index: mi_dsc, name: :descending, dtype:)
133
- )
134
- end
135
-
136
- it "sorts using the supplied block" do
137
- mi_abs = DaruLite::MultiIndex.from_tuples([
138
- [:b, :two, :baz],
139
- [:a, :two, :bar],
140
- [:a, :one, :foo],
141
- [:b, :three, :bar],
142
- [:b, :one, :bar]
143
- ])
144
- expect(vector.sort { |a,b| a.abs <=> b.abs }).to eq(
145
- DaruLite::Vector.new([0, 22, 44, -56, 111], index: mi_abs, name: :sort_abs, dtype:)
146
- )
147
- end
148
- end
149
-
150
- context DaruLite::CategoricalIndex do
151
- let(:idx) { DaruLite::CategoricalIndex.new [:a, 1, :a, 1, :c] }
152
- let(:dv_numeric) { DaruLite::Vector.new [4, 5, 3, 2, 1], index: idx }
153
- let(:dv_string) { DaruLite::Vector.new ['xxxx', 'zzzzz', 'ccc', 'bb', 'a'], index: idx }
154
- let(:dv_nil) { DaruLite::Vector.new [3, nil, 2, 1, -1], index: idx }
155
-
156
- context "increasing order" do
157
- context "numeric" do
158
- subject { dv_numeric.sort }
159
-
160
- its(:size) { is_expected.to eq 5 }
161
- its(:to_a) { is_expected.to eq [1, 2, 3, 4, 5] }
162
- its(:'index.to_a') { is_expected.to eq [:c, 1, :a, :a, 1] }
163
- end
164
-
165
- context "non-numeric" do
166
- subject { dv_string.sort }
167
-
168
- its(:size) { is_expected.to eq 5 }
169
- its(:to_a) { is_expected.to eq ['a', 'bb', 'ccc', 'xxxx', 'zzzzz'] }
170
- its(:'index.to_a') { is_expected.to eq [:c, 1, :a, :a, 1] }
171
- end
172
-
173
- context "block" do
174
- subject { dv_string.sort { |a, b| a.length <=> b.length } }
175
-
176
- its(:to_a) { is_expected.to eq ['a', 'bb', 'ccc', 'xxxx', 'zzzzz'] }
177
- its(:'index.to_a') { is_expected.to eq [:c, 1, :a, :a, 1] }
178
- end
179
-
180
- context "nils" do
181
- subject { dv_nil.sort }
182
-
183
- its(:to_a) { is_expected.to eq [nil, -1, 1, 2, 3] }
184
- its(:'index.to_a') { is_expected.to eq [1, :c, 1, :a, :a] }
185
- end
186
- end
187
-
188
- context "decreasing order" do
189
- context "numeric" do
190
- subject { dv_numeric.sort(ascending: false) }
191
-
192
- its(:size) { is_expected.to eq 5 }
193
- its(:to_a) { is_expected.to eq [5, 4, 3, 2, 1] }
194
- its(:'index.to_a') { is_expected.to eq [1, :a, :a, 1, :c] }
195
- end
196
-
197
- context "non-numeric" do
198
- subject { dv_string.sort(ascending: false) }
199
-
200
- its(:size) { is_expected.to eq 5 }
201
- its(:to_a) { is_expected.to eq ['zzzzz', 'xxxx', 'ccc', 'bb', 'a'] }
202
- its(:'index.to_a') { is_expected.to eq [1, :a, :a, 1, :c] }
203
- end
204
-
205
- context "block" do
206
- subject do
207
- dv_string.sort(ascending: false) { |a, b| a.length <=> b.length }
208
- end
209
-
210
- its(:to_a) { is_expected.to eq ['zzzzz', 'xxxx', 'ccc', 'bb', 'a'] }
211
- its(:'index.to_a') { is_expected.to eq [1, :a, :a, 1, :c] }
212
- end
213
-
214
- context "nils" do
215
- subject { dv_nil.sort(ascending: false) }
216
-
217
- its(:to_a) { is_expected.to eq [3, 2, 1, -1, nil] }
218
- its(:'index.to_a') { is_expected.to eq [:a, :a, 1, :c, 1] }
219
- end
220
- end
221
- end
222
- end
223
-
224
- describe "#sort_by_index" do
225
- let(:asc) { vector.sort_by_index }
226
- let(:desc) { vector.sort_by_index(ascending: false) }
227
-
228
- context 'numeric vector' do
229
- let(:vector) { DaruLite::Vector.new [11, 13, 12], index: [23, 21, 22] }
230
-
231
- specify { expect(asc.to_a).to eq [13, 12, 11] }
232
- specify { expect(desc.to_a).to eq [11, 12, 13] }
233
- end
234
-
235
- context 'mix variable type index' do
236
- let(:vector) { DaruLite::Vector.new [11, Float::NAN, nil], index: [21, 23, 22] }
237
-
238
- specify { expect(asc.to_a).to eq [11, nil, Float::NAN] }
239
- specify { expect(desc.to_a).to eq [Float::NAN, nil, 11] }
240
- end
241
- end
242
- end