SciRuby / daru

Data Analysis in RUby
BSD 2-Clause "Simplified" License
1.04k stars 139 forks source link

Daru::Core::GroupBy#each_group #get_group not work with newly created index. #490

Open kojix2 opened 5 years ago

kojix2 commented 5 years ago

Hi I found that each_group and get_group method do not work as expected when you set a new index. For example:

require 'daru'
df = Daru::DataFrame.new(
    x: [1,2,3,4,5,6],
    z: %w(a a b b c c))
df.index = [1,2,3,4,5,6]

gr = df.group_by(:z)

gr.each_group{|g| p g}
##<Daru::DataFrame(2x2)>
#       x   z
#   1   2   a
#   2   3   b
##<Daru::DataFrame(2x2)>
#       x   z
#   3   4   b
#   4   5   c
#NoMethodError: undefined method `size' for nil:NilClass
#gems/daru-0.2.1/lib/daru/dataframe.rb:168:in `block in rows'

gr.get_group("a")
#NoMethodError: undefined method `each' for nil:NilClass
#gems/daru-0.2.1/lib/daru/core/group_by.rb:262:in `get_group'

Sorry if this is duplicated issue.

kojix2 commented 5 years ago
# gr.get_group("a")
gr.get_group(["a"])
kojix2 commented 5 years ago

This line. https://github.com/SciRuby/daru/blob/984ff72024466917f8c8ad184e045efa14470412/lib/daru/core/group_by.rb#L262

transpose[idx] work as expected only when index is 0,1,2,...

Shekharrajak commented 5 years ago

Thanks for pointing out this error. Someone will surely look into it.