yomise 0.1.0 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2b7a8b8f154e4e7353ac87bbe7f87677a11b9b4d9fb92843cbe43d69f1ad55f6
4
- data.tar.gz: 33635b64c5b492dff53353d98d4c527948e716def4faa90ada74c880a77c9f4c
3
+ metadata.gz: 6987f2d46203deb734420f8aa9b08f076db6792890ea1a080ff58d603be9b05c
4
+ data.tar.gz: 8ebe3864cb7f76d79f5efe48a68f71af96800ed11600e44401e7871554ebdf87
5
5
  SHA512:
6
- metadata.gz: cba1da8161b7378a39f0c6f1f529e579e425938d942ceb9e327e7af815d9f59344473881f53cf06c8dba90ce4c978248a2f0222ee2f7f04fe20174aadf4bfc9a
7
- data.tar.gz: 7928f92a011a14e83c4a51e971467ad423f40a204ee7e1fc63b4c86e6c8619ab8aa842b1d13c7a8b2814ff49cd887f9e55d8ca0aace320f1601f8fcc3706ba4c
6
+ metadata.gz: f30da92c73465ec1904b4466c396bb05062fe943666af091a46db66a3b6b0b3e41d7198d4b0b14ea0b37a489d0cfeb2f295f8b3ea35829640d6d929b8ff40a52
7
+ data.tar.gz: 56d101656e8d2b202d2dbaa2a3dacdabb993efbe5661e6e7682faa0d230fb1dfe4cbbf77c4ab4ef3392f2ac7eee50a2e6305d42b61c4e423441b47e1c13d604c
data/lib/to_csv.rb CHANGED
@@ -8,7 +8,7 @@ class Daru::DataFrame
8
8
  ans = self.map(&:name).join ","
9
9
  self.to_a[0].each do |item|
10
10
  ans += "\n"
11
- ans += item.map{|k, v| v}.join(",")
11
+ ans += item.map{|k, v| "\"#{v}\""}.join(",")
12
12
  end
13
13
 
14
14
  return ans
@@ -58,6 +58,29 @@ class Daru::DataFrame
58
58
  self.vectors = Daru::Index.new(vector_names_ary)
59
59
  self.index = Daru::Vector.new(self.index.to_a.map{_1[0]})
60
60
  end
61
+
62
+ def simple_pivot(index, vectors, values, agg: :mean, index_name: nil)
63
+
64
+ # index, vectors are Arrays. 'values' is String or Array.
65
+ ## 文字列データなどで最初のデータだけ欲しければ agg: :first
66
+ piv = self.pivot_table index: index, vectors: vectors, agg: :mean, values: values
67
+ piv.vectors = Daru::Index.new( piv.vectors.to_a.map { _1.join("-") } )
68
+ piv.index = Daru::Vector.new( piv.index.to_a.map { _1.join("-") } )
69
+
70
+ # indexを新しく追加
71
+ index_name ||= "Pivot_Index"
72
+ piv[index_name] = piv.index
73
+
74
+ # 順番変更
75
+ piv.order = [piv.vectors.to_a[-1]] + piv.vectors.to_a[0..-2]
76
+
77
+ return piv
78
+
79
+ end
80
+
81
+ def to_rover
82
+ Rover::DataFrame.new(self.to_a[0])
83
+ end
61
84
 
62
85
  alias_method :addvec, :add_vector
63
86
  end
@@ -69,4 +92,21 @@ class Rover::DataFrame
69
92
  enc = encoding.nil? ? "" : ":#{encoding}"
70
93
  open(path, "w#{enc}") {|f| f.write self.to_csv}
71
94
  end
95
+
96
+ def to_daru
97
+ Daru::DataFrame.new(self.to_a)
98
+ end
99
+
100
+ def simple_pivot(index, vectors, ...)
101
+ ddr = self.to_daru
102
+ piv = ddr.simple_pivot(index, vectors, ...)
103
+ return piv.to_rover
104
+ end
105
+
106
+ def outer_join
107
+ ddr = self.to_daru
108
+ # j = ddr.join ## 外部結合
109
+ return j.to_rover
110
+ end
111
+
72
112
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Yomise
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.3"
5
5
  end
data/lib/yomise.rb CHANGED
@@ -14,23 +14,49 @@ module Yomise
14
14
  module_function
15
15
 
16
16
  def read(path, **opt)
17
- return /csv$/ === path ? read_csv(path, **opt) : read_excel(path, **opt)
17
+ return /csv$/i === path ? read_csv(path, **opt) : read_excel(path, **opt)
18
18
  end
19
19
 
