daru_lite 0.3.4 → 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 (142) 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/date_time/index.rb +2 -2
  6. data/lib/daru_lite/maths/statistics/vector.rb +1 -1
  7. data/lib/daru_lite/version.rb +1 -1
  8. data/lib/daru_lite.rb +1 -0
  9. metadata +54 -284
  10. data/.github/ISSUE_TEMPLATE/bug_report.md +0 -38
  11. data/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
  12. data/.github/dependabot.yml +0 -11
  13. data/.github/workflows/ci.yml +0 -33
  14. data/.gitignore +0 -10
  15. data/.rspec +0 -2
  16. data/.rubocop.yml +0 -27
  17. data/.rubocop_todo.yml +0 -139
  18. data/CONTRIBUTING.md +0 -47
  19. data/Gemfile +0 -2
  20. data/Rakefile +0 -69
  21. data/ReleasePolicy.md +0 -20
  22. data/benchmarks/TradeoffData.csv +0 -65
  23. data/benchmarks/csv_reading.rb +0 -22
  24. data/benchmarks/dataframe_creation.rb +0 -39
  25. data/benchmarks/db_loading.rb +0 -33
  26. data/benchmarks/duplicating.rb +0 -45
  27. data/benchmarks/group_by.rb +0 -32
  28. data/benchmarks/joining.rb +0 -52
  29. data/benchmarks/row_access.rb +0 -41
  30. data/benchmarks/row_assign.rb +0 -36
  31. data/benchmarks/sorting.rb +0 -51
  32. data/benchmarks/statistics.rb +0 -28
  33. data/benchmarks/vector_access.rb +0 -31
  34. data/benchmarks/vector_assign.rb +0 -42
  35. data/benchmarks/where_clause.rb +0 -48
  36. data/benchmarks/where_vs_filter.rb +0 -28
  37. data/daru_lite.gemspec +0 -54
  38. data/daru_lite_test-shm +0 -0
  39. data/daru_lite_test-wal +0 -0
  40. data/images/README.md +0 -5
  41. data/images/con0.png +0 -0
  42. data/images/con1.png +0 -0
  43. data/images/init0.png +0 -0
  44. data/images/init1.png +0 -0
  45. data/images/man0.png +0 -0
  46. data/images/man1.png +0 -0
  47. data/images/man2.png +0 -0
  48. data/images/man3.png +0 -0
  49. data/images/man4.png +0 -0
  50. data/images/man5.png +0 -0
  51. data/images/man6.png +0 -0
  52. data/profile/_base.rb +0 -23
  53. data/profile/df_to_a.rb +0 -10
  54. data/profile/filter.rb +0 -13
  55. data/profile/joining.rb +0 -13
  56. data/profile/sorting.rb +0 -12
  57. data/profile/vector_each_with_index.rb +0 -9
  58. data/profile/vector_new.rb +0 -9
  59. data/spec/accessors/array_wrapper_spec.rb +0 -3
  60. data/spec/category_spec.rb +0 -1767
  61. data/spec/core/group_by_spec.rb +0 -655
  62. data/spec/core/merge_spec.rb +0 -179
  63. data/spec/core/query_spec.rb +0 -347
  64. data/spec/daru_lite_spec.rb +0 -22
  65. data/spec/data_frame/aggregatable_example.rb +0 -65
  66. data/spec/data_frame/buildable_example.rb +0 -109
  67. data/spec/data_frame/calculatable_example.rb +0 -135
  68. data/spec/data_frame/convertible_example.rb +0 -180
  69. data/spec/data_frame/duplicatable_example.rb +0 -134
  70. data/spec/data_frame/fetchable_example.rb +0 -510
  71. data/spec/data_frame/filterable_example.rb +0 -456
  72. data/spec/data_frame/indexable_example.rb +0 -221
  73. data/spec/data_frame/iterable_example.rb +0 -481
  74. data/spec/data_frame/joinable_example.rb +0 -131
  75. data/spec/data_frame/missable_example.rb +0 -47
  76. data/spec/data_frame/pivotable_example.rb +0 -297
  77. data/spec/data_frame/queryable_example.rb +0 -92
  78. data/spec/data_frame/setable_example.rb +0 -513
  79. data/spec/data_frame/sortable_example.rb +0 -368
  80. data/spec/dataframe_spec.rb +0 -1342
  81. data/spec/date_time/data_spec.rb +0 -197
  82. data/spec/date_time/date_time_index_helper_spec.rb +0 -72
  83. data/spec/date_time/index_spec.rb +0 -588
  84. data/spec/date_time/offsets_spec.rb +0 -465
  85. data/spec/extensions/which_dsl_spec.rb +0 -38
  86. data/spec/fixtures/bank2.dat +0 -200
  87. data/spec/fixtures/boolean_converter_test.csv +0 -5
  88. data/spec/fixtures/countries.json +0 -7794
  89. data/spec/fixtures/duplicates.csv +0 -32
  90. data/spec/fixtures/eciresults.html +0 -394
  91. data/spec/fixtures/empties.dat +0 -2
  92. data/spec/fixtures/empty_rows_test.csv +0 -17
  93. data/spec/fixtures/macau.html +0 -3691
  94. data/spec/fixtures/macd_data.csv +0 -150
  95. data/spec/fixtures/matrix_test.csv +0 -100
  96. data/spec/fixtures/moneycontrol.html +0 -6812
  97. data/spec/fixtures/music_data.tsv +0 -2501
  98. data/spec/fixtures/repeated_fields.csv +0 -7
  99. data/spec/fixtures/sales-funnel.csv +0 -18
  100. data/spec/fixtures/scientific_notation.csv +0 -4
  101. data/spec/fixtures/string_converter_test.csv +0 -5
  102. data/spec/fixtures/strings.dat +0 -2
  103. data/spec/fixtures/test_xls.xls +0 -0
  104. data/spec/fixtures/test_xls_2.xls +0 -0
  105. data/spec/fixtures/url_test.txt~ +0 -0
  106. data/spec/fixtures/valid_markup.html +0 -62
  107. data/spec/fixtures/wiki_climate.html +0 -1243
  108. data/spec/fixtures/wiki_table_info.html +0 -631
  109. data/spec/formatters/table_formatter_spec.rb +0 -137
  110. data/spec/helpers_spec.rb +0 -8
  111. data/spec/index/categorical_index_spec.rb +0 -289
  112. data/spec/index/index_spec.rb +0 -438
  113. data/spec/index/multi_index_spec.rb +0 -752
  114. data/spec/io/io_spec.rb +0 -357
  115. data/spec/io/sql_data_source_spec.rb +0 -48
  116. data/spec/iruby/dataframe_spec.rb +0 -170
  117. data/spec/iruby/helpers_spec.rb +0 -49
  118. data/spec/iruby/multi_index_spec.rb +0 -37
  119. data/spec/iruby/vector_spec.rb +0 -105
  120. data/spec/maths/arithmetic/dataframe_spec.rb +0 -148
  121. data/spec/maths/arithmetic/vector_spec.rb +0 -165
  122. data/spec/maths/statistics/dataframe_spec.rb +0 -191
  123. data/spec/maths/statistics/vector_spec.rb +0 -756
  124. data/spec/monkeys_spec.rb +0 -42
  125. data/spec/shared/vector_display_spec.rb +0 -213
  126. data/spec/spec_helper.rb +0 -87
  127. data/spec/support/database_helper.rb +0 -29
  128. data/spec/support/matchers.rb +0 -5
  129. data/spec/vector/aggregatable_example.rb +0 -27
  130. data/spec/vector/calculatable_example.rb +0 -82
  131. data/spec/vector/convertible_example.rb +0 -126
  132. data/spec/vector/duplicatable_example.rb +0 -48
  133. data/spec/vector/fetchable_example.rb +0 -463
  134. data/spec/vector/filterable_example.rb +0 -165
  135. data/spec/vector/indexable_example.rb +0 -201
  136. data/spec/vector/iterable_example.rb +0 -111
  137. data/spec/vector/joinable_example.rb +0 -25
  138. data/spec/vector/missable_example.rb +0 -88
  139. data/spec/vector/queryable_example.rb +0 -91
  140. data/spec/vector/setable_example.rb +0 -300
  141. data/spec/vector/sortable_example.rb +0 -242
  142. data/spec/vector_spec.rb +0 -599
