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
data/spec/index/index_spec.rb
CHANGED
@@ -389,6 +389,14 @@ describe DaruLite::Index do
|
|
389
389
|
|
390
390
|
end
|
391
391
|
|
392
|
+
describe '#delete_at' do
|
393
|
+
subject { idx.delete_at(1)}
|
394
|
+
|
395
|
+
let(:idx) { DaruLite::Index.new([:a, :b, 1, 2]) }
|
396
|
+
|
397
|
+
it { is_expected.to eq DaruLite::Index.new([:a, 1, 2]) }
|
398
|
+
end
|
399
|
+
|
392
400
|
context '#to_df' do
|
393
401
|
let(:idx) do
|
394
402
|
DaruLite::Index.new(['speaker', 'mic', 'guitar', 'amp'],
|
@@ -0,0 +1,27 @@
|
|
1
|
+
shared_examples_for 'an aggregatable Vector' do
|
2
|
+
context "#group_by" do
|
3
|
+
let(:dv) { DaruLite::Vector.new [:a, :b, :a, :b, :c] }
|
4
|
+
|
5
|
+
context 'vector not specified' do
|
6
|
+
subject { dv.group_by }
|
7
|
+
|
8
|
+
it { is_expected.to be_a DaruLite::Core::GroupBy }
|
9
|
+
its(:'groups.size') { is_expected.to eq 3 }
|
10
|
+
its(:groups) { is_expected.to eq({[:a]=>[0, 2], [:b]=>[1, 3], [:c]=>[4]}) }
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'vector name specified' do
|
14
|
+
before { dv.name = :hello }
|
15
|
+
subject { dv.group_by :hello }
|
16
|
+
|
17
|
+
it { is_expected.to be_a DaruLite::Core::GroupBy }
|
18
|
+
its(:'groups.size') { is_expected.to eq 3 }
|
19
|
+
its(:groups) { is_expected.to eq({[:a]=>[0, 2], [:b]=>[1, 3], [:c]=>[4]}) }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'vector name invalid' do
|
23
|
+
before { dv.name = :hello }
|
24
|
+
it { expect { dv.group_by :abc }.to raise_error }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
shared_examples_for 'a calculatable Vector' do |dtype|
|
2
|
+
describe '#count_values' do
|
3
|
+
let(:vector) { DaruLite::Vector.new([1, 2, 3, 1, 2, nil, nil]) }
|
4
|
+
|
5
|
+
it { expect(vector.count_values 1, 2).to eq 4 }
|
6
|
+
it { expect(vector.count_values nil).to eq 2 }
|
7
|
+
it { expect(vector.count_values 3, Float::NAN).to eq 1 }
|
8
|
+
it { expect(vector.count_values 4).to eq 0 }
|
9
|
+
end
|
10
|
+
|
11
|
+
context "#summary" do
|
12
|
+
subject { vector.summary }
|
13
|
+
|
14
|
+
context 'all types' do
|
15
|
+
let(:vector) { DaruLite::Vector.new([1 ,2, 3, 4, 5], name: 'vector') }
|
16
|
+
|
17
|
+
it { is_expected.to include vector.name }
|
18
|
+
|
19
|
+
it { is_expected.to include "n :#{vector.size}" }
|
20
|
+
|
21
|
+
it { is_expected.to include "non-missing:#{vector.size - vector.count_values(*DaruLite::MISSING_VALUES)}" }
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
context "numeric type" do
|
26
|
+
let(:vector) { DaruLite::Vector.new([1,2,5], name: 'numeric') }
|
27
|
+
|
28
|
+
it { is_expected. to eq %Q{
|
29
|
+
|= numeric
|
30
|
+
| n :3
|
31
|
+
| non-missing:3
|
32
|
+
| median: 2
|
33
|
+
| mean: 2.6667
|
34
|
+
| std.dev.: 2.0817
|
35
|
+
| std.err.: 1.2019
|
36
|
+
| skew: 0.2874
|
37
|
+
| kurtosis: -2.3333
|
38
|
+
}.unindent }
|
39
|
+
end
|
40
|
+
|
41
|
+
context "numeric type with missing values" do
|
42
|
+
let(:vector) { DaruLite::Vector.new([1,2,5,nil,Float::NAN], name: 'numeric') }
|
43
|
+
|
44
|
+
it { is_expected.not_to include 'skew' }
|
45
|
+
it { is_expected.not_to include 'kurtosis' }
|
46
|
+
end
|
47
|
+
|
48
|
+
if dtype == :array
|
49
|
+
context "object type" do
|
50
|
+
let(:vector) { DaruLite::Vector.new([1, 1, 2, 2, "string", nil, Float::NAN], name: 'object') }
|
51
|
+
|
52
|
+
if RUBY_VERSION >= '2.2'
|
53
|
+
it { is_expected.to eq %Q{
|
54
|
+
|= object
|
55
|
+
| n :7
|
56
|
+
| non-missing:5
|
57
|
+
| factors: 1,2,string
|
58
|
+
| mode: 1,2
|
59
|
+
| Distribution
|
60
|
+
| string 1 50.00%
|
61
|
+
| NaN 1 50.00%
|
62
|
+
| 1 2 100.00%
|
63
|
+
| 2 2 100.00%
|
64
|
+
}.unindent }
|
65
|
+
else
|
66
|
+
it { is_expected.to eq %Q{
|
67
|
+
|= object
|
68
|
+
| n :7
|
69
|
+
| non-missing:5
|
70
|
+
| factors: 1,2,string
|
71
|
+
| mode: 1,2
|
72
|
+
| Distribution
|
73
|
+
| NaN 1 50.00%
|
74
|
+
| string 1 50.00%
|
75
|
+
| 2 2 100.00%
|
76
|
+
| 1 2 100.00%
|
77
|
+
}.unindent }
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
shared_examples_for 'a convertible Vector' do |dtype|
|
2
|
+
describe "#to_df" do
|
3
|
+
subject { vector.to_df }
|
4
|
+
|
5
|
+
let(:vector) do
|
6
|
+
DaruLite::Vector.new(['a','b','c'], name: :my_dv, index: ['alpha', 'beta', 'gamma'])
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'is a dataframe' do
|
10
|
+
expect(subject).to be_a DaruLite::DataFrame
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'converts the vector to a single-vector dataframe' do
|
14
|
+
expect(subject[:my_dv]).to eq vector
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'has the same index as the original vector' do
|
18
|
+
expect(subject.index).to eq vector.index
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'has the same name as the vector' do
|
22
|
+
expect(subject.name).to eq :my_dv
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "#to_h" do
|
27
|
+
context DaruLite::Index do
|
28
|
+
subject { vector.to_h }
|
29
|
+
|
30
|
+
let(:vector) do
|
31
|
+
DaruLite::Vector.new(
|
32
|
+
[1,2,3,4,5],
|
33
|
+
name: :a,
|
34
|
+
index: [:one, :two, :three, :four, :five],
|
35
|
+
dtype:
|
36
|
+
)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "returns the vector as a hash" do
|
40
|
+
expect(subject).to eq({one: 1, two: 2, three: 3, four: 4, five: 5})
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context DaruLite::MultiIndex do
|
45
|
+
pending
|
46
|
+
# it "returns vector as a Hash" do
|
47
|
+
# pending
|
48
|
+
# mi = DaruLite::MultiIndex.from_tuples([
|
49
|
+
# [:a,:two,:bar],
|
50
|
+
# [:a,:two,:baz],
|
51
|
+
# [:b,:one,:bar],
|
52
|
+
# [:b,:two,:bar]
|
53
|
+
# ])
|
54
|
+
# vector = DaruLite::Vector.new([1,2,3,4], index: mi, dtype: dtype)
|
55
|
+
# expect(vector.to_h).to eq({
|
56
|
+
# [:a,:two,:bar] => 1,
|
57
|
+
# [:a,:two,:baz] => 2,
|
58
|
+
# [:b,:one,:bar] => 3,
|
59
|
+
# [:b,:two,:bar] => 4
|
60
|
+
# })
|
61
|
+
# end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "#to_json" do
|
66
|
+
subject { vector.to_json }
|
67
|
+
|
68
|
+
let(:vector) do
|
69
|
+
DaruLite::Vector.new(
|
70
|
+
[1,2,3,4,5],
|
71
|
+
name: :a,
|
72
|
+
index: [:one, :two, :three, :four, :five],
|
73
|
+
dtype: dtype
|
74
|
+
)
|
75
|
+
end
|
76
|
+
|
77
|
+
it "returns the vector as json" do
|
78
|
+
expect(subject).to eq(vector.to_h.to_json)
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "#to_s" do
|
84
|
+
let(:vector) { DaruLite::Vector.new(["a", "b"], index: [1, 2], name:) }
|
85
|
+
|
86
|
+
context 'name is nil' do
|
87
|
+
let(:name) { nil }
|
88
|
+
|
89
|
+
it 'produces a class, size description' do
|
90
|
+
expect(vector.to_s).to eq("#<DaruLite::Vector(2)>")
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
context 'name is present' do
|
95
|
+
let(:name) { "Test" }
|
96
|
+
|
97
|
+
it 'produces a class, name, size description' do
|
98
|
+
expect(vector.to_s).to eq("#<DaruLite::Vector: Test(2)>")
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
context 'name is a symbol' do
|
103
|
+
let(:name) { :Test }
|
104
|
+
|
105
|
+
it 'produces a class, name, size description' do
|
106
|
+
expect(vector.to_s).to eq("#<DaruLite::Vector: Test(2)>")
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
describe "#to_matrix" do
|
112
|
+
let(:vector) { DaruLite::Vector.new [1, 2, 3, 4, 5, 6] }
|
113
|
+
|
114
|
+
it "converts DaruLite::Vector to a horizontal Ruby Matrix" do
|
115
|
+
expect(vector.to_matrix).to eq(Matrix[[1, 2, 3, 4, 5, 6]])
|
116
|
+
end
|
117
|
+
|
118
|
+
it "converts DaruLite::Vector to a vertical Ruby Matrix" do
|
119
|
+
expect(vector.to_matrix(:vertical)).to eq(Matrix.columns([[1, 2, 3, 4, 5, 6]]))
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'raises on wrong axis' do
|
123
|
+
expect { vector.to_matrix(:strange) }.to raise_error(ArgumentError)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
shared_examples_for 'a duplicatable Vector' do
|
2
|
+
describe "#dup" do
|
3
|
+
subject { vector.dup }
|
4
|
+
|
5
|
+
let(:vector) do
|
6
|
+
DaruLite::Vector.new([1, 2], name: :yoda, index: [:happy, :lightsaber])
|
7
|
+
end
|
8
|
+
|
9
|
+
it "copies the original data" do
|
10
|
+
expect(subject.send(:data)).to eq([1,2])
|
11
|
+
end
|
12
|
+
|
13
|
+
it "creates a new data object" do
|
14
|
+
expect(subject.send(:data).object_id).not_to eq(vector.send(:data).object_id)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "copies the name" do
|
18
|
+
expect(subject.name).to eq(:yoda)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "copies the original index" do
|
22
|
+
expect(subject.index).to eq(DaruLite::Index.new([:happy, :lightsaber]))
|
23
|
+
end
|
24
|
+
|
25
|
+
it "creates a new index object" do
|
26
|
+
expect(subject.index.object_id).not_to eq(vector.index.object_id)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "#clone_structure" do
|
31
|
+
subject { vector.clone_structure }
|
32
|
+
context DaruLite::Index do
|
33
|
+
let(:vector) do
|
34
|
+
DaruLite::Vector.new([1, 2, 3, 4, 5], index: [:a,:b,:c,:d,:e])
|
35
|
+
end
|
36
|
+
|
37
|
+
it "clones a vector with its index and fills it with nils" do
|
38
|
+
expect(subject).to eq(
|
39
|
+
DaruLite::Vector.new([nil, nil, nil, nil, nil], index: [:a,:b,:c,:d,:e])
|
40
|
+
)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context DaruLite::MultiIndex do
|
45
|
+
pending
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|