Class: CADataFrameGroupMulti

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

Instance Method Summary collapse

Constructor Details

#initialize(dataframe, *names) ⇒ CADataFrameGroupMulti

Returns a new instance of CADataFrameGroupMulti.



1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
# File 'lib/carray/dataframe/dataframe.rb', line 1348

def initialize (dataframe, *names)
  @rank = names.size
  @dataframe = dataframe
  @names = []
  @column = []
  @keys = []
  names.each_with_index do |name, i|    
    case name
    when Hash
      name, list = name.first
      @column[i] = @dataframe.col(name)
      @keys[i] = list.to_ca
    else
      @column[i] = @dataframe.col(name)
      @keys[i] = @column[i].to_ca.uniq.sort
    end
    @names[i] = name
  end
  @addrs = {}
  each_with_keys do |list|
    flag = @column[0].eq(list[0])
    (1...@rank).each do |i|
      flag &= @column[i].eq(list[i])
    end
    @addrs[list] = flag.where
  end
end

Instance Method Details

#[](group_value) ⇒ Object



1403
1404
1405
1406
1407
1408
1409
# File 'lib/carray/dataframe/dataframe.rb', line 1403

def [] (group_value)
  if map = @addrs[group_value]
    return @dataframe[map]
  else
    return @dataframe.vacant_copy
  end
end

#eachObject



1411
1412
1413
1414
1415
# File 'lib/carray/dataframe/dataframe.rb', line 1411

def each 
  each_with_keys do |key|
    yield key, @dataframe[@addrs[key]]
  end
end

#each_with_keys(&block) ⇒ Object



1376
1377
1378
# File 'lib/carray/dataframe/dataframe.rb', line 1376

def each_with_keys (&block)
  @keys[0].to_a.product(*@keys[1..-1].map(&:to_a)).each(&block)    
end

#table(&block) ⇒ Object



1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
# File 'lib/carray/dataframe/dataframe.rb', line 1380

def table (&block)
  hashpool = []
  each_with_keys do |list|
    hashpool << @dataframe[@addrs[list]].execute(&block) 
  end
  columns = {}
  @names.each do |name|
    columns[name] = []
  end
  each_with_keys.with_index do |list,j|
    @names.each_with_index do |name,i|
      columns[name][j] = list[i]
    end
  end
  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