class CArray

Public Class Methods

load_excel(filename, sheet=0) click to toggle source
# File lib/carray-dataframe/io.rb, line 26
def self.load_excel (filename, sheet=0)
  book = Spreadsheet.open(filename)
  sheet = book.worksheet(sheet)
  return sheet.map(&:to_a).to_ca
end

Public Instance Methods

describe(as: nil) click to toggle source
# File lib/carray-dataframe/dataframe.rb, line 1181
def describe (as: nil)
  if as
    type = as.intern
  else
    type = describe_type
  end
  case type
  when :numeric
    describe_numeric
  when :categorical
    describe_categorical
  else
    raise "unknown"
  end
end
describe_categorical() click to toggle source
# File lib/carray-dataframe/dataframe.rb, line 1211
def describe_categorical
  hash = {}
  each do |v|
    hash[v] ||= 0
    hash[v] += 1
  end
  top, freq = hash.max_by{|x| x[1]}
  {
    count:  is_masked.count_false,
    unique: hash.size,
    top:    top,
    freq:   freq,
  }
end
describe_numeric() click to toggle source
# File lib/carray-dataframe/dataframe.rb, line 1197
def describe_numeric
  min, q25, median, q75, max = *quantile
  {
    count:  is_masked.count_false,
    mean:   mean,
    std:    stddev,
    max:    max,
    q75:    q75,
    median: median,
    q25:    q25,
    min:    min,
  }
end
get_dummies() click to toggle source
# File lib/carray-dataframe/dataframe.rb, line 1264
def get_dummies
  keys = uniq
  hash = {}
  keys.each do |k|
    hash[k] = self.eq(k)
  end
  return hash
end
save_excel(filename, &block) click to toggle source
# File lib/carray-dataframe/io.rb, line 11
def save_excel (filename, &block)
  if self.rank >= 3
    raise "too large rank (>2) to write excel file"
  end
  book = Spreadsheet::Workbook.new
  worksheet = book.create_worksheet
  self.dim0.times do |i|
    worksheet.row(i).push *self[i,nil]
  end
  if block
    block.call(worksheet)
  end
  book.write(filename)
end
summary() click to toggle source
# File lib/carray-dataframe/dataframe.rb, line 1226
def summary 
  summary_categorical
end
summary_categorical() click to toggle source
# File lib/carray-dataframe/dataframe.rb, line 1230
def summary_categorical
  hash = {}
  each do |v|
    hash[v] ||= 0
    hash[v] += 1
  end
  hash
end

Private Instance Methods

describe_type() click to toggle source
# File lib/carray-dataframe/dataframe.rb, line 1161
def describe_type
  type = nil
  case true
  when numeric?
    type = :numeric
  when boolean?
    type = :categorical
  else
    begin
      self / 1
      type = :numeric
    rescue 
      type = :categorical
    end
  end
  type
end