Class: Charty::Table

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
MissingValueSupport
Defined in:
lib/charty/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MissingValueSupport

#missing_value?

Constructor Details

#initialize(data, **kwargs) ⇒ Table

Returns a new instance of Table.



18
19
20
21
22
23
24
25
26
27
# File 'lib/charty/table.rb', line 18

def initialize(data, **kwargs)
  adapter_class = TableAdapters.find_adapter_class(data)
  if kwargs.empty?
    @adapter = adapter_class.new(data)
  else
    @adapter = adapter_class.new(data, **kwargs)
  end

  @column_cache = {}
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



29
30
31
# File 'lib/charty/table.rb', line 29

def adapter
  @adapter
end

Instance Method Details

#==(other) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/charty/table.rb', line 39

def ==(other)
  return true if equal?(other)

  case other
  when Charty::Table
    adapter == other.adapter
  else
    super
  end
end

#[](key) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/charty/table.rb', line 54

def [](key)
  key = case key
        when Symbol
          key
        else
          String.try_convert(key).to_sym
        end
  if @column_cache.key?(key)
    @column_cache[key]
  else
    @column_cache[key] = @adapter[nil, key]
  end
end

#drop_naObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/charty/table.rb', line 101

def drop_na
  # TODO: Must implement this method in each adapter
  missing_index = index.select do |i|
    column_names.any? do |key|
      missing_value?(self[key][i])
    end
  end
  if missing_index.empty?
    self
  else
    select_index = index.to_a - missing_index
    new_data = column_names.map { |key|
      vals = select_index.map {|i| self[key][i] }
      [key, vals]
    }.to_h
    Charty::Table.new(new_data, index: select_index)
  end
end

#eachObject



91
92
93
94
95
96
97
98
99
# File 'lib/charty/table.rb', line 91

def each
  return to_enum(__method__) unless block_given?
  data = to_a
  i, n = 0, data.size
  while i < n
    yield data[i]
    i += 1
  end
end

#empty?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/charty/table.rb', line 50

def empty?
  length == 0
end

#to_a(x = nil, y = nil, z = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/charty/table.rb', line 68

def to_a(x=nil, y=nil, z=nil)
  case
  when defined?(Daru::DataFrame) && table.kind_of?(Daru::DataFrame)
    table.map(&:to_a)
  when defined?(Numo::NArray) && table.kind_of?(Numo::NArray)
    table.to_a
  when defined?(NMatrix) && table.kind_of?(NMatrix)
    table.to_a
  when defined?(ActiveRecord::Relation) && table.kind_of?(ActiveRecord::Relation)
    if z && x && y
      [table.pluck(x), table.pluck(y), table.pluck(z)]
    elsif x && y
      [table.pluck(x), table.pluck(y)]
    else
      raise ArgumentError, "column_names is required to convert to_a from ActiveRecord::Relation"
    end
  when table.kind_of?(Array)
    table
  else
    raise ArgumentError, "unsupported object: #{table.inspect}"
  end
end