daru 0.0.2.3 → 0.0.3
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.
- checksums.yaml +4 -4
- data/History.txt +10 -0
- data/README.md +17 -10
- data/Rakefile +5 -0
- data/daru.gemspec +2 -0
- data/lib/daru.rb +1 -1
- data/lib/daru/dataframe.rb +426 -146
- data/lib/daru/dataframe_by_row.rb +15 -0
- data/lib/daru/dataframe_by_vector.rb +15 -0
- data/lib/daru/index.rb +83 -0
- data/lib/daru/io.rb +30 -0
- data/lib/daru/monkeys.rb +18 -10
- data/lib/daru/vector.rb +178 -47
- data/lib/version.rb +1 -1
- data/spec/dataframe_spec.rb +550 -0
- data/spec/fixtures/countries.json +7794 -0
- data/spec/index_spec.rb +54 -0
- data/spec/io_spec.rb +49 -0
- data/spec/monkeys_spec.rb +6 -0
- data/spec/spec_helper.rb +10 -1
- data/spec/vector_spec.rb +155 -0
- metadata +47 -10
- data/spec/jruby/dataframe_spec.rb +0 -1
- data/spec/jruby/vector_spec.rb +0 -20
- data/spec/mri/dataframe_spec.rb +0 -139
- data/spec/mri/vector_spec.rb +0 -104
data/lib/version.rb
CHANGED
@@ -0,0 +1,550 @@
|
|
1
|
+
require 'spec_helper.rb'
|
2
|
+
|
3
|
+
describe Daru::DataFrame do
|
4
|
+
before :each do
|
5
|
+
@data_frame = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5],
|
6
|
+
c: [11,22,33,44,55]}, [:a, :b, :c], [:one, :two, :three, :four, :five])
|
7
|
+
end
|
8
|
+
|
9
|
+
context "#initialize" do
|
10
|
+
it "initializes an empty DataFrame" do
|
11
|
+
df = Daru::DataFrame.new({}, [:a, :b])
|
12
|
+
|
13
|
+
expect(df.vectors).to eq(Daru::Index.new [:a, :b])
|
14
|
+
expect(df.a.class).to eq(Daru::Vector)
|
15
|
+
expect(df.a) .to eq([].dv(:a))
|
16
|
+
end
|
17
|
+
|
18
|
+
it "initializes from a Hash" do
|
19
|
+
df = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5]}, [:a, :b],
|
20
|
+
[:one, :two, :three, :four, :five])
|
21
|
+
|
22
|
+
expect(df.index) .to eq(Daru::Index.new [:one, :two, :three, :four, :five])
|
23
|
+
expect(df.vectors).to eq(Daru::Index.new [:a, :b])
|
24
|
+
expect(df.a.class).to eq(Daru::Vector)
|
25
|
+
expect(df.a) .to eq([1,2,3,4,5].dv(:a, df.index))
|
26
|
+
end
|
27
|
+
|
28
|
+
it "initializes from a Hash of Vectors", :focus => true do
|
29
|
+
df = Daru::DataFrame.new({b: [11,12,13,14,15].dv(:b, [:one, :two, :three, :four, :five]),
|
30
|
+
a: [1,2,3,4,5].dv(:a, [:one, :two, :three, :four, :five])}, [:a, :b],
|
31
|
+
[:one, :two, :three, :four, :five])
|
32
|
+
|
33
|
+
expect(df.index) .to eq(Daru::Index.new [:one, :two, :three, :four, :five])
|
34
|
+
expect(df.vectors).to eq(Daru::Index.new [:a, :b])
|
35
|
+
expect(df.a.class).to eq(Daru::Vector)
|
36
|
+
expect(df.a) .to eq([1,2,3,4,5].dv(:a, [:one, :two, :three, :four, :five]))
|
37
|
+
end
|
38
|
+
|
39
|
+
it "initializes from an Array of Hashes" do
|
40
|
+
df = Daru::DataFrame.new([{a: 1, b: 11}, {a: 2, b: 12}, {a: 3, b: 13},
|
41
|
+
{a: 4, b: 14}, {a: 5, b: 15}], [:b, :a], [:one, :two, :three, :four, :five])
|
42
|
+
|
43
|
+
expect(df.index) .to eq(Daru::Index.new [:one, :two, :three, :four, :five])
|
44
|
+
expect(df.vectors).to eq(Daru::Index.new [:b, :a])
|
45
|
+
expect(df.a.class).to eq(Daru::Vector)
|
46
|
+
expect(df.a) .to eq([1,2,3,4,5].dv(:a,[:one, :two, :three, :four, :five]))
|
47
|
+
end
|
48
|
+
|
49
|
+
it "accepts Index objects for row/col" do
|
50
|
+
rows = Daru::Index.new [:one, :two, :three, :four, :five]
|
51
|
+
cols = Daru::Index.new [:a, :b]
|
52
|
+
|
53
|
+
df = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5]}, cols, rows)
|
54
|
+
|
55
|
+
expect(df.a) .to eq(Daru::Vector.new(:a, [1,2,3,4,5] , rows))
|
56
|
+
expect(df.b) .to eq(Daru::Vector.new(:b, [11,12,13,14,15], rows))
|
57
|
+
expect(df.index) .to eq(Daru::Index.new [:one, :two, :three, :four, :five])
|
58
|
+
expect(df.vectors).to eq(Daru::Index.new [:a, :b])
|
59
|
+
end
|
60
|
+
|
61
|
+
it "initializes without specifying row/col index" do
|
62
|
+
df = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5]})
|
63
|
+
|
64
|
+
expect(df.index) .to eq(Daru::Index.new [0,1,2,3,4])
|
65
|
+
expect(df.vectors).to eq(Daru::Index.new [:a, :b])
|
66
|
+
end
|
67
|
+
|
68
|
+
it "aligns indexes properly" do
|
69
|
+
df = Daru::DataFrame.new({
|
70
|
+
b: [11,12,13,14,15].dv(:b, [:two, :one, :four, :five, :three]),
|
71
|
+
a: [1,2,3,4,5].dv(:a, [:two,:one,:three, :four, :five])
|
72
|
+
},
|
73
|
+
[:a, :b]
|
74
|
+
)
|
75
|
+
|
76
|
+
expect(df).to eq(Daru::DataFrame.new({
|
77
|
+
b: [14,13,12,15,11].dv(:b, [:five, :four, :one, :three, :two]),
|
78
|
+
a: [5,4,2,3,1].dv(:a, [:five, :four, :one, :three, :two])
|
79
|
+
}, [:a, :b])
|
80
|
+
)
|
81
|
+
end
|
82
|
+
|
83
|
+
it "adds nil values for missing indexes and aligns by index" do
|
84
|
+
df = Daru::DataFrame.new({
|
85
|
+
b: [11,12,13,14,15].dv(:b, [:two, :one, :four, :five, :three]),
|
86
|
+
a: [1,2,3] .dv(:a, [:two,:one,:three])
|
87
|
+
},
|
88
|
+
[:a, :b]
|
89
|
+
)
|
90
|
+
|
91
|
+
expect(df).to eq(Daru::DataFrame.new({
|
92
|
+
b: [14,13,12,15,11].dv(:b, [:five, :four, :one, :three, :two]),
|
93
|
+
a: [nil,nil,2,3,1].dv(:a, [:five, :four, :one, :three, :two])
|
94
|
+
},
|
95
|
+
[:a, :b])
|
96
|
+
)
|
97
|
+
end
|
98
|
+
|
99
|
+
it "adds nils in first vector when other vectors have many extra indexes" do
|
100
|
+
df = Daru::DataFrame.new({
|
101
|
+
b: [11] .dv(nil, [:one]),
|
102
|
+
a: [1,2,3] .dv(nil, [:one, :two, :three]),
|
103
|
+
c: [11,22,33,44,55] .dv(nil, [:one, :two, :three, :four, :five]),
|
104
|
+
d: [49,69,89,99,108,44].dv(nil, [:one, :two, :three, :four, :five, :six])
|
105
|
+
}, [:a, :b, :c, :d], [:one, :two, :three, :four, :five, :six])
|
106
|
+
|
107
|
+
expect(df).to eq(Daru::DataFrame.new({
|
108
|
+
b: [11,nil,nil,nil,nil,nil].dv(nil, [:one, :two, :three, :four, :five, :six]),
|
109
|
+
a: [1,2,3,nil,nil,nil] .dv(nil, [:one, :two, :three, :four, :five, :six]),
|
110
|
+
c: [11,22,33,44,55,nil] .dv(nil, [:one, :two, :three, :four, :five, :six]),
|
111
|
+
d: [49,69,89,99,108,44] .dv(nil, [:one, :two, :three, :four, :five, :six])
|
112
|
+
}, [:a, :b, :c, :d], [:one, :two, :three, :four, :five, :six])
|
113
|
+
)
|
114
|
+
end
|
115
|
+
|
116
|
+
it "correctly matches the supplied DataFrame index with the individual vector indexes" do
|
117
|
+
df = Daru::DataFrame.new({
|
118
|
+
b: [11,12,13] .dv(nil, [:one, :bleh, :blah]),
|
119
|
+
a: [1,2,3,4,5].dv(nil, [:one, :two, :booh, :baah, :three]),
|
120
|
+
c: [11,22,33,44,55].dv(nil, [0,1,3,:three, :two])
|
121
|
+
}, [:a, :b, :c], [:one, :two, :three])
|
122
|
+
|
123
|
+
expect(df).to eq(Daru::DataFrame.new({
|
124
|
+
b: [11,nil,nil].dv(nil, [:one, :two, :three]),
|
125
|
+
a: [1,2,5] .dv(nil, [:one, :two, :three]),
|
126
|
+
c: [nil,55,44] .dv(nil, [:one, :two, :three]),
|
127
|
+
},
|
128
|
+
[:a, :b, :c], [:one, :two, :three]
|
129
|
+
)
|
130
|
+
)
|
131
|
+
end
|
132
|
+
|
133
|
+
it "completes incomplete vectors" do
|
134
|
+
df = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5],
|
135
|
+
c: [11,22,33,44,55]}, [:a, :c])
|
136
|
+
|
137
|
+
expect(df.vectors).to eq([:a,:c,:b].to_index)
|
138
|
+
end
|
139
|
+
|
140
|
+
it "raises error for incomplete DataFrame index" do
|
141
|
+
expect {
|
142
|
+
df = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5],
|
143
|
+
c: [11,22,33,44,55]}, [:a, :b, :c], [:one, :two, :three])
|
144
|
+
}.to raise_error
|
145
|
+
end
|
146
|
+
|
147
|
+
it "raises error for unequal sized vectors/arrays" do
|
148
|
+
expect {
|
149
|
+
df = Daru::DataFrame.new({b: [11,12,13], a: [1,2,3,4,5],
|
150
|
+
c: [11,22,33,44,55]}, [:a, :b, :c], [:one, :two, :three])
|
151
|
+
}.to raise_error
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
context "#[:vector]" do
|
156
|
+
before :each do
|
157
|
+
@df = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5],
|
158
|
+
c: [11,22,33,44,55]}, [:a, :b, :c], [:one, :two, :three, :four, :five])
|
159
|
+
end
|
160
|
+
|
161
|
+
it "returns a Vector" do
|
162
|
+
expect(@df[:a, :vector]).to eq([1,2,3,4,5].dv(:a, [:one, :two, :three, :four, :five]))
|
163
|
+
end
|
164
|
+
|
165
|
+
it "returns a DataFrame" do
|
166
|
+
temp = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5]},
|
167
|
+
[:a, :b], [:one, :two, :three, :four, :five])
|
168
|
+
|
169
|
+
expect(@df[:a, :b, :vector]).to eq(temp)
|
170
|
+
end
|
171
|
+
|
172
|
+
it "accesses vector with Integer index" do
|
173
|
+
expect(@df[0, :vector]).to eq([1,2,3,4,5].dv(:a, [:one, :two, :three, :four, :five]))
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
context "#[:row]" do
|
178
|
+
before :each do
|
179
|
+
@df = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5],
|
180
|
+
c: [11,22,33,44,55]}, [:a, :b, :c], [:one, :two, :three, :four, :five])
|
181
|
+
end
|
182
|
+
|
183
|
+
it "returns a row with the given index" do
|
184
|
+
expect(@df[:one, :row]).to eq([1,11,11].dv(:one, [:a, :b, :c]))
|
185
|
+
end
|
186
|
+
|
187
|
+
it "returns a row with given Integer index" do
|
188
|
+
expect(@df[0, :row]).to eq([1,11,11].dv(:one, [:a, :b, :c]))
|
189
|
+
end
|
190
|
+
|
191
|
+
it "returns a row with given Integer index for default index-less DataFrame" do
|
192
|
+
df = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5],
|
193
|
+
c: [11,22,33,44,55]}, [:a, :b, :c])
|
194
|
+
|
195
|
+
expect(df[0, :row]).to eq([1,11,11].dv(nil, [:a, :b, :c]))
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
context "#[:vector]=" do
|
200
|
+
before :each do
|
201
|
+
@df = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5],
|
202
|
+
c: [11,22,33,44,55]}, [:a, :b, :c], [:one, :two, :three, :four, :five])
|
203
|
+
end
|
204
|
+
|
205
|
+
it "appends an Array as a Daru::Vector" do
|
206
|
+
@df[:d, :vector] = [69,99,108,85,49]
|
207
|
+
|
208
|
+
expect(@df.d.class).to eq(Daru::Vector)
|
209
|
+
end
|
210
|
+
|
211
|
+
it "replaces an already present vector" do
|
212
|
+
@df[:a, :vector] = [69,99,108,85,49].dv(nil, [:one, :two, :three, :four, :five])
|
213
|
+
|
214
|
+
expect(@df.a).to eq([69,99,108,85,49].dv(nil, [:one, :two, :three, :four, :five]))
|
215
|
+
end
|
216
|
+
|
217
|
+
it "appends a new vector to the DataFrame" do
|
218
|
+
@df[:woo, :vector] = [69,99,108,85,49].dv(nil, [:one, :two, :three, :four, :five])
|
219
|
+
|
220
|
+
expect(@df.vectors).to eq([:a, :b, :c, :woo].to_index)
|
221
|
+
end
|
222
|
+
|
223
|
+
it "creates an index for the new vector if not specified" do
|
224
|
+
@df[:woo, :vector] = [69,99,108,85,49]
|
225
|
+
|
226
|
+
expect(@df.woo.index).to eq([:one, :two, :three, :four, :five].to_index)
|
227
|
+
end
|
228
|
+
|
229
|
+
it "matches index of vector to be inserted with the DataFrame index" do
|
230
|
+
@df[:shankar, :vector] = [69,99,108,85,49].dv(:shankar, [:two, :one, :three, :five, :four])
|
231
|
+
|
232
|
+
expect(@df.shankar).to eq([99,69,108,49,85].dv(:shankar,
|
233
|
+
[:one, :two, :three, :four, :five]))
|
234
|
+
end
|
235
|
+
|
236
|
+
it "matches index of vector to be inserted, inserting nils where no match found" do
|
237
|
+
@df.vector[:shankar] = [1,2,3].dv(:shankar, [:one, :james, :hetfield])
|
238
|
+
|
239
|
+
expect(@df.shankar).to eq([1,nil,nil,nil,nil].dv(:shankar, [:one, :two, :three, :four, :five]))
|
240
|
+
end
|
241
|
+
|
242
|
+
it "raises error for Array assignment of wrong length" do
|
243
|
+
expect{
|
244
|
+
@df.vector[:shiva] = [1,2,3]
|
245
|
+
}.to raise_error
|
246
|
+
end
|
247
|
+
|
248
|
+
it "appends multiple vectors at a time" do
|
249
|
+
pending "Implement after initialize with array of arrays is done with."
|
250
|
+
|
251
|
+
# Rudimentary example. Yet to think this out.
|
252
|
+
|
253
|
+
@df[:woo, :boo, :vector] = [[69,99,108,85,49].dv(nil, [:one, :two, :three, :four, :five]),
|
254
|
+
[69,99,108,85,49].dv(nil, [:one, :two, :three, :four, :five])]
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
context "#[:row]=" do
|
259
|
+
before :each do
|
260
|
+
@df = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5],
|
261
|
+
c: [11,22,33,44,55]}, [:a, :b, :c], [:one, :two, :three, :four, :five])
|
262
|
+
end
|
263
|
+
|
264
|
+
it "assigns specified row when Array" do
|
265
|
+
@df.row[:one] = [49, 99, 59]
|
266
|
+
|
267
|
+
expect(@df[:one, :row]) .to eq([49, 99, 59].dv(:one, [:a, :b, :c]))
|
268
|
+
expect(@df[:one, :row].index).to eq([:a, :b, :c].to_index)
|
269
|
+
expect(@df[:one, :row].name) .to eq(:one)
|
270
|
+
end
|
271
|
+
|
272
|
+
it "assigns specified row when DV" do
|
273
|
+
@df[:one, :row] = [49, 99, 59].dv(nil, [:a, :b, :c])
|
274
|
+
|
275
|
+
expect(@df[:one, :row]).to eq([49, 99, 59].dv(:one, [:a, :b, :c]))
|
276
|
+
end
|
277
|
+
|
278
|
+
it "creates a new row from an Array" do
|
279
|
+
@df.row[:patekar] = [9,2,11]
|
280
|
+
|
281
|
+
expect(@df[:patekar, :row]).to eq([9,2,11].dv(:patekar, [:a, :b, :c]))
|
282
|
+
end
|
283
|
+
|
284
|
+
it "creates a new row from a DV" do
|
285
|
+
@df.row[:patekar] = [9,2,11].dv(nil, [:a, :b, :c])
|
286
|
+
|
287
|
+
expect(@df[:patekar, :row]).to eq([9,2,11].dv(:patekar, [:a, :b, :c]))
|
288
|
+
end
|
289
|
+
|
290
|
+
it "creates a new row from numeric row index and named DV" do
|
291
|
+
@df.row[2] = [9,2,11].dv(nil, [:a, :b, :c])
|
292
|
+
|
293
|
+
expect(@df[2, :row]).to eq([9,2,11].dv(nil, [:a, :b, :c]))
|
294
|
+
end
|
295
|
+
|
296
|
+
it "correctly aligns assigned DV by index" do
|
297
|
+
@df.row[:two] = [9,2,11].dv(nil, [:b, :a, :c])
|
298
|
+
|
299
|
+
expect(@df.row[:two]).to eq([2,9,11].dv(:two, [:a, :b, :c]))
|
300
|
+
end
|
301
|
+
|
302
|
+
it "inserts nils for indexes that dont exist in the DataFrame" do
|
303
|
+
@df.row[:two] = [49, 99, 59].dv(nil, [:oo, :aah, :gaah])
|
304
|
+
|
305
|
+
expect(@df.row[:two]).to eq([nil,nil,nil].dv(nil, [:a, :b, :c]))
|
306
|
+
end
|
307
|
+
|
308
|
+
it "correctly inserts row of a different length by matching indexes" do
|
309
|
+
@df.row[:four] = [5,4,3,2,1,3].dv(nil, [:you, :have, :a, :big, :appetite, :spock])
|
310
|
+
|
311
|
+
expect(@df.row[:four]).to eq([3,nil,nil].dv(:four, [:a, :b, :c]))
|
312
|
+
end
|
313
|
+
|
314
|
+
it "raises error for row insertion by Array of wrong length" do
|
315
|
+
expect{
|
316
|
+
@df.row[:one] = [1,2,3,4,5,6,7]
|
317
|
+
}.to raise_error
|
318
|
+
end
|
319
|
+
|
320
|
+
it "returns a DataFrame when mutiple indexes specified" do
|
321
|
+
pending "Next release"
|
322
|
+
|
323
|
+
raise
|
324
|
+
end
|
325
|
+
end
|
326
|
+
|
327
|
+
context "#row" do
|
328
|
+
before :each do
|
329
|
+
@df = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5],
|
330
|
+
c: [11,22,33,44,55]}, [:a, :b, :c], [:one, :two, :three, :four, :five])
|
331
|
+
end
|
332
|
+
|
333
|
+
it "creates an index for assignment if not already specified" do
|
334
|
+
@df.row[:one] = [49, 99, 59]
|
335
|
+
|
336
|
+
expect(@df[:one, :row]) .to eq([49, 99, 59].dv(:one, [:a, :b, :c]))
|
337
|
+
expect(@df[:one, :row].index).to eq([:a, :b, :c].to_index)
|
338
|
+
expect(@df[:one, :row].name) .to eq(:one)
|
339
|
+
end
|
340
|
+
end
|
341
|
+
|
342
|
+
context "#vector" do
|
343
|
+
it "appends an Array as a Daru::Vector" do
|
344
|
+
@data_frame[:d, :vector] = [69,99,108,85,49]
|
345
|
+
|
346
|
+
expect(@data_frame.d.class).to eq(Daru::Vector)
|
347
|
+
end
|
348
|
+
end
|
349
|
+
|
350
|
+
context "#==" do
|
351
|
+
it "compares by vectors, index and values of a DataFrame (ignores name)" do
|
352
|
+
a = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5]},
|
353
|
+
[:a, :b], [:one, :two, :three, :four, :five])
|
354
|
+
|
355
|
+
b = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5]},
|
356
|
+
[:a, :b], [:one, :two, :three, :four, :five])
|
357
|
+
|
358
|
+
expect(a).to eq(b)
|
359
|
+
end
|
360
|
+
end
|
361
|
+
|
362
|
+
context "#dup" do
|
363
|
+
it "dups every data structure inside DataFrame" do
|
364
|
+
clo = @data_frame.dup
|
365
|
+
|
366
|
+
expect(clo.object_id) .not_to eq(@data_frame.object_id)
|
367
|
+
expect(clo.vectors.object_id).not_to eq(@data_frame.object_id)
|
368
|
+
expect(clo.index.object_id) .not_to eq(@data_frame.object_id)
|
369
|
+
|
370
|
+
@data_frame.each_vector_with_index do |vector, index|
|
371
|
+
expect(vector.object_id).not_to eq(clo.vector[index].object_id)
|
372
|
+
end
|
373
|
+
end
|
374
|
+
end
|
375
|
+
|
376
|
+
context "#each_vector" do
|
377
|
+
it "iterates over all vectors" do
|
378
|
+
ret = @data_frame.each_vector do |vector|
|
379
|
+
expect(vector.index).to eq([:one, :two, :three, :four, :five].to_index)
|
380
|
+
expect(vector.class).to eq(Daru::Vector)
|
381
|
+
end
|
382
|
+
|
383
|
+
expect(ret).to eq(@data_frame)
|
384
|
+
end
|
385
|
+
end
|
386
|
+
|
387
|
+
context "#each_vector_with_index" do
|
388
|
+
it "iterates over vectors with index" do
|
389
|
+
idxs = []
|
390
|
+
ret = @data_frame.each_vector_with_index do |vector, index|
|
391
|
+
idxs << index
|
392
|
+
expect(vector.index).to eq([:one, :two, :three, :four, :five].to_index)
|
393
|
+
expect(vector.class).to eq(Daru::Vector)
|
394
|
+
end
|
395
|
+
|
396
|
+
expect(idxs).to eq([:a, :b, :c])
|
397
|
+
|
398
|
+
expect(ret).to eq(@data_frame)
|
399
|
+
end
|
400
|
+
end
|
401
|
+
|
402
|
+
context "#each_row" do
|
403
|
+
it "iterates over rows" do
|
404
|
+
ret = @data_frame.each_row do |row|
|
405
|
+
expect(row.index).to eq([:a, :b, :c].to_index)
|
406
|
+
expect(row.class).to eq(Daru::Vector)
|
407
|
+
end
|
408
|
+
|
409
|
+
expect(ret).to eq(@data_frame)
|
410
|
+
end
|
411
|
+
end
|
412
|
+
|
413
|
+
context "#each_row_with_index" do
|
414
|
+
it "iterates over rows with indexes" do
|
415
|
+
idxs = []
|
416
|
+
ret = @data_frame.each_row_with_index do |row, idx|
|
417
|
+
idxs << idx
|
418
|
+
expect(row.index).to eq([:a, :b, :c].to_index)
|
419
|
+
expect(row.class).to eq(Daru::Vector)
|
420
|
+
end
|
421
|
+
|
422
|
+
expect(idxs).to eq([:one, :two, :three, :four, :five])
|
423
|
+
expect(ret) .to eq(@data_frame)
|
424
|
+
end
|
425
|
+
end
|
426
|
+
|
427
|
+
context "#map_vectors" do
|
428
|
+
it "iterates over vectors and returns a modified DataFrame" do
|
429
|
+
ans = Daru::DataFrame.new({b: [21,22,23,24,25], a: [11,12,13,14,15],
|
430
|
+
c: [21,32,43,54,65]}, [:a, :b, :c], [:one, :two, :three, :four, :five])
|
431
|
+
|
432
|
+
ret = @data_frame.map_vectors do |vector|
|
433
|
+
vector = vector.map { |e| e += 10}
|
434
|
+
end
|
435
|
+
|
436
|
+
expect(ret).to eq(ans)
|
437
|
+
expect(ret == @data_frame).to eq(false)
|
438
|
+
end
|
439
|
+
end
|
440
|
+
|
441
|
+
context "#map_vectors_with_index" do
|
442
|
+
it "iterates over vectors with index and returns a modified DataFrame" do
|
443
|
+
ans = Daru::DataFrame.new({b: [21,22,23,24,25], a: [11,12,13,14,15],
|
444
|
+
c: [21,32,43,54,65]}, [:a, :b, :c], [:one, :two, :three, :four, :five])
|
445
|
+
|
446
|
+
idx = []
|
447
|
+
ret = @data_frame.map_vectors_with_index do |vector, index|
|
448
|
+
idx << index
|
449
|
+
vector = vector.map { |e| e += 10}
|
450
|
+
end
|
451
|
+
|
452
|
+
expect(ret).to eq(ans)
|
453
|
+
expect(idx).to eq([:a, :b, :c])
|
454
|
+
end
|
455
|
+
end
|
456
|
+
|
457
|
+
context "#map_rows" do
|
458
|
+
it "iterates over rows and returns a modified DataFrame" do
|
459
|
+
ans = Daru::DataFrame.new({b: [121, 144, 169, 196, 225], a: [1,4,9,16,25],
|
460
|
+
c: [121, 484, 1089, 1936, 3025]}, [:a, :b, :c], [:one, :two, :three, :four, :five])
|
461
|
+
|
462
|
+
ret = @data_frame.map_rows do |row|
|
463
|
+
expect(row.class).to eq(Daru::Vector)
|
464
|
+
row = row.map { |e| e*e }
|
465
|
+
end
|
466
|
+
|
467
|
+
expect(ret).to eq(ans)
|
468
|
+
end
|
469
|
+
end
|
470
|
+
|
471
|
+
context "#map_rows_with_index" do
|
472
|
+
it "iterates over rows with index and returns a modified DataFrame" do
|
473
|
+
ans = Daru::DataFrame.new({b: [121, 144, 169, 196, 225], a: [1,4,9,16,25],
|
474
|
+
c: [121, 484, 1089, 1936, 3025]}, [:a, :b, :c], [:one, :two, :three, :four, :five])
|
475
|
+
|
476
|
+
idx = []
|
477
|
+
ret = @data_frame.map_rows_with_index do |row, index|
|
478
|
+
idx << index
|
479
|
+
expect(row.class).to eq(Daru::Vector)
|
480
|
+
row = row.map { |e| e*e }
|
481
|
+
end
|
482
|
+
|
483
|
+
expect(ret).to eq(ans)
|
484
|
+
expect(idx).to eq([:one, :two, :three, :four, :five])
|
485
|
+
end
|
486
|
+
end
|
487
|
+
|
488
|
+
context "#delete_vector" do
|
489
|
+
it "deletes the specified vector" do
|
490
|
+
@data_frame.delete_vector :a
|
491
|
+
|
492
|
+
expect(@data_frame).to eq(Daru::DataFrame.new({b: [11,12,13,14,15],
|
493
|
+
c: [11,22,33,44,55]}, [:b, :c], [:one, :two, :three, :four, :five]))
|
494
|
+
end
|
495
|
+
end
|
496
|
+
|
497
|
+
context "#delete_row" do
|
498
|
+
it "deletes the specified row" do
|
499
|
+
@data_frame.delete_row :one
|
500
|
+
|
501
|
+
expect(@data_frame).to eq(Daru::DataFrame.new({b: [12,13,14,15], a: [2,3,4,5],
|
502
|
+
c: [22,33,44,55]}, [:a, :b, :c], [:two, :three, :four, :five]))
|
503
|
+
end
|
504
|
+
end
|
505
|
+
|
506
|
+
context "#keep_row_if" do
|
507
|
+
it "keeps row if block evaluates to true" do
|
508
|
+
df = Daru::DataFrame.new({b: [10,12,20,23,30], a: [50,30,30,1,5],
|
509
|
+
c: [10,20,30,40,50]}, [:a, :b, :c], [:one, :two, :three, :four, :five])
|
510
|
+
|
511
|
+
df.keep_row_if do |row|
|
512
|
+
row.all? {|a| a % 5 == 0 }
|
513
|
+
end
|
514
|
+
|
515
|
+
expect(df).to eq(Daru::DataFrame.new({b: [10,20,30], a: [50, 30, 5],
|
516
|
+
c: [10,30,50]}, [:a, :b, :c], [:one, :three, :five]))
|
517
|
+
end
|
518
|
+
end
|
519
|
+
|
520
|
+
context "#keep_vector_if" do
|
521
|
+
it "keeps vector if block evaluates to true" do
|
522
|
+
@data_frame.keep_vector_if do |vector|
|
523
|
+
vector == [1,2,3,4,5].dv(nil, [:one, :two, :three, :four, :five])
|
524
|
+
end
|
525
|
+
|
526
|
+
expect(@data_frame).to eq(Daru::DataFrame.new({a: [1,2,3,4,5]}, [:a],
|
527
|
+
[:one, :two, :three, :four, :five]))
|
528
|
+
end
|
529
|
+
end
|
530
|
+
|
531
|
+
context "#to_a" do
|
532
|
+
it "converts DataFrame into array of hashes" do
|
533
|
+
arry = @data_frame.to_a
|
534
|
+
|
535
|
+
expect(arry).to eq(
|
536
|
+
[
|
537
|
+
[
|
538
|
+
{a: 1, b: 11, c: 11},
|
539
|
+
{a: 2, b: 12, c: 22},
|
540
|
+
{a: 3, b: 13, c: 33},
|
541
|
+
{a: 4, b: 14, c: 44},
|
542
|
+
{a: 5, b: 15, c: 55}
|
543
|
+
],
|
544
|
+
[
|
545
|
+
:one, :two, :three, :four, :five
|
546
|
+
]
|
547
|
+
])
|
548
|
+
end
|
549
|
+
end
|
550
|
+
end if mri?
|