Class: Daru::DataFrame

Inherits:
Object
  • Object
show all
Defined in:
lib/daru/dataframe.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, fields = [], name = SecureRandom.uuid) ⇒ DataFrame

Returns a new instance of DataFrame.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/daru/dataframe.rb', line 12

def initialize source, fields=[], name=SecureRandom.uuid
  if source.empty?
    @vectors = fields.inject({}){ |a,x| a[x]=Daru::Vector.new; a}
  else
    @vectors = source
  end

  @fields = fields.empty? ? source.keys.sort : fields
  @name   = name
 
  check_length
  set_fields_order if @vectors.keys.sort != @fields.sort
  set_vector_names
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



183
184
185
186
187
188
189
190
191
# File 'lib/daru/dataframe.rb', line 183

def method_missing(name, *args)
  if md = name.match(/(.+)\=/)
    insert_vector name[/(.+)\=/].delete("="), args[0]
  elsif self.has_vector? name
    column name
  else
    super(name, *args)
  end
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



6
7
8
# File 'lib/daru/dataframe.rb', line 6

def fields
  @fields
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/daru/dataframe.rb', line 10

def name
  @name
end

#sizeObject (readonly)

Returns the value of attribute size.



8
9
10
# File 'lib/daru/dataframe.rb', line 8

def size
  @size
end

#vectorsObject (readonly)

Returns the value of attribute vectors.



4
5
6
# File 'lib/daru/dataframe.rb', line 4

def vectors
  @vectors
end

Class Method Details

.from_csv(file, opts = {}) {|csv| ... } ⇒ Object

Yields:

  • (csv)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/daru/dataframe.rb', line 27

def self.from_csv file, opts={}
  opts[:col_sep]           ||= ','
  opts[:headers]           ||= true
  opts[:converters]        ||= :numeric
  opts[:header_converters] ||= :symbol

  csv = CSV.open file, 'r', opts

  yield csv if block_given?

  first = true
  df    = nil

  csv.each do |row|
    if first
      df = Daru::DataFrame.new({}, csv.headers)
      first = false
    end

    df.insert_row row
  end

  df
end

Instance Method Details

#==(other) ⇒ Object



84
85
86
87
# File 'lib/daru/dataframe.rb', line 84

def ==(other)
  @name == other.name and @vectors == other.vectors and 
  @size == other.size and @fields  == other.fields 
end

#[](*name) ⇒ Object

end



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/daru/dataframe.rb', line 69

def [](*name)
  unless name[1]
    return column(name[0])
  end

  h = {}
  req_fields = @fields & name

  req_fields.each do |f|
    h[f] = @vectors[f]
  end

  DataFrame.new h, req_fields, @name
end

#[]=(name, vector) ⇒ Object



89
90
91
# File 'lib/daru/dataframe.rb', line 89

def []=(name, vector)
  insert_vector name, vector
end

#column(name) ⇒ Object



52
53
54
# File 'lib/daru/dataframe.rb', line 52

def column name
  @vectors[name]
end

#delete(name) ⇒ Object



56
57
58
59
# File 'lib/daru/dataframe.rb', line 56

def delete name
  @vectors.delete name
  @fields.delete name
end

#each_columnObject



124
125
126
127
128
129
130
# File 'lib/daru/dataframe.rb', line 124

def each_column
  @fields.each do |field|
    yield @vectors[field]
  end

  self
end

#each_column_with_nameObject



132
133
134
135
136
137
138
# File 'lib/daru/dataframe.rb', line 132

def each_column_with_name
  @fields.each do |field|
    yield @vectors[field], field
  end

  self
end

#each_rowObject



108
109
110
111
112
113
114
# File 'lib/daru/dataframe.rb', line 108

def each_row
  0.upto(@size-1) do |index|
    yield row(index)
  end

  self
end

#each_row_with_indexObject



116
117
118
119
120
121
122
# File 'lib/daru/dataframe.rb', line 116

def each_row_with_index
  0.upto(@size-1) do |index|
    yield row(index), index
  end

  self
end

#has_vector?(vector) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/daru/dataframe.rb', line 104

def has_vector? vector
  !!@vectors[vector]
end

#insert_row(row) ⇒ Object

Raises:

  • (Exception)


148
149
150
151
152
153
154
155
# File 'lib/daru/dataframe.rb', line 148

def insert_row row
  raise Exception, "Expected new row to same as the number of rows \ 
    in the DataFrame" if row.size != @fields.size

  @fields.each_with_index do |field, index|
    @vectors[field] << row[index]
  end
end

#insert_vector(name, vector) ⇒ Object

Raises:

  • (Exeception)


140
141
142
143
144
145
146
# File 'lib/daru/dataframe.rb', line 140

def insert_vector name, vector
  raise Exeception, "Expected vector size to be same as DataFrame\ 
    size." if vector.size != self.size

  @vectors.merge({name => vector})
  @fields << name
end

#row(index) ⇒ Object

Raises:

  • (Exception)


93
94
95
96
97
98
99
100
101
102
# File 'lib/daru/dataframe.rb', line 93

def row index
  raise Exception, "Expected index to be within bounds" if index > @size

  row = []
  self.each_column do |column|
    row << column[index]
  end

  row
end

#to_html(threshold = 15) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/daru/dataframe.rb', line 157

def to_html(threshold=15)
  html = '<table>'

  html += '<tr>'
  @fields.each { |f| html.concat('<td>' + f.to_s + '</td>') }
  html += '</tr>'

  self.each_row_with_index do |row, index|
    break if index > threshold and index <= @size
    html += '<tr>'
    row.each{ |val| html.concat('<td>' + val.to_s + '</td>') }
    html += '</tr>'
    if index == threshold
      html += '<tr>'
      row.size.times { html.concat('<td>...</td>') }
      html += '</tr>'
    end
  end

  html += '</table>'
end

#to_sObject



179
180
181
# File 'lib/daru/dataframe.rb', line 179

def to_s
  to_html
end