Class: Charty::TableAdapters::DaruAdapter

Inherits:
BaseAdapter
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/charty/table_adapters/daru_adapter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseAdapter

#==, #column?, #drop_na, #group_by, #melt, #sort_values

Constructor Details

#initialize(data, columns: nil, index: nil) ⇒ DaruAdapter

Returns a new instance of DaruAdapter.



15
16
17
18
19
20
# File 'lib/charty/table_adapters/daru_adapter.rb', line 15

def initialize(data, columns: nil, index: nil)
  @data = check_type(Daru::DataFrame, data, :data)

  self.columns = columns unless columns.nil?
  self.index = index unless index.nil?
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



22
23
24
# File 'lib/charty/table_adapters/daru_adapter.rb', line 22

def data
  @data
end

Class Method Details

.supported?(data) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/charty/table_adapters/daru_adapter.rb', line 11

def self.supported?(data)
  defined?(Daru::DataFrame) && data.is_a?(Daru::DataFrame)
end

Instance Method Details

#[](row, column) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/charty/table_adapters/daru_adapter.rb', line 53

def [](row, column)
  if row
    @data[column][row]
  else
    column_data = if @data.has_vector?(column)
                    @data[column]
                  else
                    case column
                    when String
                      @data[column.to_sym]
                    else
                      @data[column.to_s]
                    end
                  end
    Vector.new(column_data)
  end
end

#[]=(key, values) ⇒ Object



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
# File 'lib/charty/table_adapters/daru_adapter.rb', line 71

def []=(key, values)
  case key
  when Symbol
    sym_key = key
    str_key = key.to_s
  else
    str_key = key.to_str
    sym_key = str_key.to_sym
  end
  case
  when @data.has_vector?(sym_key)
    key = sym_key
  when @data.has_vector?(str_key)
    key = str_key
  end

  case values
  when Charty::Vector
    case values.adapter
    when Charty::VectorAdapters::DaruVectorAdapter
      @data[key] = values.adapter.data
    else
      @data[key] = values.to_a
    end
  else
    orig_values = values
    values = Array.try_convert(values)
    if values.nil?
      raise ArgumentError, "`values` must be convertible to Array"
    end
    @data[key] = values
  end
  return values
end

#column_namesObject



40
41
42
# File 'lib/charty/table_adapters/daru_adapter.rb', line 40

def column_names
  @data.vectors.to_a
end

#columnsObject



32
33
34
# File 'lib/charty/table_adapters/daru_adapter.rb', line 32

def columns
  DaruIndex.new(data.vectors)
end

#columns=(values) ⇒ Object



36
37
38
# File 'lib/charty/table_adapters/daru_adapter.rb', line 36

def columns=(values)
  data.vectors = Daru::Index.coerce(values)
end

#compare_data_equality(other) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/charty/table_adapters/daru_adapter.rb', line 44

def compare_data_equality(other)
  case other
  when DaruAdapter
    data == other.data
  else
    super
  end
end

#indexObject



26
27
28
# File 'lib/charty/table_adapters/daru_adapter.rb', line 26

def index
  DaruIndex.new(data.index)
end