class CADataFrameGroup

Public Class Methods

new(dataframe, name) click to toggle source
# File lib/carray-dataframe/group.rb, line 20
def initialize (dataframe, name)
  @dataframe = dataframe
  case name
  when Hash
    name, list = name.first
    @column = @dataframe.col(name)
    @keys = list.to_ca
  else
    @column = @dataframe.col(name)
    @keys = @column.uniq.sort
  end
  if @column.is_a?(CATimeIndex)
    @keys = CATimeIndex.from_index_array(@keys, @column.timestep)
  end
  @name = name.to_s
  @addrs = {}
  @keys.each do |k|
    @addrs[k] = @column.eq(k).where
  end
end

Public Instance Methods

[](group_value) click to toggle source
# File lib/carray-dataframe/group.rb, line 77
def [] (group_value)
  if @column.is_a?(CATimeIndex) and group_value.is_a?(String)
    group_value = @column.timestep.index_at(group_value)
  end
  if map = @addrs[group_value]
    return @dataframe[map]
  else
    return @dataframe.vacant_copy
  end
end
calculate(label = nil, columns: nil) { |name, clmn[addrs| ... } click to toggle source
# File lib/carray-dataframe/group.rb, line 56
    def calculate (label = nil, columns: nil, &block)
new_columns = { @name => @keys }
            @dataframe.each_column do |name, clmn|
  if name == @name or ( columns && ( not columns.include?(name) ) )
    next
  end
  new_columns[name] = CArray.object(@keys.size) { UNDEF }
  @keys.each_with_index do |k, i|
    begin
      if block
        new_columns[name][i] = yield(name, clmn[@addrs[k]])
      else
        new_columns[name][i] = clmn[@addrs[k]].send(label.intern)
      end
    rescue
    end
  end
end
return CADataFrame.new(new_columns)
    end
each() { |dataframe| ... } click to toggle source
# File lib/carray-dataframe/group.rb, line 88
def each
  @addrs.each do |key, map|
    yield @dataframe[map]
  end
end
each_with_index() { |dataframe, time_at| ... } click to toggle source
# File lib/carray-dataframe/group.rb, line 94
def each_with_index
  if @column.is_a?(CATimeIndex)
    ts = @column.timestep
    @addrs.each do |key, map|
      yield @dataframe[map], ts.time_at(key)
    end
  else
    @addrs.each do |key, map|
      yield @dataframe[map], key
    end
  end
end
table(&block) click to toggle source
# File lib/carray-dataframe/group.rb, line 41
def table (&block)
  hashpool = []
  @keys.each do |k|
    hashpool << @dataframe[@addrs[k]].execute(&block)
  end
  columns = { @name => @keys }
  hashpool.each_with_index do |hash, i|
    hash.each do |key, value|
      columns[key] ||= []
      columns[key][i] = value
    end
  end
  return CADataFrame.new(columns)
end