quandl_operation 0.1.11 → 0.1.12

Sign up to get free protection for your applications and to get access to all the features.
data/UPGRADE.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.1.12
2
+
3
+ * Quandl::Operation::Collapse.perform will merge rows with nils
4
+
5
+
1
6
  ## 0.1.11
2
7
 
3
8
  * it should parse escaped csv data
@@ -9,8 +9,15 @@ class Collapse
9
9
  class << self
10
10
 
11
11
  def perform(data, frequency)
12
- data = Parse.sort( data )
12
+ # source order
13
+ order = Parse.sort_order?(data)
14
+ # operations expect data in ascending order
15
+ data = Parse.sort( data, :asc )
16
+ # collapse
13
17
  data = collapse_and_log(data, frequency)
18
+ # return to original order
19
+ data = Parse.sort( data, :desc ) if order == :desc
20
+ # onwards
14
21
  data
15
22
  end
16
23
 
@@ -40,13 +47,18 @@ class Collapse
40
47
  range = find_end_of_range( data[0][0], frequency )
41
48
  # iterate over the data
42
49
  data.each do |row|
43
- # grab date and value
50
+ # grab date and values
44
51
  date, value = row[0], row[1..-1]
45
52
  value = value.first if value.count == 1
46
53
  # bump to the next range if it exceeds the current one
47
54
  range = find_end_of_range(date, frequency) unless inside_range?(date, range)
48
55
  # consider the value for the next range
49
- collapsed_data[range] = value if inside_range?(date, range) && value.present?
56
+ if inside_range?(date, range) && value.present?
57
+ # merge this and previous row if nils are present
58
+ value = merge_row_values( value, collapsed_data[range] ) unless collapsed_data[range].nil?
59
+ # assign value
60
+ collapsed_data[range] = value
61
+ end
50
62
  end
51
63
  to_table(collapsed_data)
52
64
  end
@@ -61,6 +73,17 @@ class Collapse
61
73
  end
62
74
  end
63
75
 
76
+ def merge_row_values(top_row, bottom_row)
77
+ # merge previous values when nils are present
78
+ if top_row.is_a?(Array) && top_row.include?(nil)
79
+ # find nil indexes
80
+ indexes = find_each_index(top_row, nil)
81
+ # merge nils with previous values
82
+ indexes.each{|index| top_row[index] = bottom_row[index] }
83
+ end
84
+ top_row
85
+ end
86
+
64
87
  def collapses_greater_than(freq)
65
88
  index = valid_collapses.index(freq.to_sym)
66
89
  index.present? ? valid_collapses.slice( index + 1, valid_collapses.count ) : []
@@ -82,6 +105,18 @@ class Collapse
82
105
  Date.jd(date).end_of_frequency(frequency).jd
83
106
  end
84
107
 
108
+ def find_each_index(array, find)
109
+ found, index, q = -1, -1, []
110
+ while found
111
+ found = array[index+1..-1].index(find)
112
+ if found
113
+ index = index + found + 1
114
+ q << index
115
+ end
116
+ end
117
+ q
118
+ end
119
+
85
120
  end
86
121
 
87
122
  end
@@ -1,5 +1,5 @@
1
1
  module Quandl
2
2
  module Operation
3
- VERSION = "0.1.11"
3
+ VERSION = "0.1.12"
4
4
  end
5
5
  end
@@ -20,6 +20,8 @@ Gem::Specification.new do |s|
20
20
  s.add_development_dependency "rake", "~> 10.0"
21
21
  s.add_development_dependency "rspec", "~> 2.13"
22
22
  s.add_development_dependency "fivemat", "~> 1.2"
23
+ s.add_development_dependency "pry"
24
+ s.add_development_dependency "quandl_data"
23
25
 
24
26
  s.add_runtime_dependency "activesupport", ">= 3.0.0"
25
27
 
@@ -35,4 +35,68 @@ describe Quandl::Operation::Collapse do
35
35
  subject.collapses_greater_than_or_equal_to(:annual).should eq [:annual]
36
36
  end
37
37
 
38
+ def table_range(from, to, columns = 1)
39
+ days = to - from
40
+ r = days.times.collect do |i|
41
+ [ from + i ] + columns.times.collect{(rand(1000) * 0.7) + i}
42
+ end
43
+ r
44
+ end
45
+
46
+ describe "#perform" do
47
+
48
+ let(:data){
49
+ [[2456537, 56.2, nil, nil],
50
+ [2456518, 55.7, nil, nil], [2456506, nil, 136133.0, nil],
51
+ [2456487, 55.4, nil, nil], [2456475, nil, 135964.0, nil],
52
+ [2456457, 50.9, nil, nil], [2456445, nil, 135860.0, nil]]
53
+ }
54
+
55
+ it "should collapse data with nils" do
56
+ subject.perform(data, :monthly).should eq [
57
+ [2456566, 56.2, nil, nil],
58
+ [2456536, 55.7, 136133.0, nil],
59
+ [2456505, 55.4, 135964.0, nil],
60
+ [2456474, 50.9, 135860.0, nil]]
61
+ end
62
+
63
+ end
64
+
65
+ describe "#collapse" do
66
+
67
+ it 'should handle data sets with one data point only' do
68
+
69
+ data = [[ 2455875, 42 ]]
70
+ subject.collapse( data, :daily).should eq [[2455875, 42]]
71
+ subject.collapse( data, :weekly).should eq [[2455879, 42]]
72
+ subject.collapse( data, :monthly).should eq [[2455896, 42]]
73
+ subject.collapse( data, :quarterly).should eq [[2455927, 42]]
74
+ subject.collapse( data, :annual).should eq [[2455927, 42]]
75
+
76
+ end
77
+
78
+ it 'should handle data sets with only two data points, 1 day apart' do
79
+
80
+ data = [[ Date.parse('2011-11-09').jd, 20111109 ],[ Date.parse('2011-11-10').jd, 20111110 ]]
81
+
82
+ subject.collapse(data, :daily).should eq data
83
+ subject.collapse(data, :weekly).should eq [[ 2455879, 20111110 ]]
84
+ subject.collapse(data, :monthly).should eq [[ 2455896, 20111110 ]]
85
+ subject.collapse(data, :quarterly).should eq [[ 2455927, 20111110 ]]
86
+ subject.collapse(data, :annual).should eq [[ 2455927, 20111110 ]]
87
+
88
+ end
89
+
90
+ it 'should handle data sets one year of daily data' do
91
+ data = table_range Date.parse('Jan 1, 2011').jd, Date.parse('Dec 31, 2011').jd, 2
92
+ data.count.should eq 364
93
+ subject.collapse( data, :daily ).count.should eq 364
94
+ subject.collapse( data, :weekly ).count.should eq 53
95
+ subject.collapse( data, :monthly ).count.should eq 12
96
+ subject.collapse( data, :quarterly ).count.should eq 4
97
+ subject.collapse( data, :annual ).count.should eq 1
98
+ end
99
+
100
+ end
101
+
38
102
  end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
2
2
 
3
3
  require "rspec"
4
- require "quandl/operation"
4
+ require "quandl/operation"
5
+ require 'pry'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quandl_operation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.1.12
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-30 00:00:00.000000000 Z
12
+ date: 2013-10-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -59,6 +59,38 @@ dependencies:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
61
  version: '1.2'
62
+ - !ruby/object:Gem::Dependency
63
+ name: pry
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: quandl_data
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
62
94
  - !ruby/object:Gem::Dependency
63
95
  name: activesupport
64
96
  requirement: !ruby/object:Gem::Requirement