Module: DaruLite::Vector::Filterable
- Included in:
- DaruLite::Vector
- Defined in:
- lib/daru_lite/vector/filterable.rb
Instance Method Summary collapse
-
#apply_where(bool_array) ⇒ DaruLite::Vector
Return a new vector based on the contents of a boolean array and &block.
-
#delete_if ⇒ Object
Delete an element if block returns true.
-
#keep_if ⇒ Object
Keep an element if block returns true.
-
#only_numerics ⇒ Object
Returns a Vector with only numerical data.
-
#reject_values(*values) ⇒ DaruLite::Vector
Return a vector with specified values removed.
-
#uniq ⇒ Object
Keep only unique elements of the vector alongwith their indexes.
-
#where(bool_array) ⇒ Object
Return a new vector based on the contents of a boolean array.
Instance Method Details
#apply_where(bool_array) ⇒ DaruLite::Vector
Return a new vector based on the contents of a boolean array and &block.
61 62 63 |
# File 'lib/daru_lite/vector/filterable.rb', line 61 def apply_where(bool_array, &) DaruLite::Core::Query.vector_apply_where(self, bool_array, &) end |
#delete_if ⇒ Object
Delete an element if block returns true. Destructive.
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/daru_lite/vector/filterable.rb', line 74 def delete_if return to_enum(:delete_if) unless block_given? keep_e, keep_i = each_with_index.reject { |n, _i| yield(n) }.transpose @data = cast_vector_to @dtype, keep_e @index = DaruLite::Index.new(keep_i) update_position_cache self end |
#keep_if ⇒ Object
Keep an element if block returns true. Destructive.
88 89 90 91 92 |
# File 'lib/daru_lite/vector/filterable.rb', line 88 def keep_if return to_enum(:keep_if) unless block_given? delete_if { |val| !yield(val) } end |
#only_numerics ⇒ Object
Returns a Vector with only numerical data. Missing data is included but non-Numeric objects are excluded. Preserves index.
118 119 120 121 122 123 124 125 |
# File 'lib/daru_lite/vector/filterable.rb', line 118 def only_numerics numeric_indexes = each_with_index .select { |v, _i| v.is_a?(Numeric) || v.nil? } .map(&:last) self[*numeric_indexes] end |
#reject_values(*values) ⇒ DaruLite::Vector
Return a vector with specified values removed
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/daru_lite/vector/filterable.rb', line 103 def reject_values(*values) resultant_pos = size.times.to_a - positions(*values) dv = at(*resultant_pos) # Handle the case when number of positions is 1 # and hence #at doesn't return a vector if dv.is_a?(DaruLite::Vector) dv else pos = resultant_pos.first at(pos..pos) end end |
#uniq ⇒ Object
Keep only unique elements of the vector alongwith their indexes.
66 67 68 69 70 71 |
# File 'lib/daru_lite/vector/filterable.rb', line 66 def uniq uniq_vector = @data.uniq new_index = uniq_vector.map { |element| index_of(element) } DaruLite::Vector.new uniq_vector, name: @name, index: new_index, dtype: @dtype end |
#where(bool_array) ⇒ Object
Return a new vector based on the contents of a boolean array. Use with the comparator methods to obtain meaningful results. See this notebook for a good overview of using #where.
40 41 42 |
# File 'lib/daru_lite/vector/filterable.rb', line 40 def where(bool_array) DaruLite::Core::Query.vector_where self, bool_array end |