Class: Charty::TableAdapters::PandasDataFrameAdapter::GroupBy
- Inherits:
-
Charty::Table::GroupByBase
- Object
- Charty::Table::GroupByBase
- Charty::TableAdapters::PandasDataFrameAdapter::GroupBy
- Defined in:
- lib/charty/table_adapters/pandas_adapter.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #apply(*args, &block) ⇒ Object
-
#each_group ⇒ Object
TODO: test.
-
#each_group_key ⇒ Object
TODO: test.
- #group_keys ⇒ Object
- #indices ⇒ Object
-
#initialize(groupby) ⇒ GroupBy
constructor
A new instance of GroupBy.
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_group ⇒ Object
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_key ⇒ Object
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_keys ⇒ Object
153 154 155 |
# File 'lib/charty/table_adapters/pandas_adapter.rb', line 153 def group_keys each_group_key.to_a end |
#indices ⇒ Object
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 |