Module: DaruLite::Vector::Missable

Extended by:
Gem::Deprecate
Included in:
DaruLite::Vector
Defined in:
lib/daru_lite/vector/missable.rb

Instance Method Summary collapse

Instance Method Details

#has_missing_data?Boolean Also known as: flawed?

Reports whether missing data is present in the Vector.

Returns:

  • (Boolean)


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_validObject

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.

Parameters:

  • as_a (Symbol) (defaults to: :vector)

    Passing :array will return only the elements

  • _duplicate (Symbol) (defaults to: true)

    In case no missing data is found in the



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

Examples:

dv = DaruLite::Vector.new([1, 2, 1, 4, nil, Float::NAN, 3, nil, Float::NAN])

 2.3.3 :068 > dv.rolling_fillna(:forward)
 => #<DaruLite::Vector(9)>
 0   1
 1   2
 2   1
 3   4
 4   4
 5   4
 6   3
 7   3
 8   3

Parameters:

  • direction (Symbol) (defaults to: :forward)

    (:forward, :backward) whether replacement value is preceeding or following



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