Class: CADataFrameGroup
- Inherits:
-
Object
- Object
- CADataFrameGroup
- Includes:
- Enumerable
- Defined in:
- lib/carray-dataframe/group.rb
Instance Method Summary collapse
- #[](group_value) ⇒ Object
- #calculate(label = nil, columns: nil, &block) ⇒ Object
- #each ⇒ Object
- #each_with_index ⇒ Object
-
#initialize(dataframe, name) ⇒ CADataFrameGroup
constructor
A new instance of CADataFrameGroup.
- #table(&block) ⇒ Object
Constructor Details
#initialize(dataframe, name) ⇒ CADataFrameGroup
Returns a new instance of CADataFrameGroup.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# 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 |
Instance Method Details
#[](group_value) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# 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, &block) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# 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 ⇒ Object
88 89 90 91 92 |
# File 'lib/carray-dataframe/group.rb', line 88 def each @addrs.each do |key, map| yield @dataframe[map] end end |
#each_with_index ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 |
# 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) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# 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 |