SciRuby / daru

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

Method missing for Vectors #365

Closed info-rchitect closed 7 years ago

info-rchitect commented 7 years ago

Hi,

Wonder what people think about allowing Vector method_missing to interrogate the values within it via respond_to? and if so then execute the method. The use case for me is adding new columns to a DataFrame.

mydataframe[:new_column] = mydataframe[:str_column].slice(0..5)

In the case above it is just getting a substring of Vector with strings in it. How do folks handle this now?

thx

v0dro commented 7 years ago

You can simply add new vectors to a DataFrame by specifying a new vector name and assigning an Array or Vector using =.

2.3.3 :006 > a=Daru::DataFrame.new({a: [1,2,3], b: [2,3,4]})
 => #<Daru::DataFrame(3x2)>
       a   b
   0   1   2
   1   2   3
   2   3   4 
2.3.3 :007 > a[:c] = [3,4,5]
 => [3, 4, 5] 
2.3.3 :008 > a
 => #<Daru::DataFrame(3x3)>
       a   b   c
   0   1   2   3
   1   2   3   4
   2   3   4   5 

If you assign a Vector to a[:c] it will match the indexes with the dataframe and then create the new vector.

v0dro commented 7 years ago

The only constraint is that when you assign an Array the length of the array should be equal to the column size of the dataframe.

info-rchitect commented 7 years ago

thx