Class: Daru::MultiIndex

Inherits:
Index
  • Object
show all
Includes:
Enumerable
Defined in:
lib/daru/index.rb

Overview

class Index

Instance Attribute Summary collapse

Attributes inherited from Index

#relation_hash

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Index

__new__, #_dump, _load, inherited, new, #slice

Constructor Details

#initialize(opts = {}) ⇒ MultiIndex

Returns a new instance of MultiIndex.

Raises:

  • (ArgumentError)


165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/daru/index.rb', line 165

def initialize opts={}
  labels = opts[:labels]
  levels = opts[:levels]

  raise ArgumentError, 
    "Must specify both labels and levels" unless labels and levels
  raise ArgumentError,
    "Labels and levels should be same size" if labels.size != levels.size
  raise ArgumentError,
    "Incorrect labels and levels" if incorrect_fields?(labels, levels)

  @labels = labels
  @levels = levels.map { |e| Hash[e.map.with_index.to_a]}
end

Instance Attribute Details

#labelsObject (readonly)

Returns the value of attribute labels.



159
160
161
# File 'lib/daru/index.rb', line 159

def labels
  @labels
end

Class Method Details

.from_arrays(arrays) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/daru/index.rb', line 191

def self.from_arrays arrays
  levels = arrays.map { |e| e.uniq.sort_by { |a| a.to_s  } }
  labels = []

  arrays.each_with_index do |arry, level_index|
    label = []
    level = levels[level_index]
    arry.each do |lvl|
      label << level.index(lvl)
    end

    labels << label
  end

  MultiIndex.new labels: labels, levels: levels
end

.from_tuples(tuples) ⇒ Object



208
209
210
# File 'lib/daru/index.rb', line 208

def self.from_tuples tuples
  from_arrays tuples.transpose
end

Instance Method Details

#&(other) ⇒ Object



292
293
294
# File 'lib/daru/index.rb', line 292

def & other
  MultiIndex.from_tuples(to_a & other.to_a)
end

#==(other) ⇒ Object



316
317
318
319
320
# File 'lib/daru/index.rb', line 316

def == other
  self.class == other.class  and 
  labels     == other.labels and 
  levels     == other.levels 
end

#[](*key) ⇒ Object



212
213
214
215
216
217
218
219
# File 'lib/daru/index.rb', line 212

def [] *key
  key.flatten!
  case
  when key[0].is_a?(Range) then retrieve_from_range(key[0])
  when (key[0].is_a?(Integer) and key.size == 1) then try_retrieve_from_integer(key[0])
  else retrieve_from_tuples(key)
  end
end

#drop_left_level(by = 1) ⇒ Object



284
285
286
# File 'lib/daru/index.rb', line 284

def drop_left_level by=1
  MultiIndex.from_arrays to_a.transpose[by..-1]
end

#dupObject



280
281
282
# File 'lib/daru/index.rb', line 280

def dup
  MultiIndex.new levels: levels.dup, labels: labels
end

#each(&block) ⇒ Object



151
152
153
# File 'lib/daru/index.rb', line 151

def each(&block)
  to_a.each(&block)  
end

#empty?Boolean

Returns:

  • (Boolean)


296
297
298
# File 'lib/daru/index.rb', line 296

def empty?
  @labels.flatten.empty? and @levels.all? { |l| l.empty? }
end

#include?(tuple) ⇒ Boolean

Returns:

  • (Boolean)


300
301
302
303
304
305
306
# File 'lib/daru/index.rb', line 300

def include? tuple
  tuple.flatten!
  tuple.each_with_index do |tup, i|
    return false unless @levels[i][tup]
  end
  true
end

#inspectObject



330
331
332
# File 'lib/daru/index.rb', line 330

def inspect
  "Daru::MultiIndex:#{self.object_id} (levels: #{levels}\nlabels: #{labels})"
end

#key(index) ⇒ Object

Raises:

  • (ArgumentError)


262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/daru/index.rb', line 262

def key index
  raise ArgumentError,
    "Key #{index} is too large" if index >= @labels[0].size

  level_indexes = 
  @labels.inject([]) do |memo, label|
    memo << label[index]
    memo
  end

  tuple = []
  level_indexes.each_with_index do |level_index, i|
    tuple << @levels[i].keys[level_index]
  end

  tuple
end

#levelsObject



161
162
163
# File 'lib/daru/index.rb', line 161

def levels
  @levels.map { |e| e.keys }
end

#map(&block) ⇒ Object



155
156
157
# File 'lib/daru/index.rb', line 155

def map(&block)
  to_a.map(&block)
end

#sizeObject



308
309
310
# File 'lib/daru/index.rb', line 308

def size
  @labels[0].size
end

#to_aObject



322
323
324
# File 'lib/daru/index.rb', line 322

def to_a
  (0...size).map { |e| key(e) }
end

#try_retrieve_from_integer(int) ⇒ Object



221
222
223
224
# File 'lib/daru/index.rb', line 221

def try_retrieve_from_integer int
  return retrieve_from_tuples([int]) if @levels[0].has_key?(int)
  int
end

#valuesObject



326
327
328
# File 'lib/daru/index.rb', line 326

def values
  Array.new(size) { |i| i }
end

#widthObject



312
313
314
# File 'lib/daru/index.rb', line 312

def width
  @levels.size
end

#|(other) ⇒ Object



288
289
290
# File 'lib/daru/index.rb', line 288

def | other
  MultiIndex.from_tuples(to_a | other.to_a)
end