yomise 0.1.1 → 0.1.3.1

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: 8544346549ebbc5186a149be6d0f777b8f004f2d078314b16a4d9a1ad8467bd3
4
- data.tar.gz: cafc602a5d8e25f149cdb966033d95f4833ac061435ac165f6ffa8865baf28c7
3
+ metadata.gz: 28234d39ab9fcef06703c79054cde5a96f183bff244a7a217b5f2aa2c1672f0d
4
+ data.tar.gz: 216c59fb5b5e60f0e636e8891a32bc987e378073b7609231728f9375779c412a
5
5
  SHA512:
6
- metadata.gz: a153bb6e926a94dca2634b7c69e99a40a6d393871ce9f915f8a77f7f9cecebb23e1e20dc22b785d49dbd59c5c55a4f2f69c7faf89e39093346281d1bd37162fe
7
- data.tar.gz: fc357a3c2ad6f836d47bfaa9cf628149d191b15797a52f3bc19c1e2d59b54ec620e52585514a7b9f36922a23ae87e7989f1c455a4fd3e718b39087b22205ecd9
6
+ metadata.gz: 6355f13b441d32c5f73def97805c155a343add2f343f55c0a0c40ea483e6536937529c793afdcb592b80c550a5ccf2551980f2f6462c1fa5aa54be8abc9bdfa0
7
+ data.tar.gz: '08edbe65b56af24bb6d6a06096128a04edd6fdc37e061209593096fe11e657dde41081204b2625f3e2844e0f53d1a8b765ebf4b11f94ed08ffe561d5676aad83'
data/lib/to_csv.rb CHANGED
@@ -8,15 +8,23 @@ 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
15
15
  end
16
16
 
17
- def write_csv(path, encoding: nil)
18
- enc = encoding.nil? ? "" : ":#{encoding}"
19
- open(path, "w#{enc}") { _1.write to_csv }
17
+ def write_csv(path, encoding: nil, alt: false)
18
+ if alt
19
+ # Experimental: faster algorithm
20
+ # ""付加を先にDaru上でやってあげる案
21
+
22
+ #今はとりあえず
23
+ self.to_daru.write_csv path, encoding: encoding
24
+ else
25
+ enc = encoding.nil? ? "" : ":#{encoding}"
26
+ open(path, "w#{enc}") { _1.write to_csv }
27
+ end
20
28
  end
21
29
 
22
30
  # To avoid bug about adding column to Daru::DataFrame
@@ -58,6 +66,29 @@ class Daru::DataFrame
58
66
  self.vectors = Daru::Index.new(vector_names_ary)
59
67
  self.index = Daru::Vector.new(self.index.to_a.map{_1[0]})
60
68
  end
69
+
70
+ def simple_pivot(index, vectors, values, agg: :mean, index_name: nil)
71
+
72
+ # index, vectors are Arrays. 'values' is String or Array.
73
+ ## 文字列データなどで最初のデータだけ欲しければ agg: :first
74
+ piv = self.pivot_table index: index, vectors: vectors, agg: agg, values: values
75
+ piv.vectors = Daru::Index.new( piv.vectors.to_a.map { _1.join("-") } )
76
+ piv.index = Daru::Vector.new( piv.index.to_a.map { _1.join("-") } )
77
+
78
+ # indexを新しく追加
79
+ index_name ||= "Pivot_Index"
80
+ piv[index_name] = piv.index
81
+
82
+ # 順番変更
83
+ piv.order = [piv.vectors.to_a[-1]] + piv.vectors.to_a[0..-2]
84
+
85
+ return piv
86
+
87
+ end
88
+
89
+ def to_rover
90
+ Rover::DataFrame.new(self.to_a[0])
91
+ end
61
92
 
62
93
  alias_method :addvec, :add_vector
63
94
  end
@@ -69,4 +100,21 @@ class Rover::DataFrame
69
100
  enc = encoding.nil? ? "" : ":#{encoding}"
70
101
  open(path, "w#{enc}") {|f| f.write self.to_csv}
71
102
  end
103
+
104
+ def to_daru
105
+ Daru::DataFrame.new(self.to_a)
106
+ end
107
+
108
+ def simple_pivot(index, vectors, ...)
109
+ ddr = self.to_daru
110
+ piv = ddr.simple_pivot(index, vectors, ...)
111
+ return piv.to_rover
112
+ end
113
+
114
+ def outer_join
115
+ ddr = self.to_daru
116
+ # j = ddr.join ## 外部結合
117
+ return j.to_rover
118
+ end
119
+
72
120
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Yomise
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.3.1"
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
 
@@ -50,7 +76,7 @@ module Yomise
50
76
 
51
77
  # Converting Encode and Setting index.. rover not supported yet
52
78
  if format.to_s == "daru" || format.nil?
53
- ans.convert_enc!(from: encoding, to: "utf-8")
79
+ ans.convert_enc!(from: encoding, to: "utf-8") if encoding.to_s.downcase != "utf-8"
54
80
  begin
55
81
  ans.index = ind_orig if index
56
82
  rescue
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.1
4
+ version: 0.1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - showata
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-09-19 00:00:00.000000000 Z
11
+ date: 2025-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: daru