SciRuby / daru

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

Convenient way of setting new/existing column: df[:colx] = 1.0 #351

Closed parthm closed 6 years ago

parthm commented 7 years ago

It may be worthwhile having df[:c] = 1.0 as a convenient method of setting a new column and existing columns. Right now it errors out.

Daru: 0.1.5 Ruby: 2.4.0

[4] pry(main)> 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
[5] pry(main)> df[:c] = 1.0
NoMethodError: undefined method `size' for 1.0:Float
from /home/user/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/daru-0.1.5/lib/daru/dataframe.rb:2188:in `prepare_vector_for_insert'
[6] pry(main)> df[:c] = [1.0] * df.nrows
=> [1.0, 1.0, 1.0]
[7] pry(main)> df
=> #<Daru::DataFrame(3x3)>
       a   b   c
   0   1  10 1.0
   1   2  20 1.0
   2   3  30 1.0

While we can do df[:c] = [1.0] * df.nrows it's probably more intuitive and clearer to do df[:c] = 1.0, especially for new users from other libs like pandas.

Behavior for pandas.

In [1]: import pandas as pd

In [2]: df = pd.DataFrame({'a': [1, 2, 3], 'b' : [10, 20, 30]})

In [3]: df
Out[3]: 
   a   b
0  1  10
1  2  20
2  3  30

In [4]: df['c'] = 1.0

In [5]: df
Out[5]: 
   a   b    c
0  1  10  1.0
1  2  20  1.0
2  3  30  1.0

In [6]: 
zverok commented 7 years ago

I love the idea. Accepted :)

baarkerlounger commented 6 years ago

Think this can be closed given the above merged commit.