Module: DaruLite::Vector::Missable
- Extended by:
- Gem::Deprecate
- Included in:
- DaruLite::Vector
- Defined in:
- lib/daru_lite/vector/missable.rb
Instance Method Summary collapse
-
#has_missing_data? ⇒ Boolean
(also: #flawed?)
Reports whether missing data is present in the Vector.
-
#n_valid ⇒ Object
number of non-missing elements.
-
#only_missing(as_a = :vector) ⇒ Object
Returns a Vector containing only missing data (preserves indexes).
-
#only_valid(as_a = :vector, _duplicate = true) ⇒ Object
Creates a new vector consisting only of non-nil data.
-
#replace_nils(replacement) ⇒ Object
Non-destructive version of #replace_nils!.
-
#replace_nils!(replacement) ⇒ Object
Replace all nils in the vector with the value passed as an argument.
-
#rolling_fillna(direction = :forward) ⇒ Object
Non-destructive version of rolling_fillna!.
-
#rolling_fillna!(direction = :forward) ⇒ Object
Rolling fillna replace all Float::NAN and NIL values with the preceeding or following value.
Instance Method Details
#has_missing_data? ⇒ Boolean Also known as: flawed?
Reports whether missing data is present in the Vector.
7 8 9 |
# File 'lib/daru_lite/vector/missable.rb', line 7 def has_missing_data? # rubocop:disable Naming/PredicateName !indexes(*DaruLite::MISSING_VALUES).empty? end |
#n_valid ⇒ Object
number of non-missing elements
72 73 74 |
# File 'lib/daru_lite/vector/missable.rb', line 72 def n_valid size - indexes(*DaruLite::MISSING_VALUES).size end |
#only_missing(as_a = :vector) ⇒ Object
Returns a Vector containing only missing data (preserves indexes).
106 107 108 109 110 111 112 113 |
# File 'lib/daru_lite/vector/missable.rb', line 106 def only_missing(as_a = :vector) case as_a when :vector self[*indexes(*DaruLite::MISSING_VALUES)] when :array self[*indexes(*DaruLite::MISSING_VALUES)].to_a end end |
#only_valid(as_a = :vector, _duplicate = true) ⇒ Object
Creates a new vector consisting only of non-nil data
Arguments
as an Array. Otherwise will return a DaruLite::Vector.
vector, setting this to false will return the same vector. Otherwise, a duplicate will be returned irrespective of presence of missing data.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/daru_lite/vector/missable.rb', line 89 def only_valid(as_a = :vector, _duplicate = true) # rubocop:disable Style/OptionalBooleanParameter # FIXME: Now duplicate is just ignored. # There are no spec that fail on this case, so I'll leave it # this way for now - zverok, 2016-05-07 new_index = @index.to_a - indexes(*DaruLite::MISSING_VALUES) new_vector = new_index.map { |idx| self[idx] } if as_a == :vector DaruLite::Vector.new new_vector, index: new_index, name: @name, dtype: dtype else new_vector end end |
#replace_nils(replacement) ⇒ Object
Non-destructive version of #replace_nils!
67 68 69 |
# File 'lib/daru_lite/vector/missable.rb', line 67 def replace_nils(replacement) dup.replace_nils!(replacement) end |
#replace_nils!(replacement) ⇒ Object
Replace all nils in the vector with the value passed as an argument. Destructive. See #replace_nils for non-destructive version
Arguments
-
replacement- The value which should replace all nils
20 21 22 23 24 25 26 |
# File 'lib/daru_lite/vector/missable.rb', line 20 def replace_nils!(replacement) indexes(*DaruLite::MISSING_VALUES).each do |idx| self[idx] = replacement end self end |
#rolling_fillna(direction = :forward) ⇒ Object
Non-destructive version of rolling_fillna!
62 63 64 |
# File 'lib/daru_lite/vector/missable.rb', line 62 def rolling_fillna(direction = :forward) dup.rolling_fillna!(direction) end |
#rolling_fillna!(direction = :forward) ⇒ Object
Rolling fillna replace all Float::NAN and NIL values with the preceeding or following value
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/daru_lite/vector/missable.rb', line 48 def rolling_fillna!(direction = :forward) enum = direction == :forward ? index : index.reverse_each last_valid_value = 0 enum.each do |idx| if valid_value?(self[idx]) last_valid_value = self[idx] else self[idx] = last_valid_value end end self end |