Class: Daru::DataFrame

Inherits:
Object
  • Object
show all
Defined in:
lib/to_csv.rb

Instance Method Summary collapse

Instance Method Details

#add_vector(vecname, vec) ⇒ Object Also known as: addvec

To avoid bug about adding column to Daru::DataFrame



23
24
25
26
# File 'lib/to_csv.rb', line 23

def add_vector(vecname, vec)
	self[vecname] = vec
	self.rename_vectors({vecname => vecname})
end

#convert_enc!(from: "cp932", to: "utf-8") ⇒ Object

ver.0.3.8~ Convert Daru::DF encoding



42
43
44
45
46
47
48
# File 'lib/to_csv.rb', line 42

def convert_enc!(from: "cp932", to: "utf-8")
	self.vectors.each do |col|
		self[col] = self[col].each {|val| val.encode!(to, from_encoding: from) if val.is_a?(String)}
	end
	
	self.encode_vectors!
end

#encode_vector_name(vector_i) ⇒ Object

エンコード関連 ### vector_i番目のヘッダー名を読めるようにエンコード



30
31
32
33
34
# File 'lib/to_csv.rb', line 30

def encode_vector_name(vector_i)
	if self.vectors.to_a[vector_i].is_a?(String)
		self.vectors.to_a[vector_i].encode Encoding::UTF_8, Encoding::Windows_31J
	end
end

#encode_vectors!Object

すべての列に対し上記を実施



37
38
39
# File 'lib/to_csv.rb', line 37

def encode_vectors!
	self.vectors = Daru::Index.new(Range.new(0, self.vectors.size-1).map {|i| encode_vector_name i })
end

#set_index!(indexcolumn) ⇒ Object

rover not suppoted yet about indexing



52
53
54
# File 'lib/to_csv.rb', line 52

def set_index!(indexcolumn)
	self.index = self[indexcolumn]
end

#simplify_multi_index(vector_names_ary) ⇒ Object

To revice pivot index



57
58
59
60
# File 'lib/to_csv.rb', line 57

def simplify_multi_index(vector_names_ary)
	self.vectors = Daru::Index.new(vector_names_ary)
	self.index = Daru::Vector.new(self.index.to_a.map{_1[0]})
end

#to_csvObject



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/to_csv.rb', line 5

def to_csv()
	a = self.to_a.transpose
	
	ans = self.map(&:name).join ","
	self.to_a[0].each do |item|
		ans += "\n"
		ans += item.map{|k, v| v}.join(",")
	end
	
	return ans
end

#write_csv(path, encoding: nil) ⇒ Object



17
18
19
20
# File 'lib/to_csv.rb', line 17

def write_csv(path, encoding: nil)
	enc = encoding.nil? ? "" : ":#{encoding}"
	open(path, "w#{enc}") { _1.write to_csv }
end