Class: CADataFrameGroupMulti
- Inherits:
-
Object
- Object
- CADataFrameGroupMulti
- Defined in:
- lib/carray-dataframe/group.rb
Instance Method Summary collapse
- #[](group_value) ⇒ Object
- #calculate(label, &block) ⇒ Object
- #each ⇒ Object
- #each_with_keys(&block) ⇒ Object
-
#initialize(dataframe, *names) ⇒ CADataFrameGroupMulti
constructor
A new instance of CADataFrameGroupMulti.
- #table(&block) ⇒ Object
Constructor Details
#initialize(dataframe, *names) ⇒ CADataFrameGroupMulti
Returns a new instance of CADataFrameGroupMulti.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/carray-dataframe/group.rb', line 110 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
186 187 188 189 190 191 192 |
# File 'lib/carray-dataframe/group.rb', line 186 def [] (group_value) if map = @addrs[group_value] return @dataframe[map] else return @dataframe.vacant_copy end end |
#calculate(label, &block) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/carray-dataframe/group.rb', line 165 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 |
#each ⇒ Object
194 195 196 197 198 |
# File 'lib/carray-dataframe/group.rb', line 194 def each each_with_keys do |key| yield key, @dataframe[@addrs[key]] end end |
#each_with_keys(&block) ⇒ Object
138 139 140 |
# File 'lib/carray-dataframe/group.rb', line 138 def each_with_keys (&block) @keys[0].to_a.product(*@keys[1..-1].map(&:to_a)).each(&block) end |
#table(&block) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/carray-dataframe/group.rb', line 142 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 |