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.



105
106
107
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 105

def initialize(groupby)
  @groupby = groupby
end

Instance Method Details

#[](key) ⇒ Object



157
158
159
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 157

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

#apply(*args, &block) ⇒ Object



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

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_group_keyObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 119

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



115
116
117
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 115

def group_keys
  each_group_key.to_a
end

#indicesObject



109
110
111
112
113
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 109

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