Class: Charty::TableAdapters::PandasDataFrameAdapter::GroupBy

Inherits:
Charty::Table::GroupByBase show all
Defined in:
lib/charty/table_adapters/pandas_adapter.rb

Instance Method Summary collapse

Constructor Details

#initialize(groupby) ⇒ GroupBy

Returns a new instance of GroupBy.



143
144
145
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 143

def initialize(groupby)
  @groupby = groupby
end

Instance Method Details

#[](key) ⇒ Object



205
206
207
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 205

def [](key)
  Charty::Table.new(@groupby.get_group(key))
end

#apply(*args, &block) ⇒ Object



197
198
199
200
201
202
203
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 197

def apply(*args, &block)
  res = @groupby.apply(->(data) {
    res = block.call(Charty::Table.new(data), *args)
    Pandas::Series.new(data: res)
  })
  Charty::Table.new(res)
end

#each_groupObject

TODO: test



189
190
191
192
193
194
195
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 189

def each_group
  return enum_for(__method__) unless block_given?

  each_group_key do |key|
    yield(Array(key), self[key])
  end
end

#each_group_keyObject

TODO: test



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 158

def each_group_key
  return enum_for(__method__) unless block_given?

  if PyCall.respond_to?(:iterable)
    PyCall.iterable(@groupby).each do |key, index|
      if key.class == PyCall.builtins.tuple
        key = key.to_a
      end
      yield key
    end
  else # TODO: Remove this clause after the new PyCall will be released
    iter = @groupby.__iter__()
    while true
      begin
        key, sub_data = iter.__next__
        if key.class == PyCall.builtins.tuple
          key = key.to_a
        end
        yield key
      rescue PyCall::PyError => error
        if error.type == PyCall.builtins.StopIteration
          break
        else
          raise error
        end
      end
    end
  end
end

#group_keysObject



153
154
155
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 153

def group_keys
  each_group_key.to_a
end

#indicesObject



147
148
149
150
151
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 147

def indices
  @groupby.indices.map { |k, v|
    [k, v.to_a]
  }.to_h
end