SciRuby / daru

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

Add_row with multi-index #415

Closed info-rchitect closed 6 years ago

info-rchitect commented 6 years ago

Hi,

I am trying to add a row of data to a DF with a multi-index and cannot figure out why this doesn't work. Any help is greatly appreciated.

[5] pry(main)> dset.data
=> #<Daru::DataFrame(8x9)>
                                     dataset       test   facility test_yield test_yield total_pass total_pass total_test total_test
        ds1      testA        png        ds1      testA        png       0.98        nil       98.0        nil      100.0        nil
                              suz        ds1      testA        suz        nil       0.97        nil       97.0        nil      100.0
                 testB        png        ds1      testB        png       0.79        nil       79.0        nil      100.0        nil
                              suz        ds1      testB        suz        nil       0.92        nil       92.0        nil      100.0
                 testC        png        ds1      testC        png       0.99        nil       99.0        nil      100.0        nil
                              suz        ds1      testC        suz        nil        0.9        nil       90.0        nil      100.0
                 testD        png        ds1      testD        png       0.86        nil       86.0        nil      100.0        nil
                              suz        ds1      testD        suz        nil       0.85        nil       85.0        nil      100.0

[8] pry(main)> new_row_index = dset.index.to_a[-2]
=> [:ds1, "testD", "png"]
[9] pry(main)> new_row_index[1] = "testE"
=> "testE"
[10] pry(main)> new_row_index
=> [:ds1, "testE", "png"]
[11] pry(main)> new_row_data = [:ds1, "testE", "png"] + [nil] * 6
=> [:ds1, "testE", "png", nil, nil, nil, nil, nil, nil]
[12] pry(main)> dset.add_row new_row_data, index: new_row_index
IndexError: element size differs (1 should be 3)

thx

EDIT

I tried using the same multi-index to edit an existing row and it worked just fine but the add-row method doesn't seem to like a similar index passed as an Array.

[20] pry(main)> dset.row[[:ds1, 'testA', 'png']]
=> #<Daru::Vector(9)>
                         ds1testApng
           dataset               ds1
              test             testA
          facility               png
   test_yield_amg1              0.999
   test_yield_amg2               nil
   total_pass_amg1              99.0
   total_pass_amg2               nil
 total_tested_amg1             100.0
 total_tested_amg2               nil
[21] pry(main)> dset.row[[:ds1, 'testA', 'png']] = [nil] * 9
=> [nil, nil, nil, nil, nil, nil, nil, nil, nil]
[22] pry(main)> dset.row[[:ds1, 'testA', 'png']]
=> #<Daru::Vector(9)>
                         ds1testApng
           dataset               nil
              test               nil
          facility               nil
   test_yield_amg1               nil
   test_yield_amg2               nil
   total_pass_amg1               nil
   total_pass_amg2               nil
 total_tested_amg1               nil
 total_tested_amg2               nil
[23] pry(main)> dset.add_row [nil]*9, index: [:ds1, 'testE', 'png']
IndexError: element size differs (1 should be 3)
from /home/user/.origen/gems/ruby/2.3.0/gems/daru-0.1.6/lib/daru/index/multi_index.rb:117:in `transpose'

Does anyone have an example of adding a row using a multi-index?

thx

zverok commented 6 years ago

Thanks for the reporting! Fixed in https://github.com/SciRuby/daru/pull/418. Until it is merged and released, you can just use (with the same effect, yet working):

dset.row[*new_row_index] = new_row_data
info-rchitect commented 6 years ago

thanks @zverok !