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.



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

def initialize(groupby)
  @groupby = groupby
end

Instance Method Details

#[](key) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 206

def [](key)
  key = case key
        when PyCall::Tuple
          key
        else
          key = Array(key)
          if key.length == 1 && key[0].is_a?(Integer)
            key[0]
          else
            PyCall::Tuple.new(*key)
          end
        end
  Charty::Table.new(@groupby.get_group(key))
end

#apply(*args, &block) ⇒ Object



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

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



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

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



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
187
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 159

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



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

def group_keys
  each_group_key.to_a
end

#indicesObject



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

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