davidrichards-data_frame 0.0.8 → 0.0.9
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.
- data/VERSION.yml +1 -1
- data/lib/data_frame.rb +27 -0
- data/spec/data_frame_spec.rb +13 -0
- metadata +2 -2
data/VERSION.yml
CHANGED
data/lib/data_frame.rb
CHANGED
@@ -4,6 +4,14 @@ require 'just_enumerable_stats'
|
|
4
4
|
require 'open-uri'
|
5
5
|
require 'fastercsv'
|
6
6
|
|
7
|
+
# Use a Dictionary if available
|
8
|
+
begin
|
9
|
+
require 'facets/dictionary'
|
10
|
+
rescue LoadError => e
|
11
|
+
# Do nothing
|
12
|
+
end
|
13
|
+
|
14
|
+
|
7
15
|
Dir.glob("#{File.dirname(__FILE__)}/ext/*.rb").each { |file| require file }
|
8
16
|
|
9
17
|
$:.unshift(File.dirname(__FILE__))
|
@@ -94,6 +102,25 @@ class DataFrame
|
|
94
102
|
@items.transpose[i]
|
95
103
|
end
|
96
104
|
|
105
|
+
# The rows as an array of arrays, an alias for items.
|
106
|
+
alias :rows :items
|
107
|
+
|
108
|
+
# The columns as a Dictionary or Hash
|
109
|
+
# This is cached, call columns(true) to reset the cache.
|
110
|
+
def columns(reset=false)
|
111
|
+
@columns = nil if reset
|
112
|
+
return @columns if @columns
|
113
|
+
|
114
|
+
container = defined?(Dictionary) ? Dictionary.new : Hash.new
|
115
|
+
i = 0
|
116
|
+
|
117
|
+
@columns = @items.transpose.inject(container) do |cont, col|
|
118
|
+
cont[@labels[i]] = col
|
119
|
+
i += 1
|
120
|
+
cont
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
97
124
|
def render_row(sym)
|
98
125
|
i = self.row_labels.index(sym)
|
99
126
|
return nil unless i
|
data/spec/data_frame_spec.rb
CHANGED
@@ -101,5 +101,18 @@ describe DataFrame do
|
|
101
101
|
@df.labels.should eql([:threes, :fours])
|
102
102
|
end
|
103
103
|
|
104
|
+
it "should offer a hash-like structure of columns" do
|
105
|
+
@df.add [1,2,3,4]
|
106
|
+
@df.add [5, 6, 7, 8]
|
107
|
+
@df.columns[:these].should eql([1, 5])
|
108
|
+
@df.columns[:are].should eql([2, 6])
|
109
|
+
@df.columns[:the].should eql([3, 7])
|
110
|
+
@df.columns[:labels].should eql([4, 8])
|
111
|
+
end
|
104
112
|
|
113
|
+
it "should alias items with rows" do
|
114
|
+
@df.add [1,2,3,4]
|
115
|
+
@df.add [5, 6, 7, 8]
|
116
|
+
@df.rows.should eql(@df.items)
|
117
|
+
end
|
105
118
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: davidrichards-data_frame
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Richards
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-08-
|
12
|
+
date: 2009-08-04 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|