20
20
  # ##Generate Array from CSV File, and convert it to Hash or DataFrame.
21
21
  # **opt candidate= line_from: 1, header: 0
22
22
  # ver. 0.3.8~ default format=:daru
23
- def read_csv(path, format: :daru, encoding: "utf-8", col_sep: ",", index: nil, **opt)
23
+ def read_csv(path, format: :daru, encoding: "utf-8", liberal_parsing: true, col_sep: ",", index: nil, **opt)
24
24
  ## TODO.. index: option that designate column number to generate DF index.
25
25
  ## That is, revicing set_index method.
26
26
 
27
27
  # Get 2D Array
28
28
  begin
29
- csv = CSV.parse(File.open(path, encoding: encoding, &:read), col_sep: col_sep)
29
+ if liberal_parsing
30
+ csvd = CSV.read(path, encoding: encoding, liberal_parsing: true)
31
+ if encoding.to_s.downcase != "utf-8"
32
+ csv = csvd.to_a.map {|l| l.map {|cell| cell.nil? ? nil : cell.encode("utf-8", invalid: :replace, replace: '') }}
33
+ else
34
+ csv = csvd
35
+ end
36
+
37
+ encoding = "utf-8"
38
+ else
39
+ # Old style (Not Recommended)
40
+ # This "&:read" is not Yomise's function(defined avobe here).. parhaps File's method.
41
+ csv = CSV.parse(File.open(path, encoding: encoding, &:read), col_sep: col_sep)
42
+ end
30
43
  rescue
31
44
  # Try Another Encoding
32
45
  ## puts "Fail Encoding #{encoding}. Trying cp932..."
33
- csv = CSV.parse(File.open(path, encoding: "cp932", &:read), col_sep: col_sep)
46
+ if liberal_parsing
47
+ csvd = CSV.read(path, encoding: "cp932", liberal_parsing: true)
48
+ if encoding.to_s.downcase != "utf-8"
49
+ csv = csvd.to_a.map {|l| l.map {|cell| cell.nil? ? nil : cell.encode("utf-8", invalid: :replace, replace: '') }}
50
+ else
51
+ csv = csvd
52
+ end
53
+
54
+ encoding = "UTF-8"
55
+ else
56
+ # Old style (Not Recommended)
57
+ # This "&:read" is not Yomise's function(defined avobe here).. parhaps File's method.
58
+ csv = CSV.parse(File.open(path, encoding: "cp932", &:read), col_sep: col_sep)
59
+ end
34
60
  encoding = "cp932"
35
61
  end
36
62
 
@@ -39,6 +65,10 @@ module Yomise
39
65
  elsif format.to_s == "hash"
40
66
  h, i = to_hash(csv, **opt)
41
67
  return h
68
+ elsif format.to_s == "csv"
69
+ return csv.to_csv
70
+ elsif format.to_s == "numo"
71
+ return csv # Under Construction
42
72
  else # include format.nil? (in this case, convert to Daru::DF).
43
73
 
44
74
  h, ind_orig = to_hash(csv, index: index, **opt)
@@ -46,7 +76,7 @@ module Yomise
46
76
 
47
77
  # Converting Encode and Setting index.. rover not supported yet
48
78
  if format.to_s == "daru" || format.nil?
49
- ans.convert_enc!(from: encoding, to: "utf-8")
79
+ ans.convert_enc!(from: encoding, to: "utf-8") if encoding.to_s.downcase != "utf-8"
50
80
  begin
51
81
  ans.index = ind_orig if index
52
82
  rescue
@@ -68,6 +98,10 @@ module Yomise
68
98
  elsif format.to_s == "hash"
69
99
  h, i = to_hash(a2d, **opt)
70
100
  return h
101
+ elsif format.to_s == "csv"
102
+ return a2d.to_csv
103
+ elsif format.to_s == "numo"
104
+ return a2d # Under Construction
71
105
  else # include format.nil?
72
106
  h, ind_orig = to_hash(a2d, index: index, **opt)
73
107
  ans = to_df(h, format: format)
@@ -148,7 +182,7 @@ module Yomise
148
182
 
149
183
  # Genarate Array from excel file
150
184
  def open_excel(path, sheet_i, encoding: "utf-8")
151
- if /xlsx$/ === path
185
+ if /xls(x|m)$/ === path
152
186
  puts "Sorry, encoding option is not supported yet for xlsx file." if encoding != "utf-8"
153
187
 
154
188
  book = Roo::Excelx.new(path)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yomise
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - showata
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-11-25 00:00:00.000000000 Z
11
+ date: 2025-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: daru