Class: Daru::MultiIndex
- Inherits:
-
Object
- Object
- Daru::MultiIndex
- Includes:
- Enumerable
- Defined in:
- lib/daru/multi_index.rb
Overview
Class for holding multi index on Vector and DataFrame.
Instance Attribute Summary collapse
-
#relation_hash ⇒ Object
readonly
Returns the value of attribute relation_hash.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Instance Method Summary collapse
- #+(other) ⇒ Object
-
#==(other) ⇒ Object
Compare two MultiIndex objects for equality based on the contents of their relation hashes.
- #[](*indexes) ⇒ Object
-
#dup ⇒ Object
Completely duplicate a MultiIndex object and its contents.
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#include?(tuple) ⇒ Boolean
Check whether a tuple or identifier number exists in the multi index.
-
#initialize(source, values = nil) ⇒ MultiIndex
constructor
Initialize a MultiIndex by passing a tuple of indexes.
-
#key(key) ⇒ Object
Obtain the tuple that correponds with the indentifier number.
- #map(&block) ⇒ Object
-
#to_a ⇒ Object
Convert a MultiIndex back to tuples (array of arrays).
Constructor Details
#initialize(source, values = nil) ⇒ MultiIndex
Initialize a MultiIndex by passing a tuple of indexes. The order assigned to the multi index corresponds to the position of the tuple in the array of tuples.
Although you can create your own hierarchially indexed Vectors and DataFrames, this class currently contains minimal error checking and is mainly used internally for summarizing, splitting and grouping of data.
Arguments
-
source
- The array of arrays from which the multi index is to be created.
Usage
tuples = [:a,:a,:b,:b].zip([:one,:two,:one,:two])
#=> [[:a, :one], [:a, :two], [:b, :one], [:b, :two]]
Daru::MultiIndex.new(tuples)
36 37 38 39 40 41 42 43 |
# File 'lib/daru/multi_index.rb', line 36 def initialize source, values=nil @relation_hash = {} @size = source.size values = Array.new(source.size) { |i| i } if values.nil? create_relation_hash source, values @relation_hash.freeze @values = values end |
Instance Attribute Details
#relation_hash ⇒ Object (readonly)
Returns the value of attribute relation_hash.
15 16 17 |
# File 'lib/daru/multi_index.rb', line 15 def relation_hash @relation_hash end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
16 17 18 |
# File 'lib/daru/multi_index.rb', line 16 def size @size end |
#values ⇒ Object (readonly)
Returns the value of attribute values.
17 18 19 |
# File 'lib/daru/multi_index.rb', line 17 def values @values end |
Instance Method Details
#+(other) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/daru/multi_index.rb', line 71 def + other other.flatten! tuples = to_a raise ArgumentError, "Incomplete tuple #{other}" unless tuples.all? { |t| t.size == other.size } Daru::MultiIndex.new(tuples << (other)) end |
#==(other) ⇒ Object
Compare two MultiIndex objects for equality based on the contents of their relation hashes. Does not take object_id into account.
86 87 88 89 |
# File 'lib/daru/multi_index.rb', line 86 def == other return false if size != other.size deep_compare @relation_hash, other.relation_hash end |
#[](*indexes) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/daru/multi_index.rb', line 45 def [] *indexes indexes.flatten! location = indexes[0] if location.is_a?(Symbol) result = read_relation_hash @relation_hash, indexes, 0 return nil if result.nil? result.is_a?(Integer) ? result : Daru::MultiIndex.new(*make_tuples(result)) else case location when Integer self[@relation_hash.keys[location]] when Range first = location.first last = location.last hsh = {} first.upto(last) do |index| key = @relation_hash.keys[index] hsh[key] = read_relation_hash(@relation_hash, [key], 0) end Daru::MultiIndex.new(*make_tuples(hsh)) end end end |
#dup ⇒ Object
Completely duplicate a MultiIndex object and its contents.
98 99 100 |
# File 'lib/daru/multi_index.rb', line 98 def dup Daru::MultiIndex.new to_a end |
#each(&block) ⇒ Object
6 7 8 9 |
# File 'lib/daru/multi_index.rb', line 6 def each(&block) to_a.each(&block) self end |
#empty? ⇒ Boolean
80 81 82 |
# File 'lib/daru/multi_index.rb', line 80 def empty? @relation_hash.empty? end |
#include?(tuple) ⇒ Boolean
Check whether a tuple or identifier number exists in the multi index. The argument tuple can either a complete or incomplete tuple, or a number.
104 105 106 107 108 |
# File 'lib/daru/multi_index.rb', line 104 def include? tuple tuple = [tuple] unless tuple.is_a?(Array) tuple.flatten! !!read_relation_hash(@relation_hash, tuple, 0) end |
#key(key) ⇒ Object
Obtain the tuple that correponds with the indentifier number.
Arguments
-
key
- A number for which the tuple is to be obtained.
Usage
mi.key(3) #=> [:a,:two,:baz]
119 120 121 122 |
# File 'lib/daru/multi_index.rb', line 119 def key key tuple = find_tuple_for(@relation_hash, key) tuple.empty? ? nil : tuple end |
#map(&block) ⇒ Object
11 12 13 |
# File 'lib/daru/multi_index.rb', line 11 def map(&block) to_a.map(&block) end |
#to_a ⇒ Object
Convert a MultiIndex back to tuples (array of arrays). Will retain the order of creation.
93 94 95 |
# File 'lib/daru/multi_index.rb', line 93 def to_a make_tuples(@relation_hash)[0] end |