daru 0.0.3 → 0.0.3.1
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/README.md +269 -13
- data/lib/daru/accessors/array_wrapper.rb +8 -0
- data/lib/daru/accessors/dataframe_by_row.rb +17 -0
- data/lib/daru/accessors/dataframe_by_vector.rb +17 -0
- data/lib/daru/accessors/mdarray_wrapper.rb +9 -0
- data/lib/daru/accessors/nmatrix_wrapper.rb +9 -0
- data/lib/daru/dataframe.rb +59 -20
- data/lib/daru/{io.rb → io/io.rb} +1 -1
- data/lib/daru/math/arithmetic/dataframe.rb +28 -0
- data/lib/daru/math/arithmetic/vector.rb +71 -0
- data/lib/daru/math/statistics/dataframe.rb +10 -0
- data/lib/daru/math/statistics/vector.rb +9 -0
- data/lib/daru/monkeys.rb +5 -5
- data/lib/daru/vector.rb +20 -9
- data/lib/version.rb +1 -1
- data/spec/dataframe_spec.rb +73 -44
- data/spec/{io_spec.rb → io/io_spec.rb} +0 -0
- data/spec/math/arithmetic/dataframe_spec.rb +5 -0
- data/spec/math/arithmetic/vector_spec.rb +51 -0
- data/spec/math/statistics/dataframe_spec.rb +5 -0
- data/spec/math/statistics/vector_spec.rb +12 -0
- data/spec/vector_spec.rb +19 -19
- metadata +22 -7
- data/lib/daru/dataframe_by_row.rb +0 -15
- data/lib/daru/dataframe_by_vector.rb +0 -15
data/lib/daru/{io.rb → io/io.rb}
RENAMED
@@ -0,0 +1,28 @@
|
|
1
|
+
module Daru
|
2
|
+
module Math
|
3
|
+
module Arithmetic
|
4
|
+
module DataFrame
|
5
|
+
|
6
|
+
def + other
|
7
|
+
|
8
|
+
end
|
9
|
+
|
10
|
+
def - other
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
def * other
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
def / other
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
def % other
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module Daru
|
2
|
+
module Math
|
3
|
+
module Arithmetic
|
4
|
+
module Vector
|
5
|
+
def + other
|
6
|
+
case other
|
7
|
+
when Daru::Vector
|
8
|
+
v2v_binary :+, other
|
9
|
+
else
|
10
|
+
Daru::Vector.new self.map { |e| e + other }, name: @name, index: @index
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def - other
|
15
|
+
case other
|
16
|
+
when Daru::Vector
|
17
|
+
v2v_binary :-, other
|
18
|
+
else
|
19
|
+
Daru::Vector.new self.map { |e| e - other }, name: @name, index: @index
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def * other
|
24
|
+
case other
|
25
|
+
when Daru::Vector
|
26
|
+
v2v_binary :*, other
|
27
|
+
else
|
28
|
+
Daru::Vector.new self.map { |e| e * other }, name: @name, index: @index
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def / other
|
33
|
+
case other
|
34
|
+
when Daru::Vector
|
35
|
+
v2v_binary :/, other
|
36
|
+
else
|
37
|
+
Daru::Vector.new self.map { |e| e / other }, name: @name, index: @index
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def % other
|
42
|
+
case other
|
43
|
+
when Daru::Vector
|
44
|
+
v2v_binary :%, other
|
45
|
+
else
|
46
|
+
Daru::Vector.new self.map { |e| e % other }, name: @name, index: @index
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def v2v_binary operation, other
|
53
|
+
common_idxs = []
|
54
|
+
elements = []
|
55
|
+
|
56
|
+
@index.each do |idx|
|
57
|
+
this = self[idx]
|
58
|
+
that = other[idx]
|
59
|
+
|
60
|
+
if this and that
|
61
|
+
elements << this.send(operation ,that)
|
62
|
+
common_idxs << idx
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
Daru::Vector.new(elements, name: @name, index: common_idxs)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/lib/daru/monkeys.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
class Array
|
2
2
|
def daru_vector name=nil, index=nil
|
3
|
-
Daru::Vector.new
|
3
|
+
Daru::Vector.new self, name: name, index: index
|
4
4
|
end
|
5
5
|
|
6
6
|
alias_method :dv, :daru_vector
|
@@ -12,7 +12,7 @@ end
|
|
12
12
|
|
13
13
|
class Range
|
14
14
|
def daru_vector name=nil, index=nil
|
15
|
-
Daru::Vector.new
|
15
|
+
Daru::Vector.new self, name: name, index: index
|
16
16
|
end
|
17
17
|
|
18
18
|
alias_method :dv, :daru_vector
|
@@ -24,7 +24,7 @@ end
|
|
24
24
|
|
25
25
|
class Hash
|
26
26
|
def daru_vector index=nil
|
27
|
-
Daru::Vector.new self.values[0], self.keys[0], index
|
27
|
+
Daru::Vector.new self.values[0], name: self.keys[0], index: index
|
28
28
|
end
|
29
29
|
|
30
30
|
alias_method :dv, :daru_vector
|
@@ -32,7 +32,7 @@ end
|
|
32
32
|
|
33
33
|
class NMatrix
|
34
34
|
def daru_vector name=nil, index=nil
|
35
|
-
Daru::Vector.new
|
35
|
+
Daru::Vector.new self, name: name, index: index
|
36
36
|
end
|
37
37
|
|
38
38
|
alias_method :dv, :daru_vector
|
@@ -40,7 +40,7 @@ end
|
|
40
40
|
|
41
41
|
class MDArray
|
42
42
|
def daru_vector name=nil, index=nil
|
43
|
-
Daru::Vector.new
|
43
|
+
Daru::Vector.new self, name: name, index: index
|
44
44
|
end
|
45
45
|
|
46
46
|
alias_method :dv, :daru_vector
|
data/lib/daru/vector.rb
CHANGED
@@ -1,5 +1,10 @@
|
|
1
|
+
require_relative 'math/arithmetic/vector.rb'
|
2
|
+
require_relative 'math/statistics/vector.rb'
|
3
|
+
|
1
4
|
module Daru
|
2
5
|
class Vector
|
6
|
+
include Daru::Math::Arithmetic::Vector
|
7
|
+
include Daru::Math::Statistics::Vector
|
3
8
|
include Enumerable
|
4
9
|
|
5
10
|
def each(&block)
|
@@ -11,10 +16,10 @@ module Daru
|
|
11
16
|
attr_reader :size
|
12
17
|
|
13
18
|
# Pass it name, source and index
|
14
|
-
def initialize
|
15
|
-
|
16
|
-
|
17
|
-
index =
|
19
|
+
def initialize source, opts={}
|
20
|
+
source = source || []
|
21
|
+
name = opts[:name]
|
22
|
+
index = opts[:index]
|
18
23
|
|
19
24
|
set_name name
|
20
25
|
|
@@ -50,12 +55,13 @@ module Daru
|
|
50
55
|
elsif index.is_a?(Numeric)
|
51
56
|
@vector[index]
|
52
57
|
else
|
53
|
-
|
58
|
+
return nil
|
54
59
|
end
|
55
60
|
else
|
56
61
|
indexes.unshift index
|
57
62
|
|
58
|
-
Daru::Vector.new
|
63
|
+
Daru::Vector.new indexes.map { |index| @vector[@index[index]] },name: @name,
|
64
|
+
index: indexes
|
59
65
|
end
|
60
66
|
end
|
61
67
|
|
@@ -130,6 +136,10 @@ module Daru
|
|
130
136
|
end
|
131
137
|
end
|
132
138
|
|
139
|
+
def to_a
|
140
|
+
@vector.to_a
|
141
|
+
end
|
142
|
+
|
133
143
|
def to_json *args
|
134
144
|
self.to_hash.to_json
|
135
145
|
end
|
@@ -158,7 +168,8 @@ module Daru
|
|
158
168
|
end
|
159
169
|
|
160
170
|
def inspect spacing=10, threshold=15
|
161
|
-
longest = [@
|
171
|
+
longest = [@name.to_s.size,
|
172
|
+
@index.to_a.map(&:to_s).map(&:size).max,
|
162
173
|
@vector .map(&:to_s).map(&:size).max].max
|
163
174
|
|
164
175
|
content = ""
|
@@ -170,7 +181,7 @@ module Daru
|
|
170
181
|
|
171
182
|
content += sprintf formatter, "", name
|
172
183
|
@index.each_with_index do |index, num|
|
173
|
-
content += sprintf formatter, index.to_s, self[index]
|
184
|
+
content += sprintf formatter, index.to_s, (self[index] || 'nil').to_s
|
174
185
|
|
175
186
|
if num > threshold
|
176
187
|
content += sprintf formatter, '...', '...'
|
@@ -194,7 +205,7 @@ module Daru
|
|
194
205
|
end
|
195
206
|
|
196
207
|
def dup
|
197
|
-
Daru::Vector.new @
|
208
|
+
Daru::Vector.new @vector.dup, name: @name, index: @index.dup
|
198
209
|
end
|
199
210
|
|
200
211
|
def daru_vector *name
|
data/lib/version.rb
CHANGED
data/spec/dataframe_spec.rb
CHANGED
@@ -3,12 +3,14 @@ require 'spec_helper.rb'
|
|
3
3
|
describe Daru::DataFrame do
|
4
4
|
before :each do
|
5
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]},
|
6
|
+
c: [11,22,33,44,55]},
|
7
|
+
vectors: [:a, :b, :c],
|
8
|
+
index: [:one, :two, :three, :four, :five])
|
7
9
|
end
|
8
10
|
|
9
11
|
context "#initialize" do
|
10
12
|
it "initializes an empty DataFrame" do
|
11
|
-
df = Daru::DataFrame.new({}, [:a, :b])
|
13
|
+
df = Daru::DataFrame.new({}, vectors: [:a, :b])
|
12
14
|
|
13
15
|
expect(df.vectors).to eq(Daru::Index.new [:a, :b])
|
14
16
|
expect(df.a.class).to eq(Daru::Vector)
|
@@ -16,8 +18,8 @@ describe Daru::DataFrame do
|
|
16
18
|
end
|
17
19
|
|
18
20
|
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
|
+
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])
|
21
23
|
|
22
24
|
expect(df.index) .to eq(Daru::Index.new [:one, :two, :three, :four, :five])
|
23
25
|
expect(df.vectors).to eq(Daru::Index.new [:a, :b])
|
@@ -27,8 +29,8 @@ describe Daru::DataFrame do
|
|
27
29
|
|
28
30
|
it "initializes from a Hash of Vectors", :focus => true do
|
29
31
|
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
|
+
a: [1,2,3,4,5].dv(:a, [:one, :two, :three, :four, :five])}, vectors: [:a, :b],
|
33
|
+
index: [:one, :two, :three, :four, :five])
|
32
34
|
|
33
35
|
expect(df.index) .to eq(Daru::Index.new [:one, :two, :three, :four, :five])
|
34
36
|
expect(df.vectors).to eq(Daru::Index.new [:a, :b])
|
@@ -38,7 +40,7 @@ describe Daru::DataFrame do
|
|
38
40
|
|
39
41
|
it "initializes from an Array of Hashes" do
|
40
42
|
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])
|
43
|
+
{a: 4, b: 14}, {a: 5, b: 15}], vectors: [:b, :a], index: [:one, :two, :three, :four, :five])
|
42
44
|
|
43
45
|
expect(df.index) .to eq(Daru::Index.new [:one, :two, :three, :four, :five])
|
44
46
|
expect(df.vectors).to eq(Daru::Index.new [:b, :a])
|
@@ -50,10 +52,11 @@ describe Daru::DataFrame do
|
|
50
52
|
rows = Daru::Index.new [:one, :two, :three, :four, :five]
|
51
53
|
cols = Daru::Index.new [:a, :b]
|
52
54
|
|
53
|
-
df = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5]}, cols,
|
55
|
+
df = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5]}, vectors: cols,
|
56
|
+
index: rows)
|
54
57
|
|
55
|
-
expect(df.a) .to eq(Daru::Vector.new(
|
56
|
-
expect(df.b) .to eq(Daru::Vector.new(
|
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))
|
57
60
|
expect(df.index) .to eq(Daru::Index.new [:one, :two, :three, :four, :five])
|
58
61
|
expect(df.vectors).to eq(Daru::Index.new [:a, :b])
|
59
62
|
end
|
@@ -70,13 +73,13 @@ describe Daru::DataFrame do
|
|
70
73
|
b: [11,12,13,14,15].dv(:b, [:two, :one, :four, :five, :three]),
|
71
74
|
a: [1,2,3,4,5].dv(:a, [:two,:one,:three, :four, :five])
|
72
75
|
},
|
73
|
-
[:a, :b]
|
76
|
+
vectors: [:a, :b]
|
74
77
|
)
|
75
78
|
|
76
79
|
expect(df).to eq(Daru::DataFrame.new({
|
77
80
|
b: [14,13,12,15,11].dv(:b, [:five, :four, :one, :three, :two]),
|
78
81
|
a: [5,4,2,3,1].dv(:a, [:five, :four, :one, :three, :two])
|
79
|
-
}, [:a, :b])
|
82
|
+
}, vectors: [:a, :b])
|
80
83
|
)
|
81
84
|
end
|
82
85
|
|
@@ -85,14 +88,14 @@ describe Daru::DataFrame do
|
|
85
88
|
b: [11,12,13,14,15].dv(:b, [:two, :one, :four, :five, :three]),
|
86
89
|
a: [1,2,3] .dv(:a, [:two,:one,:three])
|
87
90
|
},
|
88
|
-
[:a, :b]
|
91
|
+
vectors: [:a, :b]
|
89
92
|
)
|
90
93
|
|
91
94
|
expect(df).to eq(Daru::DataFrame.new({
|
92
95
|
b: [14,13,12,15,11].dv(:b, [:five, :four, :one, :three, :two]),
|
93
96
|
a: [nil,nil,2,3,1].dv(:a, [:five, :four, :one, :three, :two])
|
94
97
|
},
|
95
|
-
[:a, :b])
|
98
|
+
vectors: [:a, :b])
|
96
99
|
)
|
97
100
|
end
|
98
101
|
|
@@ -102,14 +105,14 @@ describe Daru::DataFrame do
|
|
102
105
|
a: [1,2,3] .dv(nil, [:one, :two, :three]),
|
103
106
|
c: [11,22,33,44,55] .dv(nil, [:one, :two, :three, :four, :five]),
|
104
107
|
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])
|
108
|
+
}, vectors: [:a, :b, :c, :d], index: [:one, :two, :three, :four, :five, :six])
|
106
109
|
|
107
110
|
expect(df).to eq(Daru::DataFrame.new({
|
108
111
|
b: [11,nil,nil,nil,nil,nil].dv(nil, [:one, :two, :three, :four, :five, :six]),
|
109
112
|
a: [1,2,3,nil,nil,nil] .dv(nil, [:one, :two, :three, :four, :five, :six]),
|
110
113
|
c: [11,22,33,44,55,nil] .dv(nil, [:one, :two, :three, :four, :five, :six]),
|
111
114
|
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])
|
115
|
+
}, vectors: [:a, :b, :c, :d], index: [:one, :two, :three, :four, :five, :six])
|
113
116
|
)
|
114
117
|
end
|
115
118
|
|
@@ -118,21 +121,21 @@ describe Daru::DataFrame do
|
|
118
121
|
b: [11,12,13] .dv(nil, [:one, :bleh, :blah]),
|
119
122
|
a: [1,2,3,4,5].dv(nil, [:one, :two, :booh, :baah, :three]),
|
120
123
|
c: [11,22,33,44,55].dv(nil, [0,1,3,:three, :two])
|
121
|
-
}, [:a, :b, :c], [:one, :two, :three])
|
124
|
+
}, vectors: [:a, :b, :c], index: [:one, :two, :three])
|
122
125
|
|
123
126
|
expect(df).to eq(Daru::DataFrame.new({
|
124
127
|
b: [11,nil,nil].dv(nil, [:one, :two, :three]),
|
125
128
|
a: [1,2,5] .dv(nil, [:one, :two, :three]),
|
126
129
|
c: [nil,55,44] .dv(nil, [:one, :two, :three]),
|
127
130
|
},
|
128
|
-
[:a, :b, :c], [:one, :two, :three]
|
131
|
+
vectors: [:a, :b, :c], index: [:one, :two, :three]
|
129
132
|
)
|
130
133
|
)
|
131
134
|
end
|
132
135
|
|
133
136
|
it "completes incomplete vectors" do
|
134
137
|
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])
|
138
|
+
c: [11,22,33,44,55]}, vectors: [:a, :c])
|
136
139
|
|
137
140
|
expect(df.vectors).to eq([:a,:c,:b].to_index)
|
138
141
|
end
|
@@ -140,14 +143,14 @@ describe Daru::DataFrame do
|
|
140
143
|
it "raises error for incomplete DataFrame index" do
|
141
144
|
expect {
|
142
145
|
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])
|
146
|
+
c: [11,22,33,44,55]}, vectors: [:a, :b, :c], index: [:one, :two, :three])
|
144
147
|
}.to raise_error
|
145
148
|
end
|
146
149
|
|
147
150
|
it "raises error for unequal sized vectors/arrays" do
|
148
151
|
expect {
|
149
152
|
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])
|
153
|
+
c: [11,22,33,44,55]}, vectors: [:a, :b, :c], index: [:one, :two, :three])
|
151
154
|
}.to raise_error
|
152
155
|
end
|
153
156
|
end
|
@@ -155,7 +158,7 @@ describe Daru::DataFrame do
|
|
155
158
|
context "#[:vector]" do
|
156
159
|
before :each do
|
157
160
|
@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])
|
161
|
+
c: [11,22,33,44,55]}, vectors: [:a, :b, :c], index: [:one, :two, :three, :four, :five])
|
159
162
|
end
|
160
163
|
|
161
164
|
it "returns a Vector" do
|
@@ -164,7 +167,7 @@ describe Daru::DataFrame do
|
|
164
167
|
|
165
168
|
it "returns a DataFrame" do
|
166
169
|
temp = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5]},
|
167
|
-
[:a, :b], [:one, :two, :three, :four, :five])
|
170
|
+
vectors: [:a, :b], index: [:one, :two, :three, :four, :five])
|
168
171
|
|
169
172
|
expect(@df[:a, :b, :vector]).to eq(temp)
|
170
173
|
end
|
@@ -177,7 +180,9 @@ describe Daru::DataFrame do
|
|
177
180
|
context "#[:row]" do
|
178
181
|
before :each do
|
179
182
|
@df = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5],
|
180
|
-
c: [11,22,33,44,55]},
|
183
|
+
c: [11,22,33,44,55]},
|
184
|
+
vectors: [:a, :b, :c],
|
185
|
+
index: [:one, :two, :three, :four, :five])
|
181
186
|
end
|
182
187
|
|
183
188
|
it "returns a row with the given index" do
|
@@ -190,7 +195,7 @@ describe Daru::DataFrame do
|
|
190
195
|
|
191
196
|
it "returns a row with given Integer index for default index-less DataFrame" do
|
192
197
|
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])
|
198
|
+
c: [11,22,33,44,55]}, vectors: [:a, :b, :c])
|
194
199
|
|
195
200
|
expect(df[0, :row]).to eq([1,11,11].dv(nil, [:a, :b, :c]))
|
196
201
|
end
|
@@ -199,7 +204,7 @@ describe Daru::DataFrame do
|
|
199
204
|
context "#[:vector]=" do
|
200
205
|
before :each do
|
201
206
|
@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])
|
207
|
+
c: [11,22,33,44,55]}, vectors: [:a, :b, :c], index: [:one, :two, :three, :four, :five])
|
203
208
|
end
|
204
209
|
|
205
210
|
it "appends an Array as a Daru::Vector" do
|
@@ -258,7 +263,7 @@ describe Daru::DataFrame do
|
|
258
263
|
context "#[:row]=" do
|
259
264
|
before :each do
|
260
265
|
@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])
|
266
|
+
c: [11,22,33,44,55]}, vectors: [:a, :b, :c], index: [:one, :two, :three, :four, :five])
|
262
267
|
end
|
263
268
|
|
264
269
|
it "assigns specified row when Array" do
|
@@ -327,7 +332,7 @@ describe Daru::DataFrame do
|
|
327
332
|
context "#row" do
|
328
333
|
before :each do
|
329
334
|
@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])
|
335
|
+
c: [11,22,33,44,55]}, vectors: [:a, :b, :c], index: [:one, :two, :three, :four, :five])
|
331
336
|
end
|
332
337
|
|
333
338
|
it "creates an index for assignment if not already specified" do
|
@@ -350,10 +355,10 @@ describe Daru::DataFrame do
|
|
350
355
|
context "#==" do
|
351
356
|
it "compares by vectors, index and values of a DataFrame (ignores name)" do
|
352
357
|
a = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5]},
|
353
|
-
[:a, :b], [:one, :two, :three, :four, :five])
|
358
|
+
vectors: [:a, :b], index: [:one, :two, :three, :four, :five])
|
354
359
|
|
355
360
|
b = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5]},
|
356
|
-
[:a, :b], [:one, :two, :three, :four, :five])
|
361
|
+
vectors: [:a, :b], index: [:one, :two, :three, :four, :five])
|
357
362
|
|
358
363
|
expect(a).to eq(b)
|
359
364
|
end
|
@@ -427,7 +432,7 @@ describe Daru::DataFrame do
|
|
427
432
|
context "#map_vectors" do
|
428
433
|
it "iterates over vectors and returns a modified DataFrame" do
|
429
434
|
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])
|
435
|
+
c: [21,32,43,54,65]}, vectors: [:a, :b, :c], index: [:one, :two, :three, :four, :five])
|
431
436
|
|
432
437
|
ret = @data_frame.map_vectors do |vector|
|
433
438
|
vector = vector.map { |e| e += 10}
|
@@ -441,7 +446,7 @@ describe Daru::DataFrame do
|
|
441
446
|
context "#map_vectors_with_index" do
|
442
447
|
it "iterates over vectors with index and returns a modified DataFrame" do
|
443
448
|
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])
|
449
|
+
c: [21,32,43,54,65]}, vectors: [:a, :b, :c], index: [:one, :two, :three, :four, :five])
|
445
450
|
|
446
451
|
idx = []
|
447
452
|
ret = @data_frame.map_vectors_with_index do |vector, index|
|
@@ -457,7 +462,8 @@ describe Daru::DataFrame do
|
|
457
462
|
context "#map_rows" do
|
458
463
|
it "iterates over rows and returns a modified DataFrame" do
|
459
464
|
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],
|
465
|
+
c: [121, 484, 1089, 1936, 3025]}, vectors: [:a, :b, :c],
|
466
|
+
index: [:one, :two, :three, :four, :five])
|
461
467
|
|
462
468
|
ret = @data_frame.map_rows do |row|
|
463
469
|
expect(row.class).to eq(Daru::Vector)
|
@@ -471,7 +477,7 @@ describe Daru::DataFrame do
|
|
471
477
|
context "#map_rows_with_index" do
|
472
478
|
it "iterates over rows with index and returns a modified DataFrame" do
|
473
479
|
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])
|
480
|
+
c: [121, 484, 1089, 1936, 3025]},vectors: [:a, :b, :c], index: [:one, :two, :three, :four, :five])
|
475
481
|
|
476
482
|
idx = []
|
477
483
|
ret = @data_frame.map_rows_with_index do |row, index|
|
@@ -490,7 +496,8 @@ describe Daru::DataFrame do
|
|
490
496
|
@data_frame.delete_vector :a
|
491
497
|
|
492
498
|
expect(@data_frame).to eq(Daru::DataFrame.new({b: [11,12,13,14,15],
|
493
|
-
c: [11,22,33,44,55]}, [:b, :c],
|
499
|
+
c: [11,22,33,44,55]}, vectors: [:b, :c],
|
500
|
+
index: [:one, :two, :three, :four, :five]))
|
494
501
|
end
|
495
502
|
end
|
496
503
|
|
@@ -499,21 +506,19 @@ describe Daru::DataFrame do
|
|
499
506
|
@data_frame.delete_row :one
|
500
507
|
|
501
508
|
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]))
|
509
|
+
c: [22,33,44,55]}, vectors: [:a, :b, :c], index: [:two, :three, :four, :five]))
|
503
510
|
end
|
504
511
|
end
|
505
512
|
|
506
|
-
context "#keep_row_if" do
|
513
|
+
context "#keep_row_if", :focus => true do
|
507
514
|
it "keeps row if block evaluates to true" do
|
508
515
|
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])
|
516
|
+
c: [10,20,30,40,50]}, vectors: [:a, :b, :c], index: [:one, :two, :three, :four, :five])
|
510
517
|
|
511
518
|
df.keep_row_if do |row|
|
512
|
-
row
|
519
|
+
row[:a] % 10 == 0
|
513
520
|
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]))
|
521
|
+
# TODO: write expectation
|
517
522
|
end
|
518
523
|
end
|
519
524
|
|
@@ -523,8 +528,32 @@ describe Daru::DataFrame do
|
|
523
528
|
vector == [1,2,3,4,5].dv(nil, [:one, :two, :three, :four, :five])
|
524
529
|
end
|
525
530
|
|
526
|
-
expect(@data_frame).to eq(Daru::DataFrame.new({a: [1,2,3,4,5]}, [:a],
|
527
|
-
[:one, :two, :three, :four, :five]))
|
531
|
+
expect(@data_frame).to eq(Daru::DataFrame.new({a: [1,2,3,4,5]}, vectors: [:a],
|
532
|
+
index: [:one, :two, :three, :four, :five]))
|
533
|
+
end
|
534
|
+
end
|
535
|
+
|
536
|
+
context "#filter_rows" do
|
537
|
+
it "filters rows" do
|
538
|
+
df = Daru::DataFrame.new({a: [1,2,3], b: [2,3,4]})
|
539
|
+
|
540
|
+
a = df.filter_rows do |row|
|
541
|
+
row[:a] % 2 == 0
|
542
|
+
end
|
543
|
+
|
544
|
+
expect(a).to eq(Daru::DataFrame.new({a: [2], b: [3]}, vectors: [:a, :b], index: [1]))
|
545
|
+
end
|
546
|
+
end
|
547
|
+
|
548
|
+
context "#filter_vectors" do
|
549
|
+
it "filters vectors" do
|
550
|
+
df = Daru::DataFrame.new({a: [1,2,3], b: [2,3,4]})
|
551
|
+
|
552
|
+
a = df.filter_vectors do |vector|
|
553
|
+
vector[0] == 1
|
554
|
+
end
|
555
|
+
|
556
|
+
expect(a).to eq(Daru::DataFrame.new({a: [1,2,3]}))
|
528
557
|
end
|
529
558
|
end
|
530
559
|
|