Module: DaruLite::DataFrame::Sortable
- Included in:
- DaruLite::DataFrame
- Defined in:
- lib/daru_lite/data_frame/sortable.rb
Instance Method Summary collapse
-
#order=(order_array) ⇒ Object
Reorder the vectors in a dataframe.
-
#rotate_vectors(count = -1)) ⇒ Object
Return the dataframe with rotate vectors positions, the vector at position count is now the first vector of the dataframe.
-
#sort(vector_order, opts = {}) ⇒ Object
Non-destructive version of #sort!.
-
#sort!(vector_order, opts = {}) ⇒ Object
Sorts a dataframe (ascending/descending) in the given pripority sequence of vectors, with or without a block.
Instance Method Details
#order=(order_array) ⇒ Object
Reorder the vectors in a dataframe
18 19 20 21 22 |
# File 'lib/daru_lite/data_frame/sortable.rb', line 18 def order=(order_array) raise ArgumentError, 'Invalid order' unless order_array.tally == vectors.to_a.tally initialize(to_h, order: order_array) end |
#rotate_vectors(count = -1)) ⇒ Object
Return the dataframe with rotate vectors positions, the vector at position count is now the first vector of the dataframe. If only one vector in the dataframe, the dataframe is return without any change.
41 42 43 44 45 46 |
# File 'lib/daru_lite/data_frame/sortable.rb', line 41 def rotate_vectors(count = -1) return self unless vectors.many? self.order = vectors.to_a.rotate(count) self end |
#sort(vector_order, opts = {}) ⇒ Object
Non-destructive version of #sort!
155 156 157 |
# File 'lib/daru_lite/data_frame/sortable.rb', line 155 def sort(vector_order, opts = {}) dup.sort! vector_order, opts end |
#sort!(vector_order, opts = {}) ⇒ Object
Sorts a dataframe (ascending/descending) in the given pripority sequence of vectors, with or without a block.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/daru_lite/data_frame/sortable.rb', line 131 def sort!(vector_order, opts = {}) raise ArgumentError, 'Required atleast one vector name' if vector_order.empty? # To enable sorting with categorical data, # map categories to integers preserving their order old = convert_categorical_vectors vector_order block = sort_prepare_block vector_order, opts order = @index.size.times.sort(&block) new_index = @index.reorder order # To reverse map mapping of categorical data to integers restore_categorical_vectors old @data.each do |vector| vector.reorder! order end self.index = new_index self end |