Class: CADataFramePivot

Inherits:
Object
  • Object
show all
Defined in:
lib/carray-dataframe/pivot.rb

Instance Method Summary collapse

Constructor Details

#initialize(dataframe, name1, name2) ⇒ CADataFramePivot

Returns a new instance of CADataFramePivot.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/carray-dataframe/pivot.rb', line 14

def initialize (dataframe, name1, name2)
  @dataframe = dataframe
  case name1
  when Hash
    name1, list = name1.first
    @column1 = @dataframe.col(name1)
    @keys1 = list.to_ca
  else
    @column1 = @dataframe.col(name1)
    @keys1 = @column1.uniq.sort
  end
  case name2
  when Hash
    name2, list = name2.first
    @column2 = @dataframe.col(name2)
    @keys2 = list
  else
    @column2 = @dataframe.col(name2)
    @keys2 = @column2.uniq.sort
  end
  @addrs = {}
  @keys1.each do |k1|
    @keys2.each do |k2|
      @addrs[[k1,k2]] = (@column1.eq(k1) & @column2.eq(k2)).where
    end
  end
end

Instance Method Details

#table(&block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/carray-dataframe/pivot.rb', line 42

def table (&block)
  columns = {}
  @keys2.each do |k2|
    columns[k2] = CArray.object(@keys1.size) { UNDEF }
  end
  @keys1.each_with_index do |k1, i|
    @keys2.each do |k2|
      columns[k2][i] = block.call(@dataframe[@addrs[[k1,k2]]])
    end
  end
  return CADataFrame.new(columns, index: @keys1)
end