SciRuby / daru

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

Daru::DateTimeIndexHelper unable to infer offsets that do not match a specifc Daru::DateTimeIndexHelper::TIME_INTERVALS #527

Closed jpaulgs closed 4 years ago

jpaulgs commented 4 years ago

Use case:

Create a Daru::Vector from a hash (timestamp: value) if the calulated offset does not fall onto one of the following time intervals than an error is raised

      TIME_INTERVALS = {
        Rational(1,1)     => Daru::Offsets::Day,
        Rational(1,24)    => Daru::Offsets::Hour,
        Rational(1,1440)  => Daru::Offsets::Minute,
        Rational(1,86_400) => Daru::Offsets::Second
      }.freeze

Code example showing the issue::

data = [DateTime.new(2020, 1, 1, 23, 58, 30), DateTime.new(2020, 1, 1, 23, 59, 00), DateTime.new(2020, 1, 1, 23, 59, 30)]

Daru::DateTimeIndexHelper.infer_offset(data)

# The offset here is calculated as being Rational(1, 2880)
# however you will get a NoMethodError
lib/ruby/gems/2.6.0/gems/daru-0.2.2/lib/daru/date_time/index.rb:97:in `infer_offset'
NoMethodError (undefined method `new' for nil:NilClass)

Why is this a problem? When passing a hash to Daru::Vector you cannot specify the index. It will be inferred...