SciRuby / daru

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

Adding vector through direct assignment fails when DataFrame is empty #522

Open dipanm opened 4 years ago

dipanm commented 4 years ago

Here is the code that works:

df = Daru::DataFrame.new({:name => ['john'], :address => ['NY'], :age => [22]}, :index => [1])
 => #<Daru::DataFrame(1x3)>
            name address     age
       1    john      NY      22 

df[:id] = 101
=> 101 
 df
 => #<Daru::DataFrame(1x4)>
            name address     age      id
       1    john      NY      22     101 

So when we add value in a non-existant vector - it essentially creates a new vector and fills the value.

However, the same doesn't work when the existing DF is empty.

df = Daru::DataFrame.new({:name => [], :address => [], :age => []}, :index => [])
 => #<Daru::DataFrame(0x3)>
    name address     age 
df[:id] = 0
NoMethodError: undefined method `to_a' for 0:Fixnum
    from /Users/Dipan/.rvm/gems/ruby-2.2.5/gems/daru-0.2.2/lib/daru/dataframe.rb:2629:in `insert_vector_in_empty'
    from /Users/Dipan/.rvm/gems/ruby-2.2.5/gems/daru-0.2.2/lib/daru/dataframe.rb:2589:in `insert_or_modify_vector'
    from /Users/Dipan/.rvm/gems/ruby-2.2.5/gems/daru-0.2.2/lib/daru/dataframe.rb:2487:in `dispatch_to_axis'
    from /Users/Dipan/.rvm/gems/ruby-2.2.5/gems/daru-0.2.2/lib/daru/dataframe.rb:537:in `[]='
    from (irb):7
    from /Users/Dipan/.rvm/rubies/ruby-2.2.5/bin/irb:11:in `<main>'

A typical expectation would be if the df empty is empty it should just add a vector and return without adding any row. At least don't face an unhandled exception.

Any help?