Module: Yomise
- Defined in:
- lib/yomise.rb,
lib/yomise/version.rb,
sig/yomise.rbs
Overview
write_excelでDF横並び、縦並びも可にしたい
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
"0.1.6"
Class Method Summary collapse
- .available(data, truevalue: true, falsevalue: false, blank_str_is_false: true) ⇒ Object
-
.is_available(value, truevalue: true, falsevalue: false, blank_str_is_false: true) ⇒ Object
Rover用: nil や nanを取り除くマスク、または二値の列(true-false value)生成.
- .read(path, **opt) ⇒ Object
-
.read_csv(path, format: :rover, encoding: "utf-8", liberal_parsing: true, reconvert_utf8: false, col_sep: ",", index: nil, **opt) ⇒ Object
##Generate Array from CSV File, and convert it to Hash or DataFrame.
-
.read_excel(path, sheet_i: 0, format: :rover, encoding: "utf-8", index: nil, **opt) ⇒ Object
##Generate Array from EXCEL File, and convert it to Hash or DataFrame.
- .recognize_type(str, expected) ⇒ Object
-
.to_df(d, format: :rover) ⇒ Object
Convert Hash to DataFrame.
-
.to_hash(array2d, line_from: 1, line_until: nil, line_ignored: nil, column_from: nil, column_until: nil, header: 0, symbol_header: false, replaced_by_nil: [], analyze_type: true, index: nil) ⇒ Object
Convert 2d Array to Hash header: nil -> Default Headers(:column1, column2,...) are generated.
- .write_excel(dfs, filename, sheetnames: nil) ⇒ Object
Class Method Details
.available(data, truevalue: true, falsevalue: false, blank_str_is_false: true) ⇒ Object
212 213 214 215 216 217 218 219 |
# File 'lib/yomise.rb', line 212 def available(data, truevalue: true, falsevalue: false, blank_str_is_false: true) if data.is_a? Rover::Vector data.map { |v| is_available(v, truevalue: truevalue, falsevalue: falsevalue, blank_str_is_false: blank_str_is_false) } elsif data.is_a? Rover::DataFrame dfdata = data.keys.map {|k| data[k].map { |d| is_available(d, truevalue: truevalue, falsevalue: falsevalue, blank_str_is_false: blank_str_is_false) } } Rover::DataFrame.new(data.keys.zip(dfdata).map{[_1, _2]}.to_h) end end |
.is_available(value, truevalue: true, falsevalue: false, blank_str_is_false: true) ⇒ Object
Rover用: nil や nanを取り除くマスク、または二値の列(true-false value)生成
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/yomise.rb', line 194 def is_available(value, truevalue: true, falsevalue: false, blank_str_is_false: true) if value.nil? falsevalue else if value.is_a? Numeric !value.nan? ? truevalue : falsevalue elsif value.is_a? String if value == "" blank_str_is_false ? falsevalue : truevalue else truevalue end else truevalue end end end |
.read(path, **opt) ⇒ Object
27 28 29 |
# File 'lib/yomise.rb', line 27 def read(path, **opt) return /csv$/i === path ? read_csv(path, **opt) : read_excel(path, **opt) end |
.read_csv(path, format: :rover, encoding: "utf-8", liberal_parsing: true, reconvert_utf8: false, col_sep: ",", index: nil, **opt) ⇒ Object
##Generate Array from CSV File, and convert it to Hash or DataFrame. **opt candidate= line_from: 1, header: 0
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 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/yomise.rb', line 33 def read_csv(path, format: :rover, encoding: "utf-8", liberal_parsing: true, reconvert_utf8: false, col_sep: ",", index: nil, **opt) ## TODO.. index: option that designate column number to generate DF index. ## That is, revicing set_index method. # Get 2D Array begin if liberal_parsing csvd = CSV.read(path, encoding: encoding, liberal_parsing: true) if encoding.to_s.downcase != "utf-8" csv = csvd.to_a.map {|l| l.map {|cell| cell.nil? ? nil : cell.encode("utf-8", invalid: :replace, replace: '') }} else csv = csvd end encoding = "utf-8" else # Old style (Not Recommended) # This "&:read" is not Yomise's function(defined avobe here).. parhaps File's method. csv = CSV.parse(File.open(path, encoding: encoding, &:read), col_sep: col_sep) end rescue # Try Another Encoding ## puts "Fail Encoding #{encoding}. Trying cp932..." if liberal_parsing csvd = CSV.read(path, encoding: "cp932", liberal_parsing: true) if encoding.to_s.downcase != "utf-8" csv = csvd.to_a.map {|l| l.map {|cell| cell.nil? ? nil : cell.encode("utf-8", invalid: :replace, replace: '') }} else csv = csvd end encoding = "UTF-8" else # Old style (Not Recommended) # This "&:read" is not Yomise's function(defined avobe here).. parhaps File's method. csv = CSV.parse(File.open(path, encoding: "cp932", &:read), col_sep: col_sep) end encoding = "cp932" end if reconvert_utf8 csv = csv.map {|l| l.map {|cell| cell.nil? ? nil : cell.encode("UTF-8")}} end if format.to_s == "array" return csv elsif format.to_s == "hash" h, i = to_hash(csv, **opt) return h elsif format.to_s == "csv" return csv.to_csv elsif format.to_s == "numo" return csv # Under Construction else # include format.nil? (in this case, convert to Daru::DF). h, ind_orig = to_hash(csv, index: index, **opt) ans = to_df(h, format: format) # Converting Encode and Setting index.. rover not supported yet if format.to_s == "daru" ans.convert_enc!(from: encoding, to: "utf-8") if encoding.to_s.downcase != "utf-8" begin ans.index = ind_orig if index rescue warn "Indexing failed (Parhaps due to duplicated index)." end end return ans end end |
.read_excel(path, sheet_i: 0, format: :rover, encoding: "utf-8", index: nil, **opt) ⇒ Object
##Generate Array from EXCEL File, and convert it to Hash or DataFrame. **opt candidate= line_from: 1, header: 0)
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/yomise.rb', line 107 def read_excel(path, sheet_i: 0, format: :rover, encoding: "utf-8", index: nil, **opt) a2d = open_excel(path, sheet_i, encoding: encoding) # Get 2D Array if format.to_s == "array" return a2d elsif format.to_s == "hash" h, i = to_hash(a2d, **opt) return h elsif format.to_s == "csv" return a2d.to_csv elsif format.to_s == "numo" return a2d # Under Construction else # include format.nil? h, ind_orig = to_hash(a2d, index: index, **opt) ans = to_df(h, format: format) if format.to_s == "daru" begin ans.index = ind_orig if index rescue warn "Indexing failed (Parhaps due to duplicated index)." end end return ans end end |
.recognize_type(str, expected) ⇒ Object
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/yomise.rb', line 315 def recognize_type(str, expected) return expected if str.nil? order = {:any => 0, :int => 1, :float => 2, :string => 3} if /^\s*(-|\+)?\d+\s*$/ === str type_of_str = :int elsif /^\s*(-|\+)?\d*\.\d*\s*$/ === str || /^\s*(-|\+)?(\d*\.\d+|\d+)(e|E)(-|\+)?\d+\s*$/ === str type_of_str = :float else type_of_str = :string end # p "#{type_of_str}, #{str}" if order[type_of_str] > order[expected] return order[type_of_str] > order[expected] ? type_of_str : expected end |
.to_df(d, format: :rover) ⇒ Object
Convert Hash to DataFrame
185 186 187 188 189 190 191 |
# File 'lib/yomise.rb', line 185 def to_df(d, format: :rover) if format.to_s == "daru" Daru::DataFrame.new(d) else Rover::DataFrame.new(d) end end |
.to_hash(array2d, line_from: 1, line_until: nil, line_ignored: nil, column_from: nil, column_until: nil, header: 0, symbol_header: false, replaced_by_nil: [], analyze_type: true, index: nil) ⇒ Object
Convert 2d Array to Hash header: nil -> Default Headers(:column1, column2,...) are generated. Option line_ignored, is not implemented yet.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/yomise.rb', line 136 def to_hash(array2d, line_from: 1, line_until: nil, line_ignored: nil, column_from: nil, column_until: nil, header: 0, symbol_header: false, replaced_by_nil: [], analyze_type: true, index: nil) ## TODO.. column_from: , column_until: # Define Read Range------------ lfrom, luntil = line_from, line_until lf_reg, lu_reg = line_from.kind_of?(Regexp), line_until.kind_of?(Regexp) if lf_reg || lu_reg lines_ary = array2d.map{ _1.join "," } lfrom = lines_ary.find_index{ line_from === _1 } if lf_reg luntil = (lines_ary.length-1) - lines_ary.reverse.find_index{ line_until === _1 } if lu_reg end # And get originally array----- output = array2d[lfrom...luntil] # ----------------------------- # Then get data of index------- ind_orig = index ? output.map{ _1[index] } : nil # ----------------------------- # Selecct Column--------------- output = output.map { _1[column_from...column_until] } if column_from || column_until # Define Data Array------------ output_transpose = output[0].zip(*output[1..]) output_transpose = fix_array(output_transpose, replaced_by_nil, analyze_type) # ----------------------------- # Define Header---------------- if header hd = check_header(array2d[header])[column_from...column_until] else hd = [*0...(output.longest_line)].map{"column#{_1}"} end # hd = header.nil? ? [*0...(output.longest_line)].map{"column#{_1}"} : check_header(array2d[header]) hd = hd.map { _1.intern } if symbol_header # ----------------------------- # Make Hash(Header => Data Array) return hd.each_with_object({}).with_index {|(hdr, hash), i| hash[hdr]=output_transpose[i]}, ind_orig end |
.write_excel(dfs, filename, sheetnames: nil) ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/yomise.rb', line 258 def write_excel(dfs, filename, sheetnames: nil) dfs = [dfs] if !dfs.is_a?(Array) if sheetnames.nil? sheetnames = dfs.map.with_index {|d, i| "sheet#{i+1}" } end workbook = RubyXL::Workbook.new dfs.each_with_index do |df, idx| worksheet = workbook.add_worksheet( sheetnames.length <= idx ? "sheet#{idx}" : sheetnames[idx].to_s ) df.to_h.each_with_index do |(key, itms), coli| worksheet.add_cell(0, coli, key) itms.each_with_index do |itm, linei| worksheet.add_cell(linei+1, coli, itm.to_s) end end end workbook.worksheets.delete_at(0) workbook.write(filename) end |