Class: Arrow::Group

Inherits:
Object
  • Object
show all
Defined in:
lib/arrow/group.rb

Overview

Experimental

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

Instance Method Summary collapse

Constructor Details

#initialize(table, keys) ⇒ Group

Returns a new instance of Group.



23
24
25
26
# File 'lib/arrow/group.rb', line 23

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

Instance Method Details

#averageObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/arrow/group.rb', line 58

def average
  key_names = @keys.collect(&:to_s)
  target_columns = @table.columns.reject do |column|
    key_names.include?(column.name) or
      not column.data_type.is_a?(NumericDataType)
  end
  aggregate(target_columns) do |column, indexes|
    average = 0.0
    n = 0
    indexes.each do |index|
      value = column[index]
      unless value.nil?
        n += 1
        average += (value - average) / n
      end
    end
    average
  end
end

#countObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/arrow/group.rb', line 28

def count
  key_names = @keys.collect(&:to_s)
  target_columns = @table.columns.reject do |column|
    key_names.include?(column.name)
  end
  aggregate(target_columns) do |column, indexes|
    n = 0
    indexes.each do |index|
      n += 1 unless column.null?(index)
    end
    n
  end
end

#maxObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/arrow/group.rb', line 96

def max
  key_names = @keys.collect(&:to_s)
  target_columns = @table.columns.reject do |column|
    key_names.include?(column.name) or
      not column.data_type.is_a?(NumericDataType)
  end
  aggregate(target_columns) do |column, indexes|
    n = nil
    indexes.each do |index|
      value = column[index]
      next if value.nil?
      n ||= value
      n = value if value > n
    end
    n
  end
end

#minObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/arrow/group.rb', line 78

def min
  key_names = @keys.collect(&:to_s)
  target_columns = @table.columns.reject do |column|
    key_names.include?(column.name) or
      not column.data_type.is_a?(NumericDataType)
  end
  aggregate(target_columns) do |column, indexes|
    n = nil
    indexes.each do |index|
      value = column[index]
      next if value.nil?
      n ||= value
      n = value if value < n
    end
    n
  end
end

#sumObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/arrow/group.rb', line 42

def sum
  key_names = @keys.collect(&:to_s)
  target_columns = @table.columns.reject do |column|
    key_names.include?(column.name) or
      not column.data_type.is_a?(NumericDataType)
  end
  aggregate(target_columns) do |column, indexes|
    n = 0
    indexes.each do |index|
      value = column[index]
      n += value unless value.nil?
    end
    n
  end
end