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