datamix 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a8a6ea13859c50d3385c273c2bbfd87fb5828977
4
- data.tar.gz: 67d4a65967bdd2a3b979db246ae28ebdc17665e0
3
+ metadata.gz: 2e25311621834c0792028b34973e716c1dc0bce2
4
+ data.tar.gz: df08942707700a403c16e55bf0cd39659520ec7c
5
5
  SHA512:
6
- metadata.gz: 6c52d6bf99769dfe22b293c2d439b76d0d261afca936f0e8387f80c8f223b52ecb825231485599c8f1b12916bc06520efa37fd591a4fb6cd2f6352ad7dc062d8
7
- data.tar.gz: c70e97e9dd7df50418f9542e4ae932a5fa23230035186df2dab322ec7dc507595cf9541ac30c7dd725e307769d4883a56c924185d1e68d10d649879706ea5550
6
+ metadata.gz: 643e11e9a83a4c498ca2c042b59780eed0e95a26f07cb6f2624e294b78812c6abeb6669bc897cccb21b65119d906f751b3f671e4696b7d90b802df9a0399d933
7
+ data.tar.gz: 1c9dc5162696b3b6de6110bee2d0cf183ceec3f913186bc7887554a6d716255212b4761514da3762866b1a668d41c0f55ac2d1a98183d5d86265d36948a82e58
data/README.md CHANGED
@@ -8,8 +8,8 @@ DataMix - DSL for manipulating tabular data
8
8
 
9
9
  ---
10
10
 
11
- This library refines Ruby's [`CSV::Table`][1] and `Array` object to provide a DSL
12
- for manipulating tabular data.
11
+ This library refines Ruby's [`CSV::Table`][1] and `Array` objects to provide
12
+ a DSL for manipulating tabular data.
13
13
 
14
14
  ---
15
15
 
@@ -29,17 +29,61 @@ gem 'datamix'
29
29
 
30
30
 
31
31
 
32
- Usage
32
+ Example Usage
33
33
  --------------------------------------------------
34
34
 
35
- Require the library and enable the refinements with `using DataMix`:
36
-
37
35
  ```ruby
38
36
  require 'datamix'
39
37
  using DataMix
38
+
39
+ # Load data (this is a shortcut to load a CSV::Table object)
40
+ sp500 = file 'sp500.csv'
41
+ vix = file 'vix.csv'
42
+
43
+ # Keep only desired columns, rename and round data
44
+ sp500.keep :date, :adjusted_close
45
+ sp500.rename :adjusted_close, to: :close
46
+ sp500.round :close, decimals: 2
47
+
48
+ # Calculate a Change column, based on the Close column and round
49
+ sp500[:change] = sp500[:close] - sp500[:close].prev
50
+ sp500.round :change, decimals: 2
51
+
52
+ # Keep only desired columns and rename
53
+ vix.keep :date, :adjusted_close
54
+ vix.rename :adjusted_close, to: :vix
55
+
56
+ # Join the two tables
57
+ sp500.join vix, on: :date
58
+
59
+ # Remove all rows that have any empty value
60
+ sp500.delete_empty_rows
61
+
62
+ # Save and preview
63
+ sp500.save_as 'output.csv'
64
+ sp500.preview
65
+
66
+ # Output
67
+ # +------------+---------+--------+-------+
68
+ # | date | close | change | vix |
69
+ # +------------+---------+--------+-------+
70
+ # | 2015-03-12 | 2065.95 | 25.71 | 15.42 |
71
+ # | 2015-03-13 | 2053.4 | -12.55 | 16.0 |
72
+ # | 2015-03-16 | 2081.19 | 27.79 | 15.61 |
73
+ # | 2015-03-17 | 2074.28 | -6.91 | 15.66 |
74
+ # | 2015-03-18 | 2099.5 | 25.22 | 13.97 |
75
+ # | 2015-03-19 | 2089.27 | -10.23 | 14.07 |
76
+ # | 2015-03-20 | 2108.1 | 18.83 | 13.02 |
77
+ # | 2015-03-23 | 2104.42 | -3.68 | 13.41 |
78
+ # | 2015-03-24 | 2091.5 | -12.92 | 13.62 |
79
+ # | 2015-03-25 | 2061.05 | -30.45 | 15.44 |
80
+ # +------------+---------+--------+-------+
40
81
  ```
