Class: CADataFrameGroup

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

Instance Method Summary collapse

Constructor Details

#initialize(dataframe, name) ⇒ CADataFrameGroup

Returns a new instance of CADataFrameGroup.



1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
# File 'lib/carray/dataframe/dataframe.rb', line 1268

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
  @name = name.to_s
  @addrs = {}
  @keys.each do |k|
    @addrs[k] = @column.eq(k).where
  end
end

Instance Method Details

#[](group_value) ⇒ Object



1322
1323
1324
1325
1326
1327
1328
# File 'lib/carray/dataframe/dataframe.rb', line 1322

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

#calculate(label, &block) ⇒ Object



1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
# File 'lib/carray/dataframe/dataframe.rb', line 1301

def calculate (label, &block)
   new_columns = {@name=>@keys}
  @dataframe.each_column do |name, clmn|
     if name == @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

#eachObject



1330
1331
1332
1333
1334
# File 'lib/carray/dataframe/dataframe.rb', line 1330

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

#each_with_indexObject



1336
1337
1338
1339
1340
# File 'lib/carray/dataframe/dataframe.rb', line 1336

def each_with_index
  @addrs.each do |key, map|
    yield @dataframe[map], key
  end    
end

#table(&block) ⇒ Object



1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
# File 'lib/carray/dataframe/dataframe.rb', line 1286

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