daru_lite 0.1.1 → 0.1.2
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/.rubocop_todo.yml +35 -33
- data/lib/daru_lite/data_frame/aggregatable.rb +165 -0
- data/lib/daru_lite/data_frame/calculatable.rb +140 -0
- data/lib/daru_lite/data_frame/convertible.rb +107 -0
- data/lib/daru_lite/data_frame/duplicatable.rb +64 -0
- data/lib/daru_lite/data_frame/fetchable.rb +301 -0
- data/lib/daru_lite/data_frame/filterable.rb +144 -0
- data/lib/daru_lite/data_frame/i_o_able.rb +179 -0
- data/lib/daru_lite/data_frame/indexable.rb +168 -0
- data/lib/daru_lite/data_frame/iterable.rb +339 -0
- data/lib/daru_lite/data_frame/joinable.rb +152 -0
- data/lib/daru_lite/data_frame/missable.rb +75 -0
- data/lib/daru_lite/data_frame/pivotable.rb +108 -0
- data/lib/daru_lite/data_frame/queryable.rb +67 -0
- data/lib/daru_lite/data_frame/setable.rb +109 -0
- data/lib/daru_lite/data_frame/sortable.rb +241 -0
- data/lib/daru_lite/dataframe.rb +138 -2353
- data/lib/daru_lite/index/index.rb +13 -0
- data/lib/daru_lite/maths/statistics/vector.rb +1 -1
- data/lib/daru_lite/vector/aggregatable.rb +9 -0
- data/lib/daru_lite/vector/calculatable.rb +78 -0
- data/lib/daru_lite/vector/convertible.rb +77 -0
- data/lib/daru_lite/vector/duplicatable.rb +17 -0
- data/lib/daru_lite/vector/fetchable.rb +175 -0
- data/lib/daru_lite/vector/filterable.rb +128 -0
- data/lib/daru_lite/vector/indexable.rb +77 -0
- data/lib/daru_lite/vector/iterable.rb +95 -0
- data/lib/daru_lite/vector/joinable.rb +17 -0
- data/lib/daru_lite/vector/missable.rb +124 -0
- data/lib/daru_lite/vector/queryable.rb +45 -0
- data/lib/daru_lite/vector/setable.rb +47 -0
- data/lib/daru_lite/vector/sortable.rb +113 -0
- data/lib/daru_lite/vector.rb +36 -932
- data/lib/daru_lite/version.rb +1 -1
- data/spec/data_frame/aggregatable_example.rb +65 -0
- data/spec/data_frame/buildable_example.rb +109 -0
- data/spec/data_frame/calculatable_example.rb +135 -0
- data/spec/data_frame/convertible_example.rb +180 -0
- data/spec/data_frame/duplicatable_example.rb +111 -0
- data/spec/data_frame/fetchable_example.rb +476 -0
- data/spec/data_frame/filterable_example.rb +250 -0
- data/spec/data_frame/indexable_example.rb +221 -0
- data/spec/data_frame/iterable_example.rb +465 -0
- data/spec/data_frame/joinable_example.rb +106 -0
- data/spec/data_frame/missable_example.rb +47 -0
- data/spec/data_frame/pivotable_example.rb +297 -0
- data/spec/data_frame/queryable_example.rb +92 -0
- data/spec/data_frame/setable_example.rb +482 -0
- data/spec/data_frame/sortable_example.rb +350 -0
- data/spec/dataframe_spec.rb +181 -3289
- data/spec/index/index_spec.rb +8 -0
- data/spec/vector/aggregatable_example.rb +27 -0
- data/spec/vector/calculatable_example.rb +82 -0
- data/spec/vector/convertible_example.rb +126 -0
- data/spec/vector/duplicatable_example.rb +48 -0
- data/spec/vector/fetchable_example.rb +463 -0
- data/spec/vector/filterable_example.rb +165 -0
- data/spec/vector/indexable_example.rb +201 -0
- data/spec/vector/iterable_example.rb +111 -0
- data/spec/vector/joinable_example.rb +25 -0
- data/spec/vector/missable_example.rb +88 -0
- data/spec/vector/queryable_example.rb +91 -0
- data/spec/vector/setable_example.rb +300 -0
- data/spec/vector/sortable_example.rb +242 -0
- data/spec/vector_spec.rb +111 -1805
- metadata +86 -2
@@ -0,0 +1,482 @@
|
|
1
|
+
shared_examples_for 'a setable DataFrame' do
|
2
|
+
describe "#row.set_at" do
|
3
|
+
let(:df) do
|
4
|
+
DaruLite::DataFrame.new({
|
5
|
+
a: 1..3,
|
6
|
+
b: 'a'..'c'
|
7
|
+
})
|
8
|
+
end
|
9
|
+
|
10
|
+
context "single position" do
|
11
|
+
subject { df }
|
12
|
+
before { df.row.set_at [1], ['x', 'y'] }
|
13
|
+
|
14
|
+
its(:size) { is_expected.to eq 3 }
|
15
|
+
its(:'a.to_a') { is_expected.to eq [1, 'x', 3] }
|
16
|
+
its(:'b.to_a') { is_expected.to eq ['a', 'y', 'c'] }
|
17
|
+
end
|
18
|
+
|
19
|
+
context "multiple position" do
|
20
|
+
subject { df }
|
21
|
+
before { df.row.set_at [0, 2], ['x', 'y'] }
|
22
|
+
|
23
|
+
its(:size) { is_expected.to eq 3 }
|
24
|
+
its(:'a.to_a') { is_expected.to eq ['x', 2, 'x'] }
|
25
|
+
its(:'b.to_a') { is_expected.to eq ['y', 'b', 'y'] }
|
26
|
+
end
|
27
|
+
|
28
|
+
context "invalid position" do
|
29
|
+
it { expect { df.row.set_at [3], ['x', 'y'] }.to raise_error IndexError }
|
30
|
+
end
|
31
|
+
|
32
|
+
context "invalid positions" do
|
33
|
+
it { expect { df.row.set_at [2, 3], ['x', 'y'] }.to raise_error IndexError }
|
34
|
+
end
|
35
|
+
|
36
|
+
context "incorrect size" do
|
37
|
+
it { expect { df.row.set_at [1], ['x', 'y', 'z'] }.to raise_error SizeError }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "#set_at" do
|
42
|
+
let(:df) do
|
43
|
+
DaruLite::DataFrame.new({
|
44
|
+
1 => 1..3,
|
45
|
+
a: 'a'..'c',
|
46
|
+
b: 11..13
|
47
|
+
})
|
48
|
+
end
|
49
|
+
|
50
|
+
context "single position" do
|
51
|
+
subject { df }
|
52
|
+
before { df.set_at [1], ['x', 'y', 'z'] }
|
53
|
+
|
54
|
+
its(:shape) { is_expected.to eq [3, 3] }
|
55
|
+
it { expect(df[1].to_a).to eq [1, 2, 3] }
|
56
|
+
its(:'a.to_a') { is_expected.to eq ['x', 'y', 'z'] }
|
57
|
+
its(:'b.to_a') { is_expected.to eq [11, 12, 13] }
|
58
|
+
end
|
59
|
+
|
60
|
+
context "multiple position" do
|
61
|
+
subject { df }
|
62
|
+
before { df.set_at [1, 2], ['x', 'y', 'z'] }
|
63
|
+
|
64
|
+
its(:shape) { is_expected.to eq [3, 3] }
|
65
|
+
it { expect(df[1].to_a).to eq [1, 2, 3] }
|
66
|
+
its(:'a.to_a') { is_expected.to eq ['x', 'y', 'z'] }
|
67
|
+
its(:'b.to_a') { is_expected.to eq ['x', 'y', 'z'] }
|
68
|
+
end
|
69
|
+
|
70
|
+
context "invalid position" do
|
71
|
+
it { expect { df.set_at [3], ['x', 'y', 'z'] }.to raise_error IndexError }
|
72
|
+
end
|
73
|
+
|
74
|
+
context "invalid positions" do
|
75
|
+
it { expect { df.set_at [2, 3], ['x', 'y', 'z'] }.to raise_error IndexError }
|
76
|
+
end
|
77
|
+
|
78
|
+
context "incorrect size" do
|
79
|
+
it { expect { df.set_at [1], ['x', 'y'] }.to raise_error SizeError }
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "#[]=" do
|
84
|
+
context DaruLite::Index do
|
85
|
+
let(:df) do
|
86
|
+
DaruLite::DataFrame.new(
|
87
|
+
{
|
88
|
+
b: [11,12,13,14,15],
|
89
|
+
a: [1,2,3,4,5],
|
90
|
+
c: [11,22,33,44,55]
|
91
|
+
},
|
92
|
+
order: [:a, :b, :c],
|
93
|
+
index: [:one, :two, :three, :four, :five]
|
94
|
+
)
|
95
|
+
end
|
96
|
+
|
97
|
+
it "assigns directly with the []= operator" do
|
98
|
+
df[:a] = [100,200,300,400,500]
|
99
|
+
expect(df).to eq(DaruLite::DataFrame.new({
|
100
|
+
b: [11,12,13,14,15],
|
101
|
+
a: [100,200,300,400,500],
|
102
|
+
c: [11,22,33,44,55]}, order: [:a, :b, :c],
|
103
|
+
index: [:one, :two, :three, :four, :five]))
|
104
|
+
end
|
105
|
+
|
106
|
+
it "assigns new vector with default length if given just a value" do
|
107
|
+
df[:d] = 1.0
|
108
|
+
expect(df[:d]).to eq(DaruLite::Vector.new([1.0, 1.0, 1.0, 1.0, 1.0],
|
109
|
+
index: [:one, :two, :three, :four, :five], name: :d))
|
110
|
+
end
|
111
|
+
|
112
|
+
it "updates vector with default length if given just a value" do
|
113
|
+
df[:c] = 1.0
|
114
|
+
expect(df[:c]).to eq(DaruLite::Vector.new([1.0, 1.0, 1.0, 1.0, 1.0],
|
115
|
+
index: [:one, :two, :three, :four, :five], name: :c))
|
116
|
+
end
|
117
|
+
|
118
|
+
it "appends an Array as a DaruLite::Vector" do
|
119
|
+
df[:d] = [69,99,108,85,49]
|
120
|
+
|
121
|
+
expect(df.d.class).to eq(DaruLite::Vector)
|
122
|
+
end
|
123
|
+
|
124
|
+
it "appends an arbitrary enumerable as a DaruLite::Vector" do
|
125
|
+
df[:d] = Set.new([69,99,108,85,49])
|
126
|
+
|
127
|
+
expect(df[:d]).to eq(DaruLite::Vector.new([69, 99, 108, 85, 49],
|
128
|
+
index: [:one, :two, :three, :four, :five], name: :c))
|
129
|
+
end
|
130
|
+
|
131
|
+
it "replaces an already present vector" do
|
132
|
+
df[:a] = [69,99,108,85,49].dv(nil, [:one, :two, :three, :four, :five])
|
133
|
+
|
134
|
+
expect(df.a).to eq([69,99,108,85,49].dv(nil, [:one, :two, :three, :four, :five]))
|
135
|
+
end
|
136
|
+
|
137
|
+
it "appends a new vector to the DataFrame" do
|
138
|
+
df[:woo] = [69,99,108,85,49].dv(nil, [:one, :two, :three, :four, :five])
|
139
|
+
|
140
|
+
expect(df.vectors).to eq([:a, :b, :c, :woo].to_index)
|
141
|
+
end
|
142
|
+
|
143
|
+
it "creates an index for the new vector if not specified" do
|
144
|
+
df[:woo] = [69,99,108,85,49]
|
145
|
+
|
146
|
+
expect(df.woo.index).to eq([:one, :two, :three, :four, :five].to_index)
|
147
|
+
end
|
148
|
+
|
149
|
+
it "matches index of vector to be inserted with the DataFrame index" do
|
150
|
+
df[:shankar] = [69,99,108,85,49].dv(:shankar, [:two, :one, :three, :five, :four])
|
151
|
+
|
152
|
+
expect(df.shankar).to eq([99,69,108,49,85].dv(:shankar,
|
153
|
+
[:one, :two, :three, :four, :five]))
|
154
|
+
end
|
155
|
+
|
156
|
+
it "matches index of vector to be inserted, inserting nils where no match found" do
|
157
|
+
df[:shankar] = [1,2,3].dv(:shankar, [:one, :james, :hetfield])
|
158
|
+
|
159
|
+
expect(df.shankar).to eq([1,nil,nil,nil,nil].dv(:shankar, [:one, :two, :three, :four, :five]))
|
160
|
+
end
|
161
|
+
|
162
|
+
it "raises error for Array assignment of wrong length" do
|
163
|
+
expect{
|
164
|
+
df[:shiva] = [1,2,3]
|
165
|
+
}.to raise_error
|
166
|
+
end
|
167
|
+
|
168
|
+
it "assigns correct name given empty dataframe" do
|
169
|
+
df_empty = DaruLite::DataFrame.new({})
|
170
|
+
df_empty[:a] = 1..5
|
171
|
+
df_empty[:b] = 1..5
|
172
|
+
|
173
|
+
expect(df_empty[:a].name).to equal(:a)
|
174
|
+
expect(df_empty[:b].name).to equal(:b)
|
175
|
+
end
|
176
|
+
|
177
|
+
it "appends multiple vectors at a time" do
|
178
|
+
# TODO
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
context DaruLite::MultiIndex do
|
183
|
+
it "raises error when incomplete index specified but index is absent" do
|
184
|
+
expect {
|
185
|
+
df_mi[:d] = [100,200,300,400,100,200,300,400,100,200,300,400]
|
186
|
+
}.to raise_error
|
187
|
+
end
|
188
|
+
|
189
|
+
it "assigns all sub-indexes when a top level index is specified" do
|
190
|
+
df_mi[:a] = [100,200,300,400,100,200,300,400,100,200,300,400]
|
191
|
+
|
192
|
+
expect(df_mi).to eq(DaruLite::DataFrame.new([
|
193
|
+
[100,200,300,400,100,200,300,400,100,200,300,400],
|
194
|
+
[100,200,300,400,100,200,300,400,100,200,300,400],
|
195
|
+
vector_arry1,
|
196
|
+
vector_arry2], index: multi_index, order: order_mi))
|
197
|
+
end
|
198
|
+
|
199
|
+
it "creates a new vector when full index specfied" do
|
200
|
+
order = DaruLite::MultiIndex.from_tuples([
|
201
|
+
[:a,:one,:bar],
|
202
|
+
[:a,:two,:baz],
|
203
|
+
[:b,:two,:foo],
|
204
|
+
[:b,:one,:foo],
|
205
|
+
[:c,:one,:bar]])
|
206
|
+
answer = DaruLite::DataFrame.new([
|
207
|
+
vector_arry1,
|
208
|
+
vector_arry2,
|
209
|
+
vector_arry1,
|
210
|
+
vector_arry2,
|
211
|
+
[100,200,300,400,100,200,300,400,100,200,300,400]
|
212
|
+
], index: multi_index, order: order)
|
213
|
+
df_mi[:c,:one,:bar] = [100,200,300,400,100,200,300,400,100,200,300,400]
|
214
|
+
|
215
|
+
expect(df_mi).to eq(answer)
|
216
|
+
end
|
217
|
+
|
218
|
+
it "assigns correct name given empty dataframe" do
|
219
|
+
df_empty = DaruLite::DataFrame.new([], index: multi_index, order: order_mi)
|
220
|
+
df_empty[:c, :one, :bar] = 1..12
|
221
|
+
|
222
|
+
expect(df_empty[:c, :one, :bar].name).to eq "conebar"
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
describe "#add_row" do
|
228
|
+
subject(:data_frame) {
|
229
|
+
DaruLite::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5],
|
230
|
+
c: [11,22,33,44,55]}, order: [:a, :b, :c],
|
231
|
+
index: [:one, :two, :three, :four, :five])
|
232
|
+
}
|
233
|
+
context 'named' do
|
234
|
+
before {
|
235
|
+
data_frame.add_row [100,200,300], :six
|
236
|
+
}
|
237
|
+
|
238
|
+
it { is_expected.to eq(DaruLite::DataFrame.new({
|
239
|
+
a: [1,2,3,4,5,100],
|
240
|
+
b: [11,12,13,14,15,200],
|
241
|
+
c: [11,22,33,44,55,300]}, order: [:a, :b, :c],
|
242
|
+
index: [:one, :two, :three, :four, :five, :six]))
|
243
|
+
}
|
244
|
+
end
|
245
|
+
|
246
|
+
context 'unnamed' do
|
247
|
+
before {
|
248
|
+
data_frame.add_row [100,200,300]
|
249
|
+
}
|
250
|
+
|
251
|
+
it { is_expected.to eq(DaruLite::DataFrame.new({
|
252
|
+
a: [1,2,3,4,5,100],
|
253
|
+
b: [11,12,13,14,15,200],
|
254
|
+
c: [11,22,33,44,55,300]}, order: [:a, :b, :c],
|
255
|
+
index: [:one, :two, :three, :four, :five, 5]))
|
256
|
+
}
|
257
|
+
end
|
258
|
+
|
259
|
+
context 'with mulitiindex DF' do
|
260
|
+
subject(:data_frame) do
|
261
|
+
DaruLite::DataFrame.new({b: [11,12,13], a: [1,2,3],
|
262
|
+
c: [11,22,33]}, order: [:a, :b, :c],
|
263
|
+
index: DaruLite::MultiIndex.from_tuples([[:one, :two], [:one, :three], [:two, :four]]))
|
264
|
+
end
|
265
|
+
|
266
|
+
before { data_frame.add_row [100,200,300], [:two, :five] }
|
267
|
+
|
268
|
+
it { is_expected.to eq(DaruLite::DataFrame.new({
|
269
|
+
b: [11,12,13,200], a: [1,2,3,100],
|
270
|
+
c: [11,22,33,300]}, order: [:a, :b, :c],
|
271
|
+
index: DaruLite::MultiIndex.from_tuples([[:one, :two], [:one, :three], [:two, :four], [:two, :five]])))
|
272
|
+
}
|
273
|
+
end
|
274
|
+
|
275
|
+
it "allows adding rows after making empty DF by specfying only order" do
|
276
|
+
df = DaruLite::DataFrame.new({}, order: [:a, :b, :c])
|
277
|
+
df.add_row [1,2,3]
|
278
|
+
df.add_row [5,6,7]
|
279
|
+
|
280
|
+
expect(df[:a]).to eq(DaruLite::Vector.new([1,5]))
|
281
|
+
expect(df[:b]).to eq(DaruLite::Vector.new([2,6]))
|
282
|
+
expect(df[:c]).to eq(DaruLite::Vector.new([3,7]))
|
283
|
+
expect(df.index).to eq(DaruLite::Index.new([0,1]))
|
284
|
+
end
|
285
|
+
end
|
286
|
+
|
287
|
+
describe '#add_vector' do
|
288
|
+
subject(:data_frame) do
|
289
|
+
DaruLite::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5],
|
290
|
+
c: [11,22,33,44,55]}, order: [:a, :b, :c],
|
291
|
+
index: [:one, :two, :three, :four, :five])
|
292
|
+
end
|
293
|
+
|
294
|
+
before { data_frame.add_vector :a, [100,200,300,400,500] }
|
295
|
+
|
296
|
+
it { is_expected.to eq(DaruLite::DataFrame.new({
|
297
|
+
b: [11,12,13,14,15],
|
298
|
+
a: [100,200,300,400,500],
|
299
|
+
c: [11,22,33,44,55]}, order: [:a, :b, :c],
|
300
|
+
index: [:one, :two, :three, :four, :five]))
|
301
|
+
}
|
302
|
+
end
|
303
|
+
|
304
|
+
describe "#insert_vector" do
|
305
|
+
subject(:data_frame) do
|
306
|
+
DaruLite::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5],
|
307
|
+
c: [11,22,33,44,55]}, order: [:a, :b, :c],
|
308
|
+
index: [:one, :two, :three, :four, :five])
|
309
|
+
end
|
310
|
+
|
311
|
+
it "insert a new vector at the desired slot" do
|
312
|
+
df = DaruLite::DataFrame.new({
|
313
|
+
a: [1,2,3,4,5],
|
314
|
+
d: [710, 720, 730, 740, 750],
|
315
|
+
b: [11, 12, 13, 14, 15],
|
316
|
+
c: [11,22,33,44,55]}, order: [:a, :d, :b, :c],
|
317
|
+
index: [:one, :two, :three, :four, :five]
|
318
|
+
)
|
319
|
+
data_frame.insert_vector 1, :d, [710, 720, 730, 740, 750]
|
320
|
+
expect(subject).to eq df
|
321
|
+
end
|
322
|
+
|
323
|
+
it "raises error for data array being too big" do
|
324
|
+
expect {
|
325
|
+
source = (1..8).to_a
|
326
|
+
data_frame.insert_vector 1, :d, source
|
327
|
+
}.to raise_error(IndexError)
|
328
|
+
end
|
329
|
+
|
330
|
+
it "raises error for invalid index value" do
|
331
|
+
expect {
|
332
|
+
source = (1..5).to_a
|
333
|
+
data_frame.insert_vector 4, :d, source
|
334
|
+
}.to raise_error(ArgumentError)
|
335
|
+
end
|
336
|
+
|
337
|
+
it "raises error for invalid source type" do
|
338
|
+
expect {
|
339
|
+
source = 14
|
340
|
+
data_frame.insert_vector 3, :d, source
|
341
|
+
}.to raise_error(ArgumentError)
|
342
|
+
end
|
343
|
+
end
|
344
|
+
|
345
|
+
describe "#row[]=" do
|
346
|
+
context DaruLite::Index do
|
347
|
+
let(:df) do
|
348
|
+
DaruLite::DataFrame.new(
|
349
|
+
{
|
350
|
+
b: [11,12,13,14,15],
|
351
|
+
a: [1,2,3,4,5],
|
352
|
+
c: [11,22,33,44,55]
|
353
|
+
},
|
354
|
+
order: [:a, :b, :c],
|
355
|
+
index: [:one, :two, :three, :four, :five]
|
356
|
+
)
|
357
|
+
end
|
358
|
+
|
359
|
+
it "assigns specified row when Array" do
|
360
|
+
df.row[:one] = [49, 99, 59]
|
361
|
+
|
362
|
+
expect(df.row[:one]) .to eq([49, 99, 59].dv(:one, [:a, :b, :c]))
|
363
|
+
expect(df.row[:one].index).to eq([:a, :b, :c].to_index)
|
364
|
+
expect(df.row[:one].name) .to eq(:one)
|
365
|
+
end
|
366
|
+
|
367
|
+
it "assigns specified row when DV" do
|
368
|
+
df.row[:one] = [49, 99, 59].dv(nil, [:a, :b, :c])
|
369
|
+
|
370
|
+
expect(df.row[:one]).to eq([49, 99, 59].dv(:one, [:a, :b, :c]))
|
371
|
+
end
|
372
|
+
|
373
|
+
it "assigns correct elements when Vector of different index" do
|
374
|
+
df.row[:one] = DaruLite::Vector.new([44,62,11], index: [:b,:f,:a])
|
375
|
+
|
376
|
+
expect(df.row[:one]).to eq(DaruLite::Vector.new([11,44,nil], index: [:a,:b,:c]))
|
377
|
+
end
|
378
|
+
|
379
|
+
it "creates a new row from an Array" do
|
380
|
+
df.row[:patekar] = [9,2,11]
|
381
|
+
|
382
|
+
expect(df.row[:patekar]).to eq([9,2,11].dv(:patekar, [:a, :b, :c]))
|
383
|
+
end
|
384
|
+
|
385
|
+
it "creates a new row from a DV" do
|
386
|
+
df.row[:patekar] = [9,2,11].dv(nil, [:a, :b, :c])
|
387
|
+
|
388
|
+
expect(df.row[:patekar]).to eq([9,2,11].dv(:patekar, [:a, :b, :c]))
|
389
|
+
end
|
390
|
+
|
391
|
+
it "creates a new row from numeric row index and named DV" do
|
392
|
+
df.row[2] = [9,2,11].dv(nil, [:a, :b, :c])
|
393
|
+
|
394
|
+
expect(df.row[2]).to eq([9,2,11].dv(nil, [:a, :b, :c]))
|
395
|
+
end
|
396
|
+
|
397
|
+
it "correctly aligns assigned DV by index" do
|
398
|
+
df.row[:two] = [9,2,11].dv(nil, [:b, :a, :c])
|
399
|
+
|
400
|
+
expect(df.row[:two]).to eq([2,9,11].dv(:two, [:a, :b, :c]))
|
401
|
+
end
|
402
|
+
|
403
|
+
it "correctlu aligns assinged DV by index for new rows" do
|
404
|
+
df.row[:latest] = DaruLite::Vector.new([2,3,1], index: [:b,:c,:a])
|
405
|
+
|
406
|
+
expect(df.row[:latest]).to eq(DaruLite::Vector.new([1,2,3], index: [:a,:b,:c]))
|
407
|
+
end
|
408
|
+
|
409
|
+
it "inserts nils for indexes that dont exist in the DataFrame" do
|
410
|
+
df.row[:two] = [49, 99, 59].dv(nil, [:oo, :aah, :gaah])
|
411
|
+
|
412
|
+
expect(df.row[:two]).to eq([nil,nil,nil].dv(nil, [:a, :b, :c]))
|
413
|
+
end
|
414
|
+
|
415
|
+
it "correctly inserts row of a different length by matching indexes" do
|
416
|
+
df.row[:four] = [5,4,3,2,1,3].dv(nil, [:you, :have, :a, :big, :appetite, :spock])
|
417
|
+
|
418
|
+
expect(df.row[:four]).to eq([3,nil,nil].dv(:four, [:a, :b, :c]))
|
419
|
+
end
|
420
|
+
|
421
|
+
it "raises error for row insertion by Array of wrong length" do
|
422
|
+
expect{
|
423
|
+
df.row[:one] = [1,2,3,4,5,6,7]
|
424
|
+
}.to raise_error
|
425
|
+
end
|
426
|
+
end
|
427
|
+
|
428
|
+
context DaruLite::MultiIndex do
|
429
|
+
pending
|
430
|
+
# TO DO
|
431
|
+
end
|
432
|
+
|
433
|
+
context DaruLite::CategoricalIndex do
|
434
|
+
let(:idx) { DaruLite::CategoricalIndex.new [:a, 1, :a, 1, :c] }
|
435
|
+
let(:df) do
|
436
|
+
DaruLite::DataFrame.new({
|
437
|
+
a: 'a'..'e',
|
438
|
+
b: 1..5
|
439
|
+
}, index: idx)
|
440
|
+
end
|
441
|
+
|
442
|
+
context "modify exiting row" do
|
443
|
+
context "single category" do
|
444
|
+
subject { df }
|
445
|
+
before { df.row[:a] = ['x', 'y'] }
|
446
|
+
|
447
|
+
it { is_expected.to be_a DaruLite::DataFrame }
|
448
|
+
its(:index) { is_expected.to eq idx }
|
449
|
+
its(:vectors) { is_expected.to eq DaruLite::Index.new [:a, :b] }
|
450
|
+
its(:'a.to_a') { is_expected.to eq ['x', 'b', 'x', 'd', 'e'] }
|
451
|
+
its(:'b.to_a') { is_expected.to eq ['y', 2, 'y', 4, 5] }
|
452
|
+
end
|
453
|
+
|
454
|
+
context "multiple categories" do
|
455
|
+
subject { df }
|
456
|
+
before { df.row[:a, 1] = ['x', 'y'] }
|
457
|
+
|
458
|
+
it { is_expected.to be_a DaruLite::DataFrame }
|
459
|
+
its(:index) { is_expected.to eq idx }
|
460
|
+
its(:vectors) { is_expected.to eq DaruLite::Index.new [:a, :b] }
|
461
|
+
its(:'a.to_a') { is_expected.to eq ['x', 'x', 'x', 'x', 'e'] }
|
462
|
+
its(:'b.to_a') { is_expected.to eq ['y', 'y', 'y', 'y', 5] }
|
463
|
+
end
|
464
|
+
|
465
|
+
context "positional index" do
|
466
|
+
subject { df }
|
467
|
+
before { df.row[0, 2] = ['x', 'y'] }
|
468
|
+
|
469
|
+
it { is_expected.to be_a DaruLite::DataFrame }
|
470
|
+
its(:index) { is_expected.to eq idx }
|
471
|
+
its(:vectors) { is_expected.to eq DaruLite::Index.new [:a, :b] }
|
472
|
+
its(:'a.to_a') { is_expected.to eq ['x', 'b', 'x', 'd', 'e'] }
|
473
|
+
its(:'b.to_a') { is_expected.to eq ['y', 2, 'y', 4, 5] }
|
474
|
+
end
|
475
|
+
end
|
476
|
+
|
477
|
+
context "add new row" do
|
478
|
+
# TODO
|
479
|
+
end
|
480
|
+
end
|
481
|
+
end
|
482
|
+
end
|