Class: Arrow::RollingWindow

Inherits:
Object
  • Object
show all
Defined in:
lib/arrow/rolling-window.rb

Overview

Experimental

TODO: Almost codes should be implemented in Apache Arrow C++.

Instance Method Summary collapse

Constructor Details

#initialize(table, size) ⇒ RollingWindow

Returns a new instance of RollingWindow.



23
24
25
26
# File 'lib/arrow/rolling-window.rb', line 23

def initialize(table, size)
  @table = table
  @size = size
end

Instance Method Details

#lag(key, diff: 1) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/arrow/rolling-window.rb', line 28

def lag(key, diff: 1)
  column = @table[key]
  if @size
    windows = column.each_slice(@size)
  else
    windows = column
  end
  lag_values = [nil] * diff
  windows.each_cons(diff + 1) do |values|
    target = values[0]
    current = values[1]
    if target.nil? or current.nil?
      lag_values << nil
    else
      lag_values << current - target
    end
  end
  ArrayBuilder.build(lag_values)
end