Class: DataFrame
- Inherits:
-
Object
- Object
- DataFrame
- Defined in:
- lib/data_frame.rb
Overview
This allows me to have named columns and optionally named rows in a data frame, to work calculations (usually on the columns), to transpose the matrix and store the transposed matrix until the object is tainted.
Instance Attribute Summary collapse
-
#items ⇒ Object
(also: #rows)
readonly
The items stored in the frame.
-
#labels ⇒ Object
(also: #variables)
readonly
The labels of the data items.
Class Method Summary collapse
-
.from_csv(obj, opts = {}) ⇒ Object
This is the neatest part of this neat gem.
Instance Method Summary collapse
- #add_item(item) ⇒ Object (also: #add)
-
#columns(reset = false) ⇒ Object
(also: #to_hash, #to_dictionary)
The columns as a Dictionary or Hash This is cached, call columns(true) to reset the cache.
- #drop!(label) ⇒ Object
-
#import(rows) ⇒ Object
Loads a batch of rows.
-
#initialize(*labels) ⇒ DataFrame
constructor
A new instance of DataFrame.
- #method_missing(sym, *args, &block) ⇒ Object
- #render_column(sym) ⇒ Object
- #render_row(sym) ⇒ Object
- #row_labels ⇒ Object
- #row_labels=(ary) ⇒ Object
Constructor Details
#initialize(*labels) ⇒ DataFrame
Returns a new instance of DataFrame.
81 82 83 84 |
# File 'lib/data_frame.rb', line 81 def initialize(*labels) @labels = labels.map {|e| e.to_underscore_sym } @items = TransposableArray.new end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/data_frame.rb', line 133 def method_missing(sym, *args, &block) if self.labels.include?(sym) render_column(sym) elsif self.row_labels.include?(sym) render_row(sym) elsif @items.respond_to?(sym) @items.send(sym, *args, &block) else super end end |
Instance Attribute Details
#items ⇒ Object (readonly) Also known as: rows
The items stored in the frame
79 80 81 |
# File 'lib/data_frame.rb', line 79 def items @items end |
#labels ⇒ Object (readonly) Also known as: variables
The labels of the data items
75 76 77 |
# File 'lib/data_frame.rb', line 75 def labels @labels end |
Class Method Details
.from_csv(obj, opts = {}) ⇒ Object
This is the neatest part of this neat gem. DataFrame.from_csv can be called in a lot of ways: DataFrame.from_csv(csv_contents) DataFrame.from_csv(filename) DataFrame.from_csv(url) If you need to define converters for FasterCSV, do it before calling this method: FasterCSV::Converters = lambda{|f| f == ‘foo’ ? ‘bar’ : ‘foo’} DataFrame.from_csv(‘example.com/my_special_url.csv’, :converters => :special) This returns bar where ‘foo’ was found and ‘foo’ everywhere else.
40 41 42 43 44 45 46 |
# File 'lib/data_frame.rb', line 40 def from_csv(obj, opts={}) labels, table = infer_csv_contents(obj, opts) return nil unless labels and table df = new(*labels) df.import(table) df end |
Instance Method Details
#add_item(item) ⇒ Object Also known as: add
86 87 88 |
# File 'lib/data_frame.rb', line 86 def add_item(item) self.items << item end |
#columns(reset = false) ⇒ Object Also known as: to_hash, to_dictionary
The columns as a Dictionary or Hash This is cached, call columns(true) to reset the cache.
111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/data_frame.rb', line 111 def columns(reset=false) @columns = nil if reset return @columns if @columns container = defined?(Dictionary) ? Dictionary.new : Hash.new i = 0 @columns = @items.transpose.inject(container) do |cont, col| cont[@labels[i]] = col i += 1 cont end end |
#drop!(label) ⇒ Object
145 146 147 148 149 150 151 152 153 |
# File 'lib/data_frame.rb', line 145 def drop!(label) i = self.labels.index(label) return nil unless i self.items.each do |item| item.delete_at(i) end self.labels.delete_at(i) true end |
#import(rows) ⇒ Object
Loads a batch of rows. Expects an array of arrays, else you don’t know what you have.
68 69 70 71 72 |
# File 'lib/data_frame.rb', line 68 def import(rows) rows.each do |row| self.add_item(row) end end |
#render_column(sym) ⇒ Object
100 101 102 103 104 |
# File 'lib/data_frame.rb', line 100 def render_column(sym) i = @labels.index(sym) return nil unless i @items.transpose[i] end |
#render_row(sym) ⇒ Object
127 128 129 130 131 |
# File 'lib/data_frame.rb', line 127 def render_row(sym) i = self.row_labels.index(sym) return nil unless i @items[i] end |
#row_labels ⇒ Object
91 92 93 |
# File 'lib/data_frame.rb', line 91 def row_labels @row_labels ||= [] end |
#row_labels=(ary) ⇒ Object
95 96 97 98 |
# File 'lib/data_frame.rb', line 95 def row_labels=(ary) raise ArgumentError, "Row labels must be an array" unless ary.is_a?(Array) @row_labels = ary end |