Module: DaruLite::DataFrame::Convertible
- Included in:
- DaruLite::DataFrame
- Defined in:
- lib/daru_lite/data_frame/convertible.rb
Instance Method Summary collapse
-
#create_sql(table, charset = 'UTF8') ⇒ Object
Create a sql, basen on a given Dataset.
-
#to_a ⇒ Object
Converts the DataFrame into an array of hashes where key is vector name and value is the corresponding element.
-
#to_df ⇒ self
Returns the dataframe.
-
#to_h ⇒ Object
Converts DataFrame to a hash (explicit) with keys as vector names and values as the corresponding vectors.
-
#to_html(threshold = DaruLite.max_rows) ⇒ Object
Convert to html for IRuby.
- #to_html_tbody(threshold = DaruLite.max_rows) ⇒ Object
- #to_html_thead ⇒ Object
-
#to_json(no_index = true) ⇒ Object
Convert to json.
-
#to_matrix ⇒ Object
Convert all vectors of type :numeric into a Matrix.
- #to_s ⇒ Object
Instance Method Details
#create_sql(table, charset = 'UTF8') ⇒ Object
Create a sql, basen on a given Dataset
Arguments
-
table - String specifying name of the table that will created in SQL.
-
charset - Character set. Default is “UTF8”.
20 21 22 23 24 25 26 27 28 |
# File 'lib/daru_lite/data_frame/convertible.rb', line 20 def create_sql(table, charset = 'UTF8') sql = "CREATE TABLE #{table} (" fields = vectors.to_a.collect do |f| v = self[f] "#{f} #{v.db_type}" end sql + fields.join(",\n ") + ") CHARACTER SET=#{charset};" end |
#to_a ⇒ Object
Converts the DataFrame into an array of hashes where key is vector name and value is the corresponding element. The 0th index of the array contains the array of hashes while the 1th index contains the indexes of each row of the dataframe. Each element in the index array corresponds to its row in the array of hashes, which has the same index.
47 48 49 |
# File 'lib/daru_lite/data_frame/convertible.rb', line 47 def to_a [each_row.map(&:to_h), @index.to_a] end |
#to_df ⇒ self
Returns the dataframe. This can be convenient when the user does not know whether the object is a vector or a dataframe.
33 34 35 |
# File 'lib/daru_lite/data_frame/convertible.rb', line 33 def to_df self end |
#to_h ⇒ Object
Converts DataFrame to a hash (explicit) with keys as vector names and values as the corresponding vectors.
63 64 65 66 67 |
# File 'lib/daru_lite/data_frame/convertible.rb', line 63 def to_h @vectors .each_with_index .map { |vec_name, idx| [vec_name, @data[idx]] }.to_h end |
#to_html(threshold = DaruLite.max_rows) ⇒ Object
Convert to html for IRuby.
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/daru_lite/data_frame/convertible.rb', line 70 def to_html(threshold = DaruLite.max_rows) table_thead = to_html_thead table_tbody = to_html_tbody(threshold) path = if index.is_a?(MultiIndex) File.('../iruby/templates/dataframe_mi.html.erb', __dir__) else File.('../iruby/templates/dataframe.html.erb', __dir__) end ERB.new(File.read(path).strip).result(binding) end |
#to_html_tbody(threshold = DaruLite.max_rows) ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/daru_lite/data_frame/convertible.rb', line 91 def to_html_tbody(threshold = DaruLite.max_rows) threshold ||= @size table_tbody_path = if index.is_a?(MultiIndex) File.('../iruby/templates/dataframe_mi_tbody.html.erb', __dir__) else File.('../iruby/templates/dataframe_tbody.html.erb', __dir__) end ERB.new(File.read(table_tbody_path).strip).result(binding) end |
#to_html_thead ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/daru_lite/data_frame/convertible.rb', line 81 def to_html_thead table_thead_path = if index.is_a?(MultiIndex) File.('../iruby/templates/dataframe_mi_thead.html.erb', __dir__) else File.('../iruby/templates/dataframe_thead.html.erb', __dir__) end ERB.new(File.read(table_thead_path).strip).result(binding) end |
#to_json(no_index = true) ⇒ Object
Convert to json. If no_index is false then the index will NOT be included in the JSON thus created.
53 54 55 56 57 58 59 |
# File 'lib/daru_lite/data_frame/convertible.rb', line 53 def to_json(no_index = true) if no_index to_a[0].to_json else to_a.to_json end end |
#to_matrix ⇒ Object
Convert all vectors of type :numeric into a Matrix.
38 39 40 |
# File 'lib/daru_lite/data_frame/convertible.rb', line 38 def to_matrix Matrix.columns each_vector.select(&:numeric?).map(&:to_a) end |
#to_s ⇒ Object
102 103 104 |
# File 'lib/daru_lite/data_frame/convertible.rb', line 102 def to_s "#<#{self.class}#{": #{@name}" if @name}(#{nrows}x#{ncols})>" end |