Class: Daru::DataFrame
- Inherits:
-
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
141
142
143
144
145
146
147
148
149
|
# File 'lib/daru/dataframe.rb', line 141
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
#fields ⇒ Object
Returns the value of attribute fields.
6
7
8
|
# File 'lib/daru/dataframe.rb', line 6
def fields
@fields
end
|
#name ⇒ Object
Returns the value of attribute name.
10
11
12
|
# File 'lib/daru/dataframe.rb', line 10
def name
@name
end
|
#size ⇒ Object
Returns the value of attribute size.
8
9
10
|
# File 'lib/daru/dataframe.rb', line 8
def size
@size
end
|
#vectors ⇒ Object
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
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.)
first = false
end
df.insert_row row
end
df
end
|
Instance Method Details
#[](name) ⇒ Object
61
62
63
|
# File 'lib/daru/dataframe.rb', line 61
def [](name)
column name
end
|
#[]=(name, vector) ⇒ Object
65
66
67
|
# File 'lib/daru/dataframe.rb', line 65
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_column ⇒ Object
96
97
98
99
100
|
# File 'lib/daru/dataframe.rb', line 96
def each_column
@vectors.values.each do |column|
yield column
end
end
|
#each_row ⇒ Object
84
85
86
87
88
|
# File 'lib/daru/dataframe.rb', line 84
def each_row
0.upto(@size) do |index|
yield row(index)
end
end
|
#each_row_with_index ⇒ Object
90
91
92
93
94
|
# File 'lib/daru/dataframe.rb', line 90
def each_row_with_index
0.upto(@size) do |index|
yield row(index), index
end
end
|
#has_vector?(vector) ⇒ Boolean
80
81
82
|
# File 'lib/daru/dataframe.rb', line 80
def has_vector? vector
!!@vectors[vector]
end
|
#insert_row(row) ⇒ Object
110
111
112
113
114
115
116
117
|
# File 'lib/daru/dataframe.rb', line 110
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
102
103
104
105
106
107
108
|
# File 'lib/daru/dataframe.rb', line 102
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
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/daru/dataframe.rb', line 69
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/daru/dataframe.rb', line 119
def to_html(threshold=15)
html = '<table>'
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 i == threshold
html += '<tr>'
row.size.times { html.concat('<td>...</td>') }
html += '</tr>'
end
end
html += '</table>'
end
|
#to_s ⇒ Object
137
138
139
|
# File 'lib/daru/dataframe.rb', line 137
def to_s
to_html
end
|