Class: Charty::VectorAdapters::ArrayAdapter

Inherits:
BaseAdapter
  • Object
show all
Extended by:
Forwardable
Includes:
IndexSupport, NameSupport, Enumerable
Defined in:
lib/charty/vector_adapters/array_adapter.rb

Instance Attribute Summary

Attributes included from IndexSupport

#index

Attributes included from NameSupport

#name

Attributes inherited from BaseAdapter

#data

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IndexSupport

#[], #[]=

Methods inherited from BaseAdapter

#==, adapter_name, #inverse_log_scale, #log_scale, #mean, #percentile, #stdev, #values_at, #where_in_array

Constructor Details

#initialize(data, index: nil) ⇒ ArrayAdapter

Returns a new instance of ArrayAdapter.



25
26
27
28
# File 'lib/charty/vector_adapters/array_adapter.rb', line 25

def initialize(data, index: nil)
  @data = check_data(data)
  self.index = index || RangeIndex.new(0 ... length)
end

Class Method Details

.supported?(data) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/charty/vector_adapters/array_adapter.rb', line 11

def self.supported?(data)
  case data
  when Array
    case data[0]
    when Numeric, String, Time, Date, DateTime, true, false, nil
      true
    else
      false
    end
  else
    false
  end
end

Instance Method Details

#boolean?Boolean

Returns:

  • (Boolean)


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

def boolean?
  case first_nonnil
  when true, false
    true
  else
    false
  end
end

#categorical?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/charty/vector_adapters/array_adapter.rb', line 62

def categorical?
  false
end

#categoriesObject



66
67
68
# File 'lib/charty/vector_adapters/array_adapter.rb', line 66

def categories
  nil
end

#drop_naObject



80
81
82
83
84
85
86
# File 'lib/charty/vector_adapters/array_adapter.rb', line 80

def drop_na
  if numeric?
    Charty::Vector.new(data.reject { |x| Util.missing?(x) })
  else
    Charty::Vector.new(data.compact)
  end
end

#eq(val) ⇒ Object



88
89
90
91
92
# File 'lib/charty/vector_adapters/array_adapter.rb', line 88

def eq(val)
  Charty::Vector.new(data.map {|x| x == val },
                     index: index,
                     name: name)
end

#first_nonnilObject



40
41
42
# File 'lib/charty/vector_adapters/array_adapter.rb', line 40

def first_nonnil
  data.drop_while(&:nil?).first
end

#group_by(grouper) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/charty/vector_adapters/array_adapter.rb', line 72

def group_by(grouper)
  groups = data.each_index.group_by {|i| grouper[i] }
  groups.map { |g, vals|
    vals.collect! {|i| self[i] }
    [g, Charty::Vector.new(vals)]
  }.to_h
end

#notnullObject



94
95
96
97
98
# File 'lib/charty/vector_adapters/array_adapter.rb', line 94

def notnull
  Charty::Vector.new(data.map {|x| ! Util.missing?(x) },
                     index: index,
                     name: name)
end

#numeric?Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
# File 'lib/charty/vector_adapters/array_adapter.rb', line 53

def numeric?
  case first_nonnil
  when Numeric
    true
  else
    false
  end
end

#where(mask) ⇒ Object



35
36
37
38
# File 'lib/charty/vector_adapters/array_adapter.rb', line 35

def where(mask)
  masked_data, masked_index = where_in_array(mask)
  Charty::Vector.new(masked_data, index: masked_index, name: name)
end