Module: Daru::IO
- Defined in:
- lib/daru/io/io.rb
Class Method Summary collapse
- .dataframe_write_csv(dataframe, path, opts = {}) ⇒ Object
- .dataframe_write_excel(dataframe, path, opts = {}) ⇒ Object
- .dataframe_write_sql(ds, dbh, table) ⇒ Object
-
.from_csv(path, opts = {}) ⇒ Object
Functions for loading/writing CSV files.
-
.from_excel(path, opts = {}) ⇒ Object
Functions for loading/writing Excel files.
-
.from_plaintext(filename, fields) ⇒ Object
Loading data from plain text files.
-
.from_sql(dbh, query) ⇒ Object
Loading/writing from SQL databases.
- .load(filename) ⇒ Object
-
.save(klass, filename) ⇒ Object
Loading and writing Marshalled DataFrame/Vector.
Class Method Details
.dataframe_write_csv(dataframe, path, opts = {}) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/daru/io/io.rb', line 105 def dataframe_write_csv dataframe, path, opts={} = { converters: :numeric }.merge(opts) writer = ::CSV.open(path, 'w', ) writer << dataframe.vectors.to_a dataframe.each_row do |row| if [:convert_comma] writer << row.map { |v| v.to_s.gsub('.', ',') } else writer << row.to_a end end writer.close end |
.dataframe_write_excel(dataframe, path, opts = {}) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/daru/io/io.rb', line 44 def dataframe_write_excel dataframe, path, opts={} book = Spreadsheet::Workbook.new sheet = book.create_worksheet format = Spreadsheet::Format.new :color => :blue, :weight => :bold sheet.row(0).concat(dataframe.vectors.to_a.map(&:to_s)) # Unfreeze strings sheet.row(0).default_format = format i = 1 dataframe.each_row do |row| sheet.row(i).concat(row.to_a) i += 1 end book.write(path) end |
.dataframe_write_sql(ds, dbh, table) ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/daru/io/io.rb', line 144 def dataframe_write_sql ds, dbh, table require 'dbi' query = "INSERT INTO #{table} ("+ds.vectors.to_a.join(",")+") VALUES ("+((["?"]*ds.vectors.size).join(","))+")" sth = dbh.prepare(query) ds.each_row { |c| sth.execute(*c.to_a) } return true end |
.from_csv(path, opts = {}) ⇒ Object
Functions for loading/writing CSV files
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/daru/io/io.rb', line 62 def from_csv path, opts={} opts[:col_sep] ||= ',' opts[:converters] ||= :numeric = opts.keys.inject({}) do |hash, k| if [:clone, :order, :index, :name].include?(k) hash[k] = opts[k] opts.delete k end hash end # Preprocess headers for detecting and correcting repetition in # case the :headers option is not specified. unless opts[:headers] csv = ::CSV.open(path, 'rb', opts) yield csv if block_given? csv_as_arrays = csv.to_a headers = csv_as_arrays[0].recode_repeated.map(&:to_sym) csv_as_arrays.delete_at 0 csv_as_arrays = csv_as_arrays.transpose hsh = {} headers.each_with_index do |h, i| hsh[h] = csv_as_arrays[i] end else opts[:header_converters] ||= :symbol csv = ::CSV.read(path, 'rb',opts) yield csv if block_given? hsh = {} csv.by_col.each do |col_name, values| hsh[col_name] = values end end Daru::DataFrame.new(hsh,) end |
.from_excel(path, opts = {}) ⇒ Object
Functions for loading/writing Excel files.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/daru/io/io.rb', line 24 def from_excel path, opts={} opts = { :worksheet_id => 0, }.merge opts worksheet_id = opts[:worksheet_id] book = Spreadsheet.open path worksheet = book.worksheet worksheet_id headers = worksheet.row(0).recode_repeated.map(&:to_sym) df = Daru::DataFrame.new({}) headers.each_with_index do |h,i| col = worksheet.column(i).to_a col.delete_at 0 df[h] = col end df end |
.from_plaintext(filename, fields) ⇒ Object
Loading data from plain text files
154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/daru/io/io.rb', line 154 def from_plaintext filename, fields ds = Daru::DataFrame.new({}, order: fields) fp = File.open(filename,"r") fp.each_line do |line| row = Daru::IOHelpers.process_row(line.strip.split(/\s+/),[""]) next if row == ["\x1A"] ds.add_row(row) end ds.update fields.each { |f| ds[f].rename f } ds end |
.from_sql(dbh, query) ⇒ Object
Loading/writing from SQL databases
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/daru/io/io.rb', line 126 def from_sql dbh, query require 'dbi' sth = dbh.execute(query) vectors = {} fields = [] sth.column_info.each do |c| vectors[c[:name]] = Daru::Vector.new([]) vectors[c[:name]].rename c[:name] fields.push(c[:name].to_sym) end ds=Daru::DataFrame.new(vectors,order: fields) sth.fetch do |row| ds.add_row(row.to_a) end ds.update ds end |
.load(filename) ⇒ Object
174 175 176 177 178 179 180 181 182 |
# File 'lib/daru/io/io.rb', line 174 def load filename if File.exist? filename o = false File.open(filename, 'r') { |fp| o = Marshal.load(fp) } o else false end end |
.save(klass, filename) ⇒ Object
Loading and writing Marshalled DataFrame/Vector
168 169 170 171 172 |
# File 'lib/daru/io/io.rb', line 168 def save klass, filename fp = File.open(filename, 'w') Marshal.dump(klass, fp) fp.close end |