@@ -1,1342 +0,0 @@
1
- require 'data_frame/aggregatable_example'
2
- require 'data_frame/buildable_example'
3
- require 'data_frame/calculatable_example'
4
- require 'data_frame/convertible_example'
5
- require 'data_frame/duplicatable_example'
6
- require 'data_frame/fetchable_example'
7
- require 'data_frame/filterable_example'
8
- require 'data_frame/indexable_example'
9
- require 'data_frame/iterable_example'
10
- require 'data_frame/joinable_example'
11
- require 'data_frame/missable_example'
12
- require 'data_frame/pivotable_example'
13
- require 'data_frame/queryable_example'
14
- require 'data_frame/setable_example'
15
- require 'data_frame/sortable_example'
16
-
17
- describe DaruLite::DataFrame do
18
- let(:df) do
19
- DaruLite::DataFrame.new(
20
- { b: [11,12,13,14,15], a: [1,2,3,4,5], c: [11,22,33,44,55] },
21
- order: [:a, :b, :c],
22
- index: [:one, :two, :three, :four, :five]
23
- )
24
- end
25
- let(:df_mi) do
26
- DaruLite::DataFrame.new(
27
- [vector_arry1, vector_arry2, vector_arry1, vector_arry2],
28
- order: order_mi,
29
- index: multi_index
30
- )
31
- end
32
- let(:vector_arry1) { [11,12,13,14,11,12,13,14,11,12,13,14] }
33
- let(:vector_arry2) { [1,2,3,4,1,2,3,4,1,2,3,4] }
34
- let(:multi_index) do
35
- tuples = [
36
- [:a,:one,:bar],
37
- [:a,:one,:baz],
38
- [:a,:two,:bar],
39
- [:a,:two,:baz],
40
- [:b,:one,:bar],
41
- [:b,:two,:bar],
42
- [:b,:two,:baz],
43
- [:b,:one,:foo],
44
- [:c,:one,:bar],
45
- [:c,:one,:baz],
46
- [:c,:two,:foo],
47
- [:c,:two,:bar]
48
- ]
49
- DaruLite::MultiIndex.from_tuples(tuples)
50
- end
51
- let(:order_mi) do
52
- DaruLite::MultiIndex.from_tuples(
53
- [
54
- [:a,:one,:bar],
55
- [:a,:two,:baz],
56
- [:b,:two,:foo],
57
- [:b,:one,:foo]
58
- ]
59
- )
60
- end
61
-
62
- it_behaves_like 'an aggregatable DataFrame'
63
- it_behaves_like 'a buildable DataFrame'
64
- it_behaves_like 'a calculatable DataFrame'
65
- it_behaves_like 'a convertible DataFrame'
66
- it_behaves_like 'a duplicatable DataFrame'
67
- it_behaves_like 'a fetchable DataFrame'
68
- it_behaves_like 'a filterable DataFrame'
69
- it_behaves_like 'an indexable DataFrame'
70
- it_behaves_like 'an iterable DataFrame'
71
- it_behaves_like 'a joinable DataFrame'
72
- it_behaves_like 'a missable DataFrame'
73
- it_behaves_like 'a pivotable DataFrame'
74
- it_behaves_like 'a queryable DataFrame'
75
- it_behaves_like 'a setable DataFrame'
76
- it_behaves_like 'a sortable DataFrame'
77
-
78
- context "#initialize" do
79
- it "initializes an empty DataFrame with no arguments" do
80
- df = DaruLite::DataFrame.new
81
- expect(df.nrows).to eq(0)
82
- expect(df.ncols).to eq(0)
83
- end
84
-
85
- context DaruLite::Index do
86
- it "initializes an empty DataFrame with empty source arg" do
87
- df = DaruLite::DataFrame.new({}, order: [:a, :b])
88
-
89
- expect(df.vectors).to eq(DaruLite::Index.new [:a, :b])
90
- expect(df.a.class).to eq(DaruLite::Vector)
91
- expect(df.a) .to eq([].dv(:a))
92
- end
93
-
94
- it "initializes from a Hash" do
95
- df = DaruLite::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5]}, order: [:a, :b],
96
- index: [:one, :two, :three, :four, :five])
97
-
98
- expect(df.index) .to eq(DaruLite::Index.new [:one, :two, :three, :four, :five])
99
- expect(df.vectors).to eq(DaruLite::Index.new [:a, :b])
100
- expect(df.a.class).to eq(DaruLite::Vector)
101
- expect(df.a) .to eq([1,2,3,4,5].dv(:a, df.index))
102
- end
103
-
104
- it "initializes from a Hash and preserves default order" do
105
- df = DaruLite::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5]},
106
- index: [:one, :two, :three, :four, :five])
107
-
108
- expect(df.vectors).to eq(DaruLite::Index.new [:b, :a])
109
- end
110
-
111
- it "initializes from a Hash of Vectors" do
112
- va = DaruLite::Vector.new([1,2,3,4,5], index: [:one, :two, :three, :four, :five])
113
- vb = DaruLite::Vector.new([11,12,13,14,15], index: [:one, :two, :three, :four, :five])
114
-
115
- df = DaruLite::DataFrame.new({ b: vb, a: va }, order: [:a, :b], index: [:one, :two, :three, :four, :five])
116
-
117
- expect(df.index) .to eq(DaruLite::Index.new [:one, :two, :three, :four, :five])
118
- expect(df.vectors).to eq(DaruLite::Index.new [:a, :b])
119
- expect(df.a.class).to eq(DaruLite::Vector)
120
- expect(df.a) .to eq([1,2,3,4,5].dv(:a, [:one, :two, :three, :four, :five]))
121
- end
122
-
123
- it "initializes from an Array of Hashes" do
124
- df = DaruLite::DataFrame.new([{a: 1, b: 11}, {a: false, b: 12}, {a: 3, b: 13},
125
- {a: 4, b: 14}, {a: 5, b: 15}], order: [:b, :a],
126
- index: [:one, :two, :three, :four, :five])
127
-
128
- expect(df.index) .to eq(DaruLite::Index.new [:one, :two, :three, :four, :five])
129
- expect(df.vectors).to eq(DaruLite::Index.new [:b, :a])
130
- expect(df.a.class).to eq(DaruLite::Vector)
131
- expect(df.a) .to eq([1,false,3,4,5].dv(:a,[:one, :two, :three, :four, :five]))
132
- end
133
-
134
- it "initializes from Array of Arrays" do
135
- df = DaruLite::DataFrame.new([[1]*5, [2]*5, [3]*5], order: [:b, :a, :c])
136
-
137
- expect(df.index) .to eq(DaruLite::Index.new(5))
138
- expect(df.vectors).to eq(DaruLite::Index.new([:b, :a, :c]))
139
- expect(df.a) .to eq(DaruLite::Vector.new([2]*5))
140
- end
141
-
142
- it "initializes from Array of Vectors" do
143
- df = DaruLite::DataFrame.new([DaruLite::Vector.new([1]*5), DaruLite::Vector.new([2]*5),
144
- DaruLite::Vector.new([3]*5)], order: [:b, :a, :c])
145
-
146
- expect(df.index) .to eq(DaruLite::Index.new(5))
147
- expect(df.vectors).to eq(DaruLite::Index.new([:b, :a, :c]))
148
- expect(df.a) .to eq(DaruLite::Vector.new([2]*5))
149
- end
150
-
151
- it "names the reindexed vectors after their order labels when indexes differ" do
152
- df = DaruLite::DataFrame.new(
153
- [
154
- DaruLite::Vector.new([1, 2], index: [:one, :two]),
155
- DaruLite::Vector.new([3, 4], index: [:two, :three])
156
- ],
157
- order: [:b, :a]
158
- )
159
-
160
- expect(df.data.map(&:name)).to eq([:b, :a])
161
- end
162
-
163
- it "accepts Index objects for row/col" do
164
- rows = DaruLite::Index.new [:one, :two, :three, :four, :five]
165
- cols = DaruLite::Index.new [:a, :b]
166
-
167
- df = DaruLite::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5]}, order: cols,
168
- index: rows)
169
-
170
- expect(df.a) .to eq(DaruLite::Vector.new([1,2,3,4,5], order: [:a], index: rows))
171
- expect(df.b) .to eq(DaruLite::Vector.new([11,12,13,14,15], name: :b, index: rows))
172
- expect(df.index) .to eq(DaruLite::Index.new [:one, :two, :three, :four, :five])
173
- expect(df.vectors).to eq(DaruLite::Index.new [:a, :b])
174
- end
175
-
176
- it "initializes without specifying row/col index" do
177
- df = DaruLite::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5]})
178
-
179
- expect(df.index) .to eq(DaruLite::Index.new [0,1,2,3,4])
180
- expect(df.vectors).to eq(DaruLite::Index.new [:b, :a])
181
- end
182
-
183
- it "aligns indexes properly" do
184
- df = DaruLite::DataFrame.new({
185
- b: [11,12,13,14,15].dv(:b, [:two, :one, :four, :five, :three]),
186
- a: [1,2,3,4,5].dv(:a, [:two,:one,:three, :four, :five])
187
- },
188
- order: [:a, :b]
189
- )
190
-
191
- expect(df).to eq(DaruLite::DataFrame.new({
192
- b: [14,13,12,15,11].dv(:b, [:five, :four, :one, :three, :two]),
193
- a: [5,4,2,3,1].dv(:a, [:five, :four, :one, :three, :two])
194
- }, order: [:a, :b])
195
- )
196
- end
197
-
198
- it "adds nil values for missing indexes and aligns by index" do
199
- df = DaruLite::DataFrame.new({
200
- b: [11,12,13,14,15].dv(:b, [:two, :one, :four, :five, :three]),
201
- a: [1,2,3] .dv(:a, [:two,:one,:three])
202
- },
203
- order: [:a, :b]
204
- )
205
-
206
- expect(df).to eq(DaruLite::DataFrame.new({
207
- b: [14,13,12,15,11].dv(:b, [:five, :four, :one, :three, :two]),
208
- a: [nil,nil,2,3,1].dv(:a, [:five, :four, :one, :three, :two])
209
- },
210
- order: [:a, :b])
211
- )
212
- end
213
-
214
- it "adds nils in first vector when other vectors have many extra indexes" do
215
- df = DaruLite::DataFrame.new({
216
- b: [11] .dv(nil, [:one]),
217
- a: [1,2,3] .dv(nil, [:one, :two, :three]),
218
- c: [11,22,33,44,55] .dv(nil, [:one, :two, :three, :four, :five]),
219
- d: [49,69,89,99,108,44].dv(nil, [:one, :two, :three, :four, :five, :six])
220
- }, order: [:a, :b, :c, :d],
221
- index: [:one, :two, :three, :four, :five, :six])
222
-
223
- expect(df).to eq(DaruLite::DataFrame.new({
224
- b: [11,nil,nil,nil,nil,nil].dv(nil, [:one, :two, :three, :four, :five, :six]),
225
- a: [1,2,3,nil,nil,nil] .dv(nil, [:one, :two, :three, :four, :five, :six]),
226
- c: [11,22,33,44,55,nil] .dv(nil, [:one, :two, :three, :four, :five, :six]),
227
- d: [49,69,89,99,108,44] .dv(nil, [:one, :two, :three, :four, :five, :six])
228
- }, order: [:a, :b, :c, :d],
229
- index: [:one, :two, :three, :four, :five, :six])
230
- )
231
- end
232
-
233
- it "correctly matches the supplied DataFrame index with the individual vector indexes" do
234
- df = DaruLite::DataFrame.new({
235
- b: [11,12,13] .dv(nil, [:one, :bleh, :blah]),
236
- a: [1,2,3,4,5].dv(nil, [:one, :two, :booh, :baah, :three]),
237
- c: [11,22,33,44,55].dv(nil, [0,1,3,:three, :two])
238
- }, order: [:a, :b, :c], index: [:one, :two, :three])
239
-
240
- expect(df).to eq(DaruLite::DataFrame.new({
241
- b: [11,nil,nil].dv(nil, [:one, :two, :three]),
242
- a: [1,2,5] .dv(nil, [:one, :two, :three]),
243
- c: [nil,55,44] .dv(nil, [:one, :two, :three]),
244
- },
245
- order: [:a, :b, :c], index: [:one, :two, :three]
246
- )
247
- )
248
- end
249
-
250
- it "completes incomplete vectors" do
251
- df = DaruLite::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5],
252
- c: [11,22,33,44,55]}, order: [:a, :c])
253
-
254
- expect(df.vectors).to eq([:a,:c,:b].to_index)
255
- end
256
-
257
- it "does not copy vectors when clone: false" do
258
- a = DaruLite::Vector.new([1,2,3,4,5])
259
- b = DaruLite::Vector.new([1,2,3,4,5])
260
- c = DaruLite::Vector.new([1,2,3,4,5])
261
- df = DaruLite::DataFrame.new({a: a, b: b, c: c}, clone: false)
262
-
263
- expect(df[:a].object_id).to eq(a.object_id)
264
- expect(df[:b].object_id).to eq(b.object_id)
265
- expect(df[:c].object_id).to eq(c.object_id)
266
- end
267
-
268
- it "allows creation of empty dataframe with only order" do
269
- df = DaruLite::DataFrame.new({}, order: [:a, :b, :c])
270
- df[:a] = DaruLite::Vector.new([1,2,3,4,5,6])
271
-
272
- expect(df.size).to eq(6)
273
- expect(df[:a]).to eq(DaruLite::Vector.new([1,2,3,4,5,6]))
274
- expect(df[:b]).to eq(DaruLite::Vector.new([nil,nil,nil,nil,nil,nil]))
275
- expect(df[:c]).to eq(DaruLite::Vector.new([nil,nil,nil,nil,nil,nil]))
276
- end
277
-
278
- it "allows creation of dataframe without specifying order or index" do
279
- df = DaruLite::DataFrame.new({})
280
- df[:a] = DaruLite::Vector.new([1,2,3,4,5])
281
-
282
- expect(df.size) .to eq(5)
283
- expect(df.index.to_a) .to eq([0,1,2,3,4])
284
- expect(df.vectors.to_a).to eq([:a])
285
- expect(df[:a]) .to eq(DaruLite::Vector.new([1,2,3,4,5]))
286
- end
287
-
288
- it "allows creation of dataframe with a default order" do
289
- arr_of_arrs_df = DaruLite::DataFrame.new([[1,2,3], [4,5,6], [7,8,9]])
290
- arr_of_vectors_df = DaruLite::DataFrame.new([DaruLite::Vector.new([1,2,3]), DaruLite::Vector.new([4,5,6]), DaruLite::Vector.new([7,8,9])])
291
-
292
- expect(arr_of_arrs_df.vectors.to_a).to eq([0,1,2])
293
- expect(arr_of_vectors_df.vectors.to_a).to eq([0,1,2])
294
- end
295
-
296
- it "raises error for incomplete DataFrame index" do
297
- expect {
298
- df = DaruLite::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5],
299
- c: [11,22,33,44,55]}, order: [:a, :b, :c],
300
- index: [:one, :two, :three])
301
- }.to raise_error
302
- end
303
-
304
- it "raises error for unequal sized vectors/arrays" do
305
- expect {
306
- df = DaruLite::DataFrame.new({b: [11,12,13], a: [1,2,3,4,5],
307
- c: [11,22,33,44,55]}, order: [:a, :b, :c],
308
- index: [:one, :two, :three])
309
- }.to raise_error
310
- end
311
- end
312
-
313
- context DaruLite::MultiIndex do
314
- it "creates empty DataFrame" do
315
- df = DaruLite::DataFrame.new({}, order: order_mi)
316
-
317
- expect(df.vectors).to eq(order_mi)
318
- expect(df[:a, :one, :bar]).to eq(DaruLite::Vector.new([]))
319
- end
320
-
321
- it "creates from Hash" do
322
- df = DaruLite::DataFrame.new({
323
- [:a,:one,:bar] => vector_arry1,
324
- [:a,:two,:baz] => vector_arry2,
325
- [:b,:one,:foo] => vector_arry1,
326
- [:b,:two,:foo] => vector_arry2
327
- }, order: order_mi, index: multi_index)
328
-
329
- expect(df.index) .to eq(multi_index)
330
- expect(df.vectors) .to eq(order_mi)
331
- expect(df[:a,:one,:bar]).to eq(DaruLite::Vector.new(vector_arry1,
332
- index: multi_index))
333
- end
334
-
335
- it "creates from Array of Hashes" do
336
- # TODO
337
- end
338
-
339
- it "creates from Array of Arrays" do
340
- df = DaruLite::DataFrame.new([vector_arry1, vector_arry2, vector_arry1,
341
- vector_arry2], index: multi_index, order: order_mi)
342
-
343
- expect(df.index) .to eq(multi_index)
344
- expect(df.vectors).to eq(order_mi)
345
- expect(df[:a, :one, :bar]).to eq(DaruLite::Vector.new(vector_arry1,
346
- index: multi_index))
347
- end
348
-
349
- it "raises error for order MultiIndex of different size than supplied Array" do
350
- expect {
351
- df = DaruLite::DataFrame.new([vector_arry1, vector_arry2], order: order_mi,
352
- index: multi_index)
353
- }.to raise_error
354
- end
355
-
356
- it "aligns MultiIndexes properly" do
357
- pending
358
- mi_a = order_mi
359
- mi_b = DaruLite::MultiIndex.from_tuples([
360
- [:b,:one,:foo],
361
- [:a,:one,:bar],
362
- [:b,:two,:foo],
363
- [:a,:one,:baz]
364
- ])
365
- mi_sorted = DaruLite::MultiIndex.from_tuples([
366
- [:a, :one, :bar],
367
- [:a, :one, :baz],
368
- [:b, :one, :foo],
369
- [:b, :two, :foo]
370
- ])
371
- order = DaruLite::MultiIndex.from_tuples([
372
- [:pee, :que],
373
- [:pee, :poo]
374
- ])
375
- a = DaruLite::Vector.new([1,2,3,4], index: mi_a)
376
- b = DaruLite::Vector.new([11,12,13,14], index: mi_b)
377
- df = DaruLite::DataFrame.new([b,a], order: order)
378
-
379
- expect(df).to eq(DaruLite::DataFrame.new({
380
- [:pee, :que] => DaruLite::Vector.new([1,2,4,3], index: mi_sorted),
381
- [:pee, :poo] => DaruLite::Vector.new([12,14,11,13], index: mi_sorted)
382
- }, order: order_mi))
383
- end
384
-
385
- it "adds nils in case of missing values" do
386
- # TODO
387
- end
388
-
389
- it "matches individual vector indexing with supplied DataFrame index" do
390
- # TODO
391
- end
392
- end
393
- end
394
-
395
- context '#method_missing' do
396
- let(:df) { DaruLite::DataFrame.new({
397
- :a => [1, 2, 3, 4, 5],
398
- 'b' => [5, 4, 3, 2, 1]
399
- }, index: 11..15)}
400
-
401
- context 'get vector' do
402
- context 'by string' do
403
- subject { df.b }
404
-
405
- it { is_expected.to be_a DaruLite::Vector }
406
- its(:to_a) { is_expected.to eq [5, 4, 3, 2, 1] }
407
- its(:'index.to_a') { is_expected.to eq [11, 12, 13, 14, 15] }
408
- end
409
-
410
- context 'by symbol' do
411
- subject { df.a }
412
-
413
- it { is_expected.to be_a DaruLite::Vector }
414
- its(:to_a) { is_expected.to eq [1, 2, 3, 4, 5] }
415
- its(:'index.to_a') { is_expected.to eq [11, 12, 13, 14, 15] }
416
- end
417
- end
418
-
419
- context 'set existing vector' do
420
- context 'by string' do
421
- before { df.b = [:a, :b, :c, :d, :e] }
422
- subject { df }
423
-
424
- it { is_expected.to be_a DaruLite::DataFrame }
425
- its(:'vectors.to_a') { is_expected.to eq [:a, 'b'] }
426
- its(:'b.to_a') { is_expected.to eq [:a, :b, :c, :d, :e] }
427
- its(:'index.to_a') { is_expected.to eq [11, 12, 13, 14, 15] }
428
- end
429
-
430
- context 'by symbol' do
431
- before { df.a = [:a, :b, :c, :d, :e] }
432
- subject { df }
433
-
434
- it { is_expected.to be_a DaruLite::DataFrame }
435
- its(:'vectors.to_a') { is_expected.to eq [:a, 'b'] }
436
- its(:'a.to_a') { is_expected.to eq [:a, :b, :c, :d, :e] }
437
- its(:'index.to_a') { is_expected.to eq [11, 12, 13, 14, 15] }
438
- end
439
- end
440
-
441
- context 'set new vector' do
442
- before { df.c = [5, 5, 5, 5, 5] }
443
- subject { df }
444
-
445
- it { is_expected.to be_a DaruLite::DataFrame }
446
- its(:'vectors.to_a') { is_expected.to eq [:a, 'b', :c] }
447
- its(:'c.to_a') { is_expected.to eq [5, 5, 5, 5, 5] }
448
- its(:'index.to_a') { is_expected.to eq [11, 12, 13, 14, 15] }
449
- end
450
-
451
- context 'reference invalid vector' do
452
- it { expect { df.d }.to raise_error NoMethodError }
453
- end
454
- end
455
-
456
- context "#row.at" do
457
- context DaruLite::Index do
458
- let(:idx) { DaruLite::Index.new [1, 0, :c] }
459
- let(:df) do
460
- DaruLite::DataFrame.new({
461
- a: 1..3,
462
- b: 'a'..'c'
463
- }, index: idx)
464
- end
465
-
466
- context "single position" do
467
- subject { df.row.at 1 }
468
-
469
- it { is_expected.to be_a DaruLite::Vector }
470
- its(:size) { is_expected.to eq 2 }
471
- its(:to_a) { is_expected.to eq [2, 'b'] }
472
- its(:'index.to_a') { is_expected.to eq [:a, :b] }
473
- end
474
-
475
- context "multiple positions" do
476
- subject { df.row.at 0, 2 }
477
-
478
- it { is_expected.to be_a DaruLite::DataFrame }
479
- its(:size) { is_expected.to eq 2 }
480
- its(:'index.to_a') { is_expected.to eq [1, :c] }
481
- its(:'a.to_a') { is_expected.to eq [1, 3] }
482
- its(:'b.to_a') { is_expected.to eq ['a', 'c'] }
483
- end
484
-
485
- context "invalid position" do
486
- it { expect { df.row.at 3 }.to raise_error IndexError }
487
- end
488
-
489
- context "invalid positions" do
490
- it { expect { df.row.at 2, 3 }.to raise_error IndexError }
491
- end
492
-
493
- context "range" do
494
- subject { df.row.at 0..1 }
495
-
496
- it { is_expected.to be_a DaruLite::DataFrame }
497
- its(:size) { is_expected.to eq 2 }
498
- its(:'index.to_a') { is_expected.to eq [1, 0] }
499
- its(:'a.to_a') { is_expected.to eq [1, 2] }
500
- its(:'b.to_a') { is_expected.to eq ['a', 'b'] }
501
- end
502
-
503
- context "range with negative end" do
504
- subject { df.row.at 0..-2 }
505
-
506
- it { is_expected.to be_a DaruLite::DataFrame }
507
- its(:size) { is_expected.to eq 2 }
508
- its(:'index.to_a') { is_expected.to eq [1, 0] }
509
- its(:'a.to_a') { is_expected.to eq [1, 2] }
510
- its(:'b.to_a') { is_expected.to eq ['a', 'b'] }
511
- end
512
-
513
- context "range with single element" do
514
- subject { df.row.at 0..0 }
515
-
516
- it { is_expected.to be_a DaruLite::DataFrame }
517
- its(:size) { is_expected.to eq 1 }
518
- its(:'index.to_a') { is_expected.to eq [1] }
519
- its(:'a.to_a') { is_expected.to eq [1] }
520
- its(:'b.to_a') { is_expected.to eq ['a'] }
521
- end
522
- end
523
-
524
- context DaruLite::MultiIndex do
525
- let (:idx) do
526
- DaruLite::MultiIndex.from_tuples [
527
- [:a,:one,:bar],
528
- [:a,:one,:baz],
529
- [:b,:two,:bar],
530
- [:a,:two,:baz],
531
- ]
532
- end
533
- let (:df) do
534
- DaruLite::DataFrame.new({
535
- a: 1..4,
536
- b: 'a'..'d'
537
- }, index: idx )
538
- end
539
-
540
- context "single position" do
541
- subject { df.row.at 1 }
542
-
543
- it { is_expected.to be_a DaruLite::Vector }
544
- its(:size) { is_expected.to eq 2 }
545
- its(:to_a) { is_expected.to eq [2, 'b'] }
546
- its(:'index.to_a') { is_expected.to eq [:a, :b] }
547
- end
548
-
549
- context "multiple positions" do
550
- subject { df.row.at 0, 2 }
551
-
552
- it { is_expected.to be_a DaruLite::DataFrame }
553
- its(:size) { is_expected.to eq 2 }
554
- its(:'index.to_a') { is_expected.to eq [[:a, :one, :bar],
555
- [:b, :two, :bar]] }
556
- its(:'a.to_a') { is_expected.to eq [1, 3] }
557
- its(:'a.index.to_a') { is_expected.to eq [[:a, :one, :bar],
558
- [:b, :two, :bar]] }
559
- its(:'b.to_a') { is_expected.to eq ['a', 'c'] }
560
- end
561
-
562
- context "invalid position" do
563
- it { expect { df.row.at 4 }.to raise_error IndexError }
564
- end
565
-
566
- context "invalid positions" do
567
- it { expect { df.row.at 3, 4 }.to raise_error IndexError }
568
- end
569
-
570
- context "range" do
571
- subject { df.row.at 0..1 }
572
-
573
- it { is_expected.to be_a DaruLite::DataFrame }
574
- its(:size) { is_expected.to eq 2 }
575
- its(:'index.to_a') { is_expected.to eq [[:a, :one, :bar],
576
- [:a, :one, :baz]] }
577
- its(:'a.to_a') { is_expected.to eq [1, 2] }
578
- its(:'a.index.to_a') { is_expected.to eq [[:a, :one, :bar],
579
- [:a, :one, :baz]] }
580
- its(:'b.to_a') { is_expected.to eq ['a', 'b'] }
581
- end
582
-
583
- context "range with negative end" do
584
- subject { df.row.at 0..-3 }
585
-
586
- it { is_expected.to be_a DaruLite::DataFrame }
587
- its(:size) { is_expected.to eq 2 }
588
- its(:'index.to_a') { is_expected.to eq [[:a, :one, :bar],
589
- [:a, :one, :baz]] }
590
- its(:'a.to_a') { is_expected.to eq [1, 2] }
591
- its(:'a.index.to_a') { is_expected.to eq [[:a, :one, :bar],
592
- [:a, :one, :baz]] }
593
- its(:'b.to_a') { is_expected.to eq ['a', 'b'] }
594
- end
595
-
596
- context " range with single element" do
597
- subject { df.row.at 0..0 }
598
-
599
- it { is_expected.to be_a DaruLite::DataFrame }
600
- its(:size) { is_expected.to eq 1 }
601
- its(:'index.to_a') { is_expected.to eq [[:a, :one, :bar]] }
602
- its(:'a.to_a') { is_expected.to eq [1] }
603
- its(:'a.index.to_a') { is_expected.to eq [[:a, :one, :bar]] }
604
- its(:'b.to_a') { is_expected.to eq ['a'] }
605
- end
606
- end
607
-
608
- context DaruLite::CategoricalIndex do
609
- let (:idx) { DaruLite::CategoricalIndex.new [:a, 1, 1, :a, :c] }
610
- let (:df) do
611
- DaruLite::DataFrame.new({
612
- a: 1..5,
613
- b: 'a'..'e'
614
- }, index: idx )
615
- end
616
-
617
- context "single positional index" do
618
- subject { df.row.at 1 }
619
-
620
- it { is_expected.to be_a DaruLite::Vector }
621
- its(:size) { is_expected.to eq 2 }
622
- its(:to_a) { is_expected.to eq [2, 'b'] }
623
- its(:'index.to_a') { is_expected.to eq [:a, :b] }
624
- end
625
-
626
- context "multiple positional indexes" do
627
- subject { df.row.at 0, 2 }
628
-
629
- it { is_expected.to be_a DaruLite::DataFrame }
630
- its(:size) { is_expected.to eq 2 }
631
- its(:'index.to_a') { is_expected.to eq [:a, 1] }
632
- its(:'a.to_a') { is_expected.to eq [1, 3] }
633
- its(:'a.index.to_a') { is_expected.to eq [:a, 1] }
634
- its(:'b.to_a') { is_expected.to eq ['a', 'c'] }
635
- its(:'b.index.to_a') { is_expected.to eq [:a, 1] }
636
- end
637
-
638
- context "invalid position" do
639
- it { expect { df.at 5 }.to raise_error IndexError }
640
- end
641
-
642
- context "invalid positions" do
643
- it { expect { df.at 4, 5 }.to raise_error IndexError }
644
- end
645
-
646
- context "range" do
647
- subject { df.row.at 0..1 }
648
-
649
- it { is_expected.to be_a DaruLite::DataFrame }
650
- its(:size) { is_expected.to eq 2 }
651
- its(:'index.to_a') { is_expected.to eq [:a, 1] }
652
- its(:'a.to_a') { is_expected.to eq [1, 2] }
653
- its(:'a.index.to_a') { is_expected.to eq [:a, 1] }
654
- its(:'b.to_a') { is_expected.to eq ['a', 'b'] }
655
- its(:'b.index.to_a') { is_expected.to eq [:a, 1] }
656
- end
657
-
658
- context "range with negative end" do
659
- subject { df.row.at 0..-4 }
660
-
661
- it { is_expected.to be_a DaruLite::DataFrame }
662
- its(:size) { is_expected.to eq 2 }
663
- its(:'index.to_a') { is_expected.to eq [:a, 1] }
664
- its(:'a.to_a') { is_expected.to eq [1, 2] }
665
- its(:'a.index.to_a') { is_expected.to eq [:a, 1] }
666
- its(:'b.to_a') { is_expected.to eq ['a', 'b'] }
667
- its(:'b.index.to_a') { is_expected.to eq [:a, 1] }
668
- end
669
-
670
- context " range with single element" do
671
- subject { df.row.at 0..0 }
672
-
673
- it { is_expected.to be_a DaruLite::DataFrame }
674
- its(:size) { is_expected.to eq 1 }
675
- its(:'index.to_a') { is_expected.to eq [:a] }
676
- its(:'a.to_a') { is_expected.to eq [1] }
677
- its(:'a.index.to_a') { is_expected.to eq [:a] }
678
- its(:'b.to_a') { is_expected.to eq ['a'] }
679
- its(:'b.index.to_a') { is_expected.to eq [:a] }
680
- end
681
- end
682
- end
683
-
684
- context "#row[]" do
685
- context DaruLite::Index do
686
- before :each do
687
- @df = DaruLite::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5],
688
- c: [11,22,33,44,55]}, order: [:a, :b, :c],
689
- index: [:one, :two, :three, :four, :five])
690
- end
691
-
692
- it "creates an index for assignment if not already specified" do
693
- @df.row[:one] = [49, 99, 59]
694
-
695
- expect(@df[:one, :row]) .to eq([49, 99, 59].dv(:one, [:a, :b, :c]))
696
- expect(@df[:one, :row].index).to eq([:a, :b, :c].to_index)
697
- expect(@df[:one, :row].name) .to eq(:one)
698
- end
699
-
700
- it "returns a DataFrame when specifying numeric Range" do
701
- expect(@df.row[0..2]).to eq(
702
- DaruLite::DataFrame.new({b: [11,12,13], a: [1,2,3],
703
- c: [11,22,33]}, order: [:a, :b, :c],
704
- index: [:one, :two, :three])
705
- )
706
- end
707
-
708
- it "returns a DataFrame when specifying symbolic Range" do
709
- expect(@df.row[:one..:three]).to eq(
710
- DaruLite::DataFrame.new({b: [11,12,13], a: [1,2,3],
711
- c: [11,22,33]}, order: [:a, :b, :c],
712
- index: [:one, :two, :three])
713
- )
714
- end
715
-
716
- it "returns a row with the given index" do
717
- expect(@df.row[:one]).to eq([1,11,11].dv(:one, [:a, :b, :c]))
718
- end
719
-
720
- it "returns a row with given Integer index" do
721
- expect(@df.row[0]).to eq([1,11,11].dv(:one, [:a, :b, :c]))
722
- end
723
-
724
- it "returns a row with given Integer index for default index-less DataFrame" do
725
- df = DaruLite::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5],
726
- c: [11,22,33,44,55]}, order: [:a, :b, :c])
727
-
728
- expect(df.row[0]).to eq([1,11,11].dv(nil, [:a, :b, :c]))
729
- expect(df.row[3]).to eq([4,14,44].dv(nil, [:a, :b, :c]))
730
- end
731
-
732
- it "returns a row with given Integer index for numerical index DataFrame" do
733
- df = DaruLite::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5],
734
- c: [11,22,33,44,55]}, order: [:a, :b, :c], index: [1,2,3,4,5])
735
-
736
- expect(df.row[0]).to eq([1,11,11].dv(nil, [:a, :b, :c]))
737
- expect(df.row[3]).to eq([3,13,33].dv(nil, [:a, :b, :c]))
738
- end
739
- end
740
-
741
- context DaruLite::MultiIndex do
742
- it "returns a Vector when specifying integer index" do
743
- expect(df_mi.row[0]).to eq(DaruLite::Vector.new([11,1,11,1], index: order_mi))
744
- end
745
-
746
- it "returns a DataFrame whecn specifying numeric range" do
747
- sub_index = DaruLite::MultiIndex.from_tuples([
748
- [:a,:one,:bar],
749
- [:a,:one,:baz]
750
- ])
751
-
752
- expect(df_mi.row[0..1]).to eq(DaruLite::DataFrame.new([
753
- [11,12],
754
- [1,2],
755
- [11,12],
756
- [1,2]
757
- ], order: order_mi, index: sub_index, name: :numeric_range))
758
- end
759
-
760
- it "returns a Vector when specifying complete tuple" do
761
- expect(df_mi.row[:c,:two,:foo]).to eq(DaruLite::Vector.new([13,3,13,3], index: order_mi))
762
- end
763
-
764
- it "returns DataFrame when specifying first layer of MultiIndex" do
765
- sub_index = DaruLite::MultiIndex.from_tuples([
766
- [:one,:bar],
767
- [:one,:baz],
768
- [:two,:foo],
769
- [:two,:bar]
770
- ])
771
- expect(df_mi.row[:c]).to eq(DaruLite::DataFrame.new([
772
- [11,12,13,14],
773
- [1,2,3,4],
774
- [11,12,13,14],
775
- [1,2,3,4]
776
- ], index: sub_index, order: order_mi))
777
- end
778
-
779
- it "returns DataFrame when specifying first and second layer of MultiIndex" do
780
- sub_index = DaruLite::MultiIndex.from_tuples([
781
- [:bar],
782
- [:baz]
783
- ])
784
- expect(df_mi.row[:c,:one]).to eq(DaruLite::DataFrame.new([
785
- [11,12],
786
- [1,2],
787
- [11,12],
788
- [1,2]
789
- ], index: sub_index, order: order_mi))
790
- end
791
- end
792
-
793
- context DaruLite::CategoricalIndex do
794
- let(:idx) { DaruLite::CategoricalIndex.new [:a, 1, :a, 1, :c] }
795
- let(:df) do
796
- DaruLite::DataFrame.new({
797
- a: 'a'..'e',
798
- b: 1..5
799
- }, index: idx)
800
- end
801
-
802
- context "single category" do
803
- context "multiple instances" do
804
- subject { df.row[:a] }
805
-
806
- it { is_expected.to be_a DaruLite::DataFrame }
807
- its(:index) { is_expected.to eq DaruLite::CategoricalIndex.new [:a, :a] }
808
- its(:vectors) { is_expected.to eq DaruLite::Index.new [:a, :b] }
809
- its(:a) { DaruLite::Vector.new ['a', 'c'] }
810
- its(:b) { DaruLite::Vector.new [1, 3] }
811
- end
812
-
813
- context "single instance" do
814
- subject { df.row[:c] }
815
-
816
- it { is_expected.to be_a DaruLite::Vector }
817
- its(:index) { is_expected.to eq DaruLite::Index.new [:a, :b] }
818
- its(:to_a) { is_expected.to eq ['e', 5] }
819
- end
820
- end
821
-
822
- context "multiple categories" do
823
- subject { df.row[:a, 1] }
824
-
825
- it { is_expected.to be_a DaruLite::DataFrame }
826
- its(:index) { is_expected.to eq DaruLite::CategoricalIndex.new(
827
- [:a, 1, :a, 1 ]) }
828
- its(:vectors) { is_expected.to eq DaruLite::Index.new [:a, :b] }
829
- its(:a) { DaruLite::Vector.new ['a', 'c', 'b', 'd'] }
830
- its(:b) { DaruLite::Vector.new [1, 3, 2, 4] }
831
- end
832
-
833
- context "positional index" do
834
- subject { df.row[0] }
835
-
836
- it { is_expected.to be_a DaruLite::Vector }
837
- its(:index) { is_expected.to eq DaruLite::Index.new [:a, :b] }
838
- its(:to_a) { is_expected.to eq ['a', 1] }
839
- end
840
-
841
- context "invalid positional index" do
842
- it { expect { df.row[5] }.to raise_error IndexError }
843
- end
844
-
845
- context "invalid category" do
846
- it { expect { df.row[:d] }.to raise_error IndexError }
847
- end
848
- end
849
- end
850
-
851
- context "#==" do
852
- it "compares by vectors, index and values of a DataFrame (ignores name)" do
853
- a = DaruLite::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5]},
854
- order: [:a, :b], index: [:one, :two, :three, :four, :five])
855
-
856
- b = DaruLite::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5]},
857
- order: [:a, :b], index: [:one, :two, :three, :four, :five])
858
-
859
- expect(a).to eq(b)
860
- end
861
- end
862
-
863
- context '#rename' do
864
- subject { df.rename 'other' }
865
-
866
- it { is_expected.to be_a DaruLite::DataFrame }
867
- its(:name) { is_expected.to eq 'other' }
868
- end
869
-
870
- context "#delete_vector" do
871
- context DaruLite::Index do
872
- it "deletes the specified vector" do
873
- df.delete_vector :a
874
-
875
- expect(df).to eq(DaruLite::DataFrame.new({b: [11,12,13,14,15],
876
- c: [11,22,33,44,55]}, order: [:b, :c],
877
- index: [:one, :two, :three, :four, :five]))
878
- end
879
- end
880
-
881
- context "with duplicate tuples in a MultiIndex" do
882
- let(:dup_df) do
883
- DaruLite::DataFrame.new(
884
- [[1, 2], [3, 4], [5, 6], [7, 8]],
885
- order: DaruLite::MultiIndex.from_tuples([
886
- [:weighted_count, 'Wave'], [:weighted_count, 'Wave'],
887
- [:percentage, 'Wave'], [:percentage, 'Wave']
888
- ]),
889
- index: %w[Yes No]
890
- )
891
- end
892
-
893
- it "deletes the first matching column, leaving the other in place" do
894
- dup_df.delete_vector([:weighted_count, 'Wave'])
895
-
896
- expect(dup_df.ncols).to eq(3)
897
- expect(dup_df.vectors).to be_a(DaruLite::MultiIndex)
898
- expect(dup_df.vectors.to_a).to eq([
899
- [:weighted_count, 'Wave'], [:percentage, 'Wave'], [:percentage, 'Wave']
900
- ])
901
- expect(dup_df.data.map(&:to_a)).to eq([[3, 4], [5, 6], [7, 8]])
902
- end
903
- end
904
- end
905
-
906
- context "#delete_vectors" do
907
- context DaruLite::Index do
908
- it "deletes the specified vectors" do
909
- df.delete_vectors :a, :b
910
-
911
- expect(df).to eq(DaruLite::DataFrame.new({
912
- c: [11,22,33,44,55]}, order: [:c],
913
- index: [:one, :two, :three, :four, :five]))
914
- end
915
- end
916
- end
917
-
918
- context "#delete_row" do
919
- it "deletes the specified row" do
920
- df.delete_row :three
921
-
922
- expect(df).to eq(DaruLite::DataFrame.new({b: [11,12,14,15], a: [1,2,4,5],
923
- c: [11,22,44,55]}, order: [:a, :b, :c], index: [:one, :two, :four, :five]))
924
- end
925
-
926
- context "when the data frame has a CategoricalIndex" do
927
- let(:categorical_df) do
928
- frame = DaruLite::DataFrame.new(
929
- { a: [1, 2, 3], b: [11, 22, 33] },
930
- order: [:a, :b],
931
- index: [:base, :one, :two]
932
- )
933
- frame.index = DaruLite::CategoricalIndex.new([:base, :one, :two])
934
- frame
935
- end
936
-
937
- it "deletes the specified row without raising" do
938
- expect { categorical_df.delete_row :base }.not_to raise_error
939
- expect(categorical_df.index.to_a).to eq [:one, :two]
940
- end
941
- end
942
- end
943
-
944
- context "#rename_vectors!" do
945
- before do
946
- @df = DaruLite::DataFrame.new({
947
- a: [1,2,3,4,5],
948
- b: [11,22,33,44,55],
949
- c: %w(a b c d e)
950
- })
951
- end
952
-
953
- it "returns self as modified dataframe" do
954
- expect(@df.rename_vectors!(:a => :alpha)).to eq(@df)
955
- end
956
-
957
- it "re-uses rename_vectors method" do
958
- name_map = { :a => :alpha, :c => :gamma }
959
- expect(@df).to receive(:rename_vectors).with(name_map)
960
- @df.rename_vectors! name_map
961
- end
962
- end
963
-
964
- context "#rename_vectors" do
965
- before do
966
- @df = DaruLite::DataFrame.new({
967
- a: [1,2,3,4,5],
968
- b: [11,22,33,44,55],
969
- c: %w(a b c d e)
970
- })
971
- end
972
-
973
- it "returns DaruLite::Index" do
974
- expect(@df.rename_vectors(:a => :alpha)).to be_kind_of(DaruLite::Index)
975
- end
976
-
977
- it "renames vectors using a hash map" do
978
- @df.rename_vectors :a => :alpha, :c => :gamma
979
- expect(@df.vectors.to_a).to eq([:alpha, :b, :gamma])
980
- end
981
-
982
- it "overwrites vectors if the new name already exists" do
983
- saved_vector = @df[:a].dup
984
-
985
- @df.rename_vectors :a => :b
986
- expect(@df.vectors.to_a).to eq([:b, :c])
987
- expect(@df[:b]).to eq saved_vector
988
- end
989
-
990
- it "makes no changes if the old and new names are the same" do
991
- saved_vector = @df[:a].dup
992
-
993
- @df.rename_vectors :a => :a
994
- expect(@df.vectors.to_a).to eq([:a, :b, :c])
995
- expect(@df[:a]).to eq saved_vector
996
- end
997
- end
998
-
999
- context "#add_level_to_vectors" do
1000
- subject { df.add_level_to_vectors(top_level_label) }
1001
-
1002
- let(:df) do
1003
- DaruLite::DataFrame.new({
1004
- a: [1, 2, 3, 4, 5],
1005
- b: [11, 22, 33, 44, 55],
1006
- c: %w(a b c d e)
1007
- })
1008
- end
1009
- let(:top_level_label) { :percentages }
1010
- let(:expected_index) do
1011
- DaruLite::MultiIndex.from_tuples([
1012
- [:percentages, :a], [:percentages, :b],[:percentages, :c],
1013
- ])
1014
- end
1015
-
1016
- it 'returns expected Multi::Index' do
1017
- expect(subject).to eq(expected_index)
1018
- end
1019
-
1020
- it 'updates dataframe vectors to the expected Multi::Index' do
1021
- expect { subject }.to change { df.vectors }.to(expected_index)
1022
- end
1023
- end
1024
-
1025
- context "#transpose" do
1026
- context DaruLite::Index do
1027
- it "transposes a DataFrame including row and column indexing" do
1028
- expect(df.transpose).to eq(DaruLite::DataFrame.new({
1029
- one: [1,11,11],
1030
- two: [2,12,22],
1031
- three: [3,13,33],
1032
- four: [4,14,44],
1033
- five: [5,15,55]
1034
- }, index: [:a, :b, :c],
1035
- order: [:one, :two, :three, :four, :five])
1036
- )
1037
- end
1038
- end
1039
-
1040
- context DaruLite::MultiIndex do
1041
- it "transposes a DataFrame including row and column indexing" do
1042
- expect(df_mi.transpose).to eq(DaruLite::DataFrame.new([
1043
- vector_arry1,
1044
- vector_arry2,
1045
- vector_arry1,
1046
- vector_arry2].transpose, index: order_mi, order: multi_index))
1047
- end
1048
- end
1049
- end
1050
-
1051
- context "#shape" do
1052
- it "returns an array containing number of rows and columns" do
1053
- expect(df.shape).to eq([5,3])
1054
- end
1055
- end
1056
-
1057
- context "#nest" do
1058
- it "nests in a hash" do
1059
- df = DaruLite::DataFrame.new({
1060
- :a => DaruLite::Vector.new(%w(a a a b b b)),
1061
- :b => DaruLite::Vector.new(%w(c c d d e e)),
1062
- :c => DaruLite::Vector.new(%w(f g h i j k))
1063
- })
1064
- nest = df.nest :a, :b
1065
- expect(nest['a']['c']).to eq([{ :c => 'f' }, { :c => 'g' }])
1066
- expect(nest['a']['d']).to eq([{ :c => 'h' }])
1067
- expect(nest['b']['e']).to eq([{ :c => 'j' }, { :c => 'k' }])
1068
- end
1069
- end
1070
-
1071
- context "#add_vectors_by_split_recode" do
1072
- before do
1073
- @ds = DaruLite::DataFrame.new({
1074
- :id => DaruLite::Vector.new([1, 2, 3, 4, 5]),
1075
- :name => DaruLite::Vector.new(%w(Alex Claude Peter Franz George)),
1076
- :age => DaruLite::Vector.new([20, 23, 25, 27, 5]),
1077
- :city => DaruLite::Vector.new(['New York', 'London', 'London', 'Paris', 'Tome']),
1078
- :a1 => DaruLite::Vector.new(['a,b', 'b,c', 'a', nil, 'a,b,c']) },
1079
- order: [:id, :name, :age, :city, :a1])
1080
- end
1081
-
1082
- it "" do
1083
- @ds.add_vectors_by_split_recode(:a1, '_')
1084
- expect(@ds.vectors.to_a) .to eq([:id, :name, :age, :city ,:a1, :a1_1, :a1_2, :a1_3])
1085
- expect(@ds[:a1_1].to_a).to eq([1, 0, 1, nil, 1])
1086
- expect(@ds[:a1_2].to_a).to eq([1, 1, 0, nil, 1])
1087
- expect(@ds[:a1_3].to_a).to eq([0, 1, 0, nil, 1])
1088
- end
1089
- end
1090
-
1091
- context "#add_vectors_by_split" do
1092
- before do
1093
- @ds = DaruLite::DataFrame.new({
1094
- :id => DaruLite::Vector.new([1, 2, 3, 4, 5]),
1095
- :name => DaruLite::Vector.new(%w(Alex Claude Peter Franz George)),
1096
- :age => DaruLite::Vector.new([20, 23, 25, 27, 5]),
1097
- :city => DaruLite::Vector.new(['New York', 'London', 'London', 'Paris', 'Tome']),
1098
- :a1 => DaruLite::Vector.new(['a,b', 'b,c', 'a', nil, 'a,b,c'])
1099
- }, order: [:id, :name, :age, :city, :a1])
1100
- end
1101
-
1102
- it "" do
1103
- @ds.add_vectors_by_split(:a1, '_')
1104
- expect(@ds.vectors.to_a).to eq([:id, :name, :age, :city, :a1, :a1_a, :a1_b, :a1_c])
1105
- expect(@ds[:a1_a].to_a).to eq([1, 0, 1, nil, 1])
1106
- expect(@ds[:a1_b].to_a).to eq([1, 1, 0, nil, 1])
1107
- expect(@ds[:a1_c].to_a).to eq([0, 1, 0, nil, 1])
1108
- end
1109
- end
1110
-
1111
- context ".crosstab_by_assignation" do
1112
- it "" do
1113
- v1 = DaruLite::Vector.new %w(a a a b b b c c c)
1114
- v2 = DaruLite::Vector.new %w(a b c a b c a b c)
1115
- v3 = DaruLite::Vector.new [0, 1, 0, 0, 1, 1, 0, 0, 1]
1116
- df = DaruLite::DataFrame.crosstab_by_assignation(v1, v2, v3)
1117
-
1118
- expect(df[:_id].type).to eq(:object)
1119
- expect(df['a'].type).to eq(:numeric)
1120
- expect(df['b'].type).to eq(:numeric)
1121
-
1122
- ev_id = DaruLite::Vector.new %w(a b c)
1123
- ev_a = DaruLite::Vector.new [0, 0, 0]
1124
- ev_b = DaruLite::Vector.new [1, 1, 0]
1125
- ev_c = DaruLite::Vector.new [0, 1, 1]
1126
- df2 = DaruLite::DataFrame.new({
1127
- :_id => ev_id, 'a' => ev_a, 'b' => ev_b, 'c' => ev_c },
1128
- order: ['a', 'b', 'c', :_id])
1129
-
1130
- expect(df2).to eq(df)
1131
- end
1132
-
1133
- it 'handles nil values in the rows vector' do
1134
- v1 = DaruLite::Vector.new [nil, 'a', nil, 'a']
1135
- v2 = DaruLite::Vector.new %w[x x y y]
1136
- v3 = DaruLite::Vector.new [1, 2, 3, 4]
1137
- df = DaruLite::DataFrame.crosstab_by_assignation(v1, v2, v3)
1138
-
1139
- expect(df[:_id].to_a).to eq([nil, 'a'])
1140
- expect(df['x'].to_a).to eq([1, 2])
1141
- expect(df['y'].to_a).to eq([3, 4])
1142
- end
1143
-
1144
- it 'raises IndexError when given duplicate (row, column) pairs' do
1145
- rows = DaruLite::Vector.new(%w[a a b])
1146
- columns = DaruLite::Vector.new(%w[x x y])
1147
- values = DaruLite::Vector.new([1, 2, 3])
1148
-
1149
- expect {
1150
- described_class.crosstab_by_assignation(rows, columns, values)
1151
- }.to raise_error(IndexError, /Duplicate/)
1152
- end
1153
- end
1154
-
1155
- context '#inspect' do
1156
- subject { df.inspect }
1157
-
1158
- context 'empty' do
1159
- let(:df) { DaruLite::DataFrame.new({}, order: %w[a b c])}
1160
- it { is_expected.to eq %Q{
1161
- |#<DaruLite::DataFrame(0x3)>
1162
- | a b c
1163
- }.unindent}
1164
- end
1165
-
1166
- context 'simple' do
1167
- let(:df) { DaruLite::DataFrame.new({a: [1,2,3], b: [3,4,5], c: [6,7,8]}, name: 'test')}
1168
- it { should == %Q{
1169
- |#<DaruLite::DataFrame: test (3x3)>
1170
- | a b c
1171
- | 0 1 3 6
1172
- | 1 2 4 7
1173
- | 2 3 5 8
1174
- }.unindent}
1175
- end
1176
-
1177
- context 'if index name is set' do
1178
- context 'single index with name' do
1179
- let(:df) { DaruLite::DataFrame.new({a: [1,2,3], b: [3,4,5], c: [6,7,8]},
1180
- name: 'test')}
1181
- before { df.index.name = 'index_name' }
1182
- it { should == %Q{
1183
- |#<DaruLite::DataFrame: test (3x3)>
1184
- | index_name a b c
1185
- | 0 1 3 6
1186
- | 1 2 4 7
1187
- | 2 3 5 8
1188
- }.unindent}
1189
- end
1190
-
1191
- context 'MultiIndex with name' do
1192
- let(:mi) { DaruLite::MultiIndex.new(
1193
- levels: [[:a,:b,:c], [:one, :two]],
1194
- labels: [[0,0,1,1,2,2], [0,1,0,1,0,1]], name: ['s1', 's2']) }
1195
- let(:df) { DaruLite::DataFrame.new({
1196
- a: [11, 12, 13, 14, 15, 16], b: [21, 22, 23, 24, 25, 26]},
1197
- name: 'test', index: mi)}
1198
- it { should == %Q{
1199
- |#<DaruLite::DataFrame: test (6x2)>
1200
- | s1 s2 a b
1201
- | a one 11 21
1202
- | two 12 22
1203
- | b one 13 23
1204
- | two 14 24
1205
- | c one 15 25
1206
- | two 16 26
1207
- }.unindent}
1208
- end
1209
-
1210
- end
1211
-
1212
- context 'no name' do
1213
- let(:df) { DaruLite::DataFrame.new({a: [1,2,3], b: [3,4,5], c: [6,7,8]})}
1214
- it { should == %Q{
1215
- |#<DaruLite::DataFrame(3x3)>
1216
- | a b c
1217
- | 0 1 3 6
1218
- | 1 2 4 7
1219
- | 2 3 5 8
1220
- }.unindent}
1221
- end
1222
-
1223
- context 'with nils' do
1224
- let(:df) { DaruLite::DataFrame.new({a: [1,nil,3], b: [3,4,5], c: [6,7,nil]}, name: 'test')}
1225
- it { is_expected.to eq %Q{
1226
- |#<DaruLite::DataFrame: test (3x3)>
1227
- | a b c
1228
- | 0 1 3 6
1229
- | 1 nil 4 7
1230
- | 2 3 5 nil
1231
- }.unindent}
1232
- end
1233
-
1234
- context 'with integers as vectors names' do
1235
- let(:df) { DaruLite::DataFrame.new({ 1 => [1,2,3], b: [3,4,5], c: [6,7,8] }, name: 'test')}
1236
-
1237
- it { is_expected.to eq %Q{
1238
- |#<DaruLite::DataFrame: test (3x3)>
1239
- | 1 b c
1240
- | 0 1 3 6
1241
- | 1 2 4 7
1242
- | 2 3 5 8
1243
- }.unindent}
1244
- end
1245
-
1246
- context 'very long' do
1247
- let(:df) { DaruLite::DataFrame.new({a: [1,1,1]*20, b: [1,1,1]*20, c: [1,1,1]*20}, name: 'test')}
1248
- it { is_expected.to eq %Q{
1249
- |#<DaruLite::DataFrame: test (60x3)>
1250
- | a b c
1251
- | 0 1 1 1
1252
- | 1 1 1 1
1253
- | 2 1 1 1
1254
- | 3 1 1 1
1255
- | 4 1 1 1
1256
- | 5 1 1 1
1257
- | 6 1 1 1
1258
- | 7 1 1 1
1259
- | 8 1 1 1
1260
- | 9 1 1 1
1261
- | 10 1 1 1
1262
- | 11 1 1 1
1263
- | 12 1 1 1
1264
- | 13 1 1 1
1265
- | 14 1 1 1
1266
- | 15 1 1 1
1267
- | 16 1 1 1
1268
- | 17 1 1 1
1269
- | 18 1 1 1
1270
- | 19 1 1 1
1271
- | 20 1 1 1
1272
- | 21 1 1 1
1273
- | 22 1 1 1
1274
- | 23 1 1 1
1275
- | 24 1 1 1
1276
- | 25 1 1 1
1277
- | 26 1 1 1
1278
- | 27 1 1 1
1279
- | 28 1 1 1
1280
- | 29 1 1 1
1281
- | ... ... ... ...
1282
- }.unindent}
1283
- end
1284
-
1285
- context 'long data lines' do
1286
- let(:df) { DaruLite::DataFrame.new({a: [1,2,3], b: [4,5,6], c: ['this is ridiculously long',nil,nil]}, name: 'test')}
1287
- it { is_expected.to eq %Q{
1288
- |#<DaruLite::DataFrame: test (3x3)>
1289
- | a b c
1290
- | 0 1 4 this is ri
1291
- | 1 2 5 nil
1292
- | 2 3 6 nil
1293
- }.unindent}
1294
- end
1295
-
1296
- context 'index is a MultiIndex' do
1297
- let(:df) {
1298
- DaruLite::DataFrame.new(
1299
- {
1300
- a: [1,2,3,4,5,6,7],
1301
- b: %w[a b c d e f g]
1302
- }, index: DaruLite::MultiIndex.from_tuples([
1303
- %w[foo one],
1304
- %w[foo two],
1305
- %w[foo three],
1306
- %w[bar one],
1307
- %w[bar two],
1308
- %w[bar three],
1309
- %w[baz one],
1310
- ]),
1311
- name: 'test'
1312
- )
1313
- }
1314
-
1315
- it { is_expected.to eq %Q{
1316
- |#<DaruLite::DataFrame: test (7x2)>
1317
- | a b
1318
- | foo one 1 a
1319
- | two 2 b
1320
- | three 3 c
1321
- | bar one 4 d
1322
- | two 5 e
1323
- | three 6 f
1324
- | baz one 7 g
1325
- }.unindent}
1326
- end
1327
-
1328
- context 'vectors is a MultiIndex' do
1329
- end
1330
-
1331
- context 'spacing and threshold settings' do
1332
- end
1333
- end
1334
-
1335
- context "#by_single_key" do
1336
- let(:df) { DaruLite::DataFrame.new(a: [1, 2, 3], b: [4, 5, 6] ) }
1337
-
1338
- it 'raise error when vector is missing from dataframe' do
1339
- expect { df[:c] }.to raise_error(IndexError, /Specified vector c does not exist/)
1340
- end
1341
- end
1342
- end if mri?