yomise 0.1.3.1 → 0.1.5

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.
data/lib/to_csv.rb CHANGED
@@ -1,120 +1,159 @@
1
- require "daru"
2
- require "rover"
3
-
4
- class Daru::DataFrame
5
- def to_csv()
6
- a = self.to_a.transpose
7
-
8
- ans = self.map(&:name).join ","
9
- self.to_a[0].each do |item|
10
- ans += "\n"
11
- ans += item.map{|k, v| "\"#{v}\""}.join(",")
12
- end
13
-
14
- return ans
15
- end
16
-
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
28
- end
29
-
30
- # To avoid bug about adding column to Daru::DataFrame
31
- def add_vector(vecname, vec)
32
- self[vecname] = vec
33
- self.rename_vectors({vecname => vecname})
34
- end
35
-
36
- ### エンコード関連 ###
37
- # vector_i番目のヘッダー名を読めるようにエンコード
38
- def encode_vector_name(vector_i)
39
- if self.vectors.to_a[vector_i].is_a?(String)
40
- self.vectors.to_a[vector_i].encode Encoding::UTF_8, Encoding::Windows_31J
41
- end
42
- end
43
-
44
- # すべての列に対し上記を実施
45
- def encode_vectors!
46
- self.vectors = Daru::Index.new(Range.new(0, self.vectors.size-1).map {|i| encode_vector_name i })
47
- end
48
-
49
- # ver.0.3.8~ Convert Daru::DF encoding
50
- def convert_enc!(from: "cp932", to: "utf-8")
51
- self.vectors.each do |col|
52
- self[col] = self[col].each {|val| val.encode!(to, from_encoding: from) if val.is_a?(String)}
53
- end
54
-
55
- self.encode_vectors!
56
- end
57
- #####################
58
-
59
- # rover not suppoted yet about indexing
60
- def set_index!(indexcolumn)
61
- self.index = self[indexcolumn]
62
- end
63
-
64
- # To revice pivot index
65
- def simplify_multi_index(vector_names_ary)
66
- self.vectors = Daru::Index.new(vector_names_ary)
67
- self.index = Daru::Vector.new(self.index.to_a.map{_1[0]})
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
92
-
93
- alias_method :addvec, :add_vector
94
- end
95
-
96
- class Rover::DataFrame
97
- # Rover#to_csv is already exist.
98
-
99
- def write_csv(path, encoding: nil)
100
- enc = encoding.nil? ? "" : ":#{encoding}"
101
- open(path, "w#{enc}") {|f| f.write self.to_csv}
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
-
120
- end
1
+ require "daru"
2
+ require "rover"
3
+
4
+ class Daru::DataFrame
5
+ def to_csv()
6
+ a = self.to_a.transpose
7
+
8
+ ans = self.map(&:name).join ","
9
+ self.to_a[0].each do |item|
10
+ ans += "\n"
11
+ ans += item.map{|k, v| "\"#{v}\""}.join(",")
12
+ end
13
+
14
+ return ans
15
+ end
16
+
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
28
+ end
29
+
30
+ # To avoid bug about adding column to Daru::DataFrame
31
+ def add_vector(vecname, vec)
32
+ self[vecname] = vec
33
+ self.rename_vectors({vecname => vecname})
34
+ end
35
+
36
+ ### エンコード関連 ###
37
+ # vector_i番目のヘッダー名を読めるようにエンコード
38
+ def encode_vector_name(vector_i)
39
+ if self.vectors.to_a[vector_i].is_a?(String)
40
+ self.vectors.to_a[vector_i].encode Encoding::UTF_8, Encoding::Windows_31J
41
+ end
42
+ end
43
+
44
+ # すべての列に対し上記を実施
45
+ def encode_vectors!
46
+ self.vectors = Daru::Index.new(Range.new(0, self.vectors.size-1).map {|i| encode_vector_name i })
47
+ end
48
+
49
+ # ver.0.3.8~ Convert Daru::DF encoding
50
+ def convert_enc!(from: "cp932", to: "utf-8")
51
+ self.vectors.each do |col|
52
+ self[col] = self[col].each {|val| val.encode!(to, from_encoding: from) if val.is_a?(String)}
53
+ end
54
+
55
+ self.encode_vectors!
56
+ end
57
+ #####################
58
+
59
+ # rover not suppoted yet about indexing
60
+ def set_index!(indexcolumn)
61
+ self.index = self[indexcolumn]
62
+ end
63
+
64
+ # To revice pivot index
65
+ def simplify_multi_index(vector_names_ary)
66
+ self.vectors = Daru::Index.new(vector_names_ary)
67
+ self.index = Daru::Vector.new(self.index.to_a.map{_1[0]})
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
92
+
93
+ alias_method :addvec, :add_vector
94
+ end
95
+
96
+ class Rover::DataFrame
97
+ # Rover#to_csv is already exist.
98
+
99
+ def write_csv(path, encoding: nil)
100
+ enc = encoding.nil? ? "" : ":#{encoding}"
101
+ open(path, "w#{enc}") {|f| f.write self.to_csv}
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(other, **kwargs)
115
+ ddr = self.to_daru
116
+ odr = self.to_daru
117
+ j = ddr.join(how: :outer, **kwargs) ## 外部結合
118
+ return j.to_rover
119
+ end
120
+
121
+ def categorize(col)
122
+ if col.is_a?(Array)
123
+ self[col].to_a.uniq
124
+ else
125
+ self[[col]].to_a.uniq
126
+ end
127
+ end
128
+
129
+ def get_category(categ)
130
+ a = self[categ.each_key.to_a].to_a
131
+ mask = a.filter_map.with_index {|v, i| i if v == categ }
132
+ return self[mask]
133
+ end
134
+
135
+ ## Roverのleft_joinをエラー回避
136
+ def left_join_rev(other, on:)
137
+ left_rows = self.to_a
138
+ right_rows = other.to_a
139
+
140
+ right_map = right_rows.each_with_object({}) do |row, map|
141
+ map[row[on]] = row
142
+ end
143
+
144
+ result_rows = []
145
+
146
+ left_rows.each do |l_row|
147
+ key = l_row[on]
148
+ r_row = right_map[key]
149
+
150
+ if r_row
151
+ result_rows << l_row.merge(r_row)
152
+ else
153
+ result_rows << l_row
154
+ end
155
+ end
156
+
157
+ Rover::DataFrame.new(result_rows)
158
+ end
159
+ end
@@ -1,5 +1,5 @@
1
- # frozen_string_literal: true
2
-
3
- module Yomise
4
- VERSION = "0.1.3.1"
5
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Yomise
4
+ VERSION = "0.1.5"
5
+ end