SciRuby / daru

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

Allow inserting or updating DataFrame vectors with single values #351 #377

Closed baarkerlounger closed 7 years ago

baarkerlounger commented 7 years ago

https://github.com/SciRuby/daru/issues/351

Enable:

df = Daru::DataFrame.new({a: [1, 2, 3], b: [10, 20, 30]})
=> #<Daru::DataFrame(3x2)>
       a   b
   0   1  10
   1   2  20
   2   3  30

df[:c] = 1.0
=> #<Daru::DataFrame(3x3)>
       a   b   c
   0   1  10  1.0 
   1   2  20  1.0
   2   3  30  1.0

df[:a] = 1.0
=> #<Daru::DataFrame(3x3)>
       a   b   c
   0   1.0  10  1.0 
   1   1.0  20  1.0
   2   1.0  30  1.0
zverok commented 7 years ago

Ugh. Sorry for being late to the party, I've started reviewing yesterday but got distracted :( This merged code has one important problem. What if value assigned is Enumerable, but is not Array or Range? (Say, it is ActiveRecord::Relation.) What user expects is it would be converted to_a, and then inserted as a vector. What actually will happen, is pretty obscure error:

@baarkerlounger can you please do a small follow-up PR which fixes this (I believe, by replacing when Array, Rang with if respond_to?(:to_a)) It is OK if you have no time for this, I can do it myself.

baarkerlounger commented 7 years ago

@zverok implemented your suggestion here: https://github.com/SciRuby/daru/pull/379