41
82
 
42
- TODO: Complete documentation.
83
+ Examples
84
+ --------------------------------------------------
43
85
 
86
+ See the [examples index][2] for more examples.
44
87
 
45
88
  [1]: https://ruby-doc.org/stdlib-2.3.1/libdoc/csv/rdoc/CSV/Table.html
89
+ [2]: https://github.com/DannyBen/datamix/tree/master/examples#examples-index
@@ -1,43 +1,19 @@
1
1
  module DataMix
2
2
  refine Array do
3
3
  def -(other)
4
- if other.respond_to? :each
5
- each_with_index do |val, index|
6
- self[index] = other[index] ? val - other[index] : nil
7
- end
8
- else
9
- map { |val| val - other }
10
- end
4
+ math_operation(other) { |left, right| left - right }
11
5
  end
12
6
 
13
7
  def +(other)
14
- if other.respond_to? :each
15
- each_with_index do |val, index|
16
- self[index] = other[index] ? val + other[index] : nil
17
- end
18
- else
19
- map { |val| val + other }
20
- end
8
+ math_operation(other) { |left, right| left + right }
21
9
  end
22
10
 
23
11
  def *(other)
24
- if other.respond_to? :each
25
- each_with_index do |val, index|
26
- self[index] = other[index] ? val * other[index] : nil
27
- end
28
- else
29
- map { |val| val * other }
30
- end
12
+ math_operation(other) { |left, right| left * right }
31
13
  end
32
14
 
33
15
  def /(other)
34
- if other.respond_to? :each
35
- each_with_index do |val, index|
36
- self[index] = other[index] ? val / other[index].to_f : nil
37
- end
38
- else
39
- map { |val| val / other.to_f }
40
- end
16
+ math_operation(other) { |left, right| left / right.to_f }
41
17
  end
42
18
 
43
19
  def offset(rows)
@@ -53,6 +29,10 @@ module DataMix
53
29
  offset rows
54
30
  end
55
31
 
32
+ def next(rows=1)
33
+ offset -rows
34
+ end
35
+
56
36
  def round(decimals=0)
57
37
  map { |val| val ? val.round(decimals) : nil }
58
38
  end
@@ -69,5 +49,17 @@ module DataMix
69
49
  Array.new(window_size-1).concat result
70
50
  end
71
51
 
52
+ private
53
+
54
+ def math_operation(other)
55
+ if other.respond_to? :each
56
+ each_with_index do |val, index|
57
+ self[index] = other[index] ? yield(val, other[index]) : nil
58
+ end
59
+ else
60
+ map { |val| yield(val, other) }
61
+ end
62
+ end
63
+
72
64
  end
73
65
  end
@@ -39,11 +39,13 @@ module DataMix
39
39
  raise CSVError, "source[#{on}] is not unique" unless by_col[on].uniq?
40
40
  raise CSVError, "other[#{on}] is not unique" unless other.by_col[on].uniq?
41
41
 
42
+ original_headers = headers.dup
43
+
42
44
  by_row.each do |row|
43
45
  other_row = other.find { |r| r[on] == row[on] }
44
46
  other.headers.each do |col|
45
47
  next if col == on
46
- new_col = headers.include?(col) ? "_#{col}" : col
48
+ new_col = original_headers.include?(col) ? "_#{col}" : col
47
49
  row[new_col] = other_row ? other_row[col] : nil
48
50
  end
49
51
  end
@@ -1,3 +1,3 @@
1
1
  module DataMix
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datamix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-03 00:00:00.000000000 Z
11
+ date: 2017-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: terminal-table
@@ -136,6 +136,20 @@ dependencies:
136
136
  - - "~>"
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0.10'
139
+ - !ruby/object:Gem::Dependency
140
+ name: filewatcher
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0.5'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0.5'
139
153
  description: DSL for manipulating tabular data
140
154
  email: db@dannyben.com
141
155
  executables: []