Class: Eps::DataFrame

Inherits:
Object
  • Object
show all
Defined in:
lib/eps/data_frame.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = []) ⇒ DataFrame


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/eps/data_frame.rb', line 6

def initialize(data = [])
  @columns = {}

  if data.is_a?(Eps::DataFrame)
    data.columns.each do |k, v|
      @columns[k] = v
    end
  elsif daru?(data)
    data.to_h.each do |k, v|
      @columns[k.to_s] = v.to_a
    end
  elsif data.is_a?(Hash)
    data.each do |k, v|
      @columns[k.to_s] = v.to_a
    end
  else
    if data.any?
      row = data[0]

      if row.is_a?(Hash)
        row.keys.each do |k|
          @columns[k.to_s] = data.map { |r| r[k] }
        end
      elsif row.is_a?(Array)
        row.size.times do |i|
          @columns["x#{i}"] = data.map { |r| r[i] }
        end
      else
        @columns["x0"] = data
      end
    end
  end
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns


3
4
5
# File 'lib/eps/data_frame.rb', line 3

def columns
  @columns
end

#labelObject

Returns the value of attribute label


4
5
6
# File 'lib/eps/data_frame.rb', line 4

def label
  @label
end

Instance Method Details

#==(other) ⇒ Object


122
123
124
# File 'lib/eps/data_frame.rb', line 122

def ==(other)
  columns.keys == other.columns.keys && columns == other.columns
end

#[](rows, cols = nil) ⇒ Object


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/eps/data_frame.rb', line 68

def [](rows, cols = nil)
  if cols.nil?
    if rows.is_a?(String) || (rows.is_a?(Array) && rows.first.is_a?(String))
      cols = rows
      rows = 0..-1
    end
  end

  if rows.is_a?(Range)
    if rows.end.nil?
      rows = Range.new(rows.begin, size - 1)
    elsif rows.end < 0
      rows = Range.new(rows.begin, size + rows.end, rows.exclude_end?)
    end
  end

  if cols
    if cols.is_a?(Range)
      c = columns.keys

      start_index = c.index(cols.begin)
      raise "Undefined column: #{cols.begin}" unless start_index

      end_index = c.index(cols.end)
      raise "Undefined column: #{cols.end}" unless end_index

      reverse = false
      if start_index > end_index
        reverse = true
        start_index, end_index = end_index, start_index
      end

      cols = c[Range.new(start_index, end_index, cols.exclude_end?)]
      cols.reverse! if reverse
    elsif !cols.is_a?(Array)
      singular = true
      cols = [cols]
    end
  else
    cols = columns.keys
  end

  df = Eps::DataFrame.new

  cols.each do |c|
    raise "Undefined column: #{c}" unless columns.include?(c)

    df.columns[c] = columns[c].values_at(*rows)
  end
  df.label = label.values_at(*rows) if label

  singular ? df.columns[cols[0]] : df
end

#any?Boolean


48
49
50
# File 'lib/eps/data_frame.rb', line 48

def any?
  @columns.any?
end

#dupObject


126
127
128
129
130
131
132
133
# File 'lib/eps/data_frame.rb', line 126

def dup
  df = Eps::DataFrame.new
  columns.each do |k, v|
    df.columns[k] = v
  end
  df.label = label
  df
end

#empty?Boolean


40
41
42
# File 'lib/eps/data_frame.rb', line 40

def empty?
  size == 0
end

#mapObject


52
53
54
55
56
57
58
# File 'lib/eps/data_frame.rb', line 52

def map
  if @columns.any?
    size.times.map do |i|
      yield Hash[@columns.map { |k, v| [k, v[i]] }]
    end
  end
end

#map_rowsObject


60
61
62
63
64
65
66
# File 'lib/eps/data_frame.rb', line 60

def map_rows
  if @columns.any?
    size.times.map do |i|
      yield @columns.map { |_, v| v[i] }
    end
  end
end

#sizeObject


44
45
46
# File 'lib/eps/data_frame.rb', line 44

def size
  @columns.any? ? columns.values.first.size : 0
end