SciRuby / daru

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

Vector method 'at' can't work when index is DateTimeIndex #331

Closed user919lx closed 7 years ago

user919lx commented 7 years ago

For examle, this code can't work

require  'daru'
dti = Daru::DateTimeIndex.new(6.times.map{|i| DateTime.new(2012,1,i+1)})
a=6.times.map{|i| i}

v=Daru::Vector.new(a,index:dti)

puts v.at(*[1,2])

system tells 'key': undefined method '[]' for nil:NilClass (NoMethodError)

I have found the reason. DateTimeIndex's constructor doesn't call super.

I add follow code then everything is OK.

class Daru::DateTimeIndex
  def initialize data, opts={freq: nil}

    super data

    Helper.possibly_convert_to_date_time data

    @offset =
      case opts[:freq]
      when :infer then Helper.infer_offset(data)
      when  nil   then nil
      else  Helper.offset_from_frequency(opts[:freq])
      end

    @frequency = @offset ? @offset.freq_string : nil
    @data      = data.each_with_index.to_a.sort_by(&:first)

    @periods = data.size
  end
end
baarkerlounger commented 7 years ago

This is fixed and should be closed.