Class: CArray

Inherits:
Object
  • Object
show all
Defined in:
lib/carray-dataframe/io.rb,
lib/carray-dataframe/dataframe.rb,
lib/carray-dataframe/dataframe.rb

Defined Under Namespace

Modules: TableMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_excel(filename, sheet = 0) ⇒ Object



26
27
28
29
30
# 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

Instance Method Details

#describe(as: nil) ⇒ Object



1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
# 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_categoricalObject



1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
# 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_numericObject



1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
# 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_dummiesObject



1264
1265
1266
1267
1268
1269
1270
1271
# 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) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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

#summaryObject



1226
1227
1228
# File 'lib/carray-dataframe/dataframe.rb', line 1226

def summary 
  summary_categorical
end

#summary_categoricalObject



1230
1231
1232
1233
1234
1235
1236
1237
# File 'lib/carray-dataframe/dataframe.rb', line 1230

def summary_categorical
  hash = {}
  each do |v|
    hash[v] ||= 0
    hash[v] += 1
  end
  hash
end