class Charty::VectorAdapters::ArrayAdapter

Public Class Methods

new(data, index: nil) click to toggle source
# 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
supported?(data) click to toggle source
# 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

Public Instance Methods

boolean?() click to toggle source
# File lib/charty/vector_adapters/array_adapter.rb, line 44
def boolean?
  case first_nonnil
  when true, false
    true
  else
    false
  end
end
categorical?() click to toggle source
# File lib/charty/vector_adapters/array_adapter.rb, line 62
def categorical?
  false
end
categories() click to toggle source
# File lib/charty/vector_adapters/array_adapter.rb, line 66
def categories
  nil
end
drop_na() click to toggle source
# 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) click to toggle source
# 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_nonnil() click to toggle source
# File lib/charty/vector_adapters/array_adapter.rb, line 40
def first_nonnil
  data.drop_while(&:nil?).first
end
group_by(grouper) click to toggle source
# 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
notnull() click to toggle source
# 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?() click to toggle source
# File lib/charty/vector_adapters/array_adapter.rb, line 53
def numeric?
  case first_nonnil
  when Numeric
    true
  else
    false
  end
end
where(mask) click to toggle source
# 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