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



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

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



50
51
52
53
54
55
56
# File 'lib/to_csv.rb', line 50

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番目のヘッダー名を読めるようにエンコード



38
39
40
41
42
# File 'lib/to_csv.rb', line 38

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

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



45
46
47
# File 'lib/to_csv.rb', line 45

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



60
61
62
# File 'lib/to_csv.rb', line 60

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

#simple_pivot(index, vectors, values, agg: :mean, index_name: nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/to_csv.rb', line 70

def simple_pivot(index, vectors, values, agg: :mean, index_name: nil)
  
  # index, vectors are Arrays. 'values' is String or Array.
  ## 文字列データなどで最初のデータだけ欲しければ agg: :first
  piv = self.pivot_table index: index, vectors: vectors, agg: agg, values: values
  piv.vectors = Daru::Index.new( piv.vectors.to_a.map { _1.join("-") } )
  piv.index = Daru::Vector.new( piv.index.to_a.map { _1.join("-") } )
  
  # indexを新しく追加
  index_name ||= "Pivot_Index"
  piv[index_name] = piv.index
  
  # 順番変更
  piv.order = [piv.vectors.to_a[-1]] + piv.vectors.to_a[0..-2]
  
  return piv
  
end

#simplify_multi_index(vector_names_ary) ⇒ Object

To revice pivot index



65
66
67
68
# File 'lib/to_csv.rb', line 65

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

#to_roverObject



89
90
91
# File 'lib/to_csv.rb', line 89

def to_rover
  Rover::DataFrame.new(self.to_a[0])
end

#write_csv(path, encoding: nil, alt: false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/to_csv.rb', line 17

def write_csv(path, encoding: nil, alt: false)
  if alt
    # Experimental: faster algorithm
    # ""付加を先にDaru上でやってあげる案
    
    #今はとりあえず
    self.to_daru.write_csv path, encoding: encoding
  else
    enc = encoding.nil? ? "" : ":#{encoding}"
    open(path, "w#{enc}") { _1.write to_csv }
  end
end