Module: DaruLite::DataFrame::Queryable

Included in:
DaruLite::DataFrame
Defined in:
lib/daru_lite/data_frame/queryable.rb

Instance Method Summary collapse

Instance Method Details

#all?(axis = :vector) ⇒ Boolean

Works like Array#all?

Examples:

Using all?

df = DaruLite::DataFrame.new({a: [1,2,3,4,5], b: ['a', 'b', 'c', 'd', 'e']})
df.all?(:row) do |row|
  row[:a] < 10
end #=> true

Parameters:

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

    (:vector) The axis to iterate over. Can be :vector or :row. A DaruLite::Vector object is yielded in the block.

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
# File 'lib/daru_lite/data_frame/queryable.rb', line 56

def all?(axis = :vector, &)
  if %i[vector column].include?(axis)
    @data.all?(&)
  elsif axis == :row
    each_row.all?(&)
  else
    raise ArgumentError, "Unidentified axis #{axis}"
  end
end

#any?(axis = :vector) ⇒ Boolean

Works like Array#any?.

Examples:

Using any?

df = DaruLite::DataFrame.new({a: [1,2,3,4,5], b: ['a', 'b', 'c', 'd', 'e']})
df.any?(:row) do |row|
  row[:a] < 3 and row[:b] == 'b'
end #=> true

Parameters:

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

    (:vector) The axis to iterate over. Can be :vector or :row. A DaruLite::Vector object is yielded in the block.

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/daru_lite/data_frame/queryable.rb', line 34

def any?(axis = :vector, &)
  if %i[vector column].include?(axis)
    @data.any?(&)
  elsif axis == :row
    each_row do |row|
      return true if yield(row)
    end
    false
  else
    raise ArgumentError, "Unidentified axis #{axis}"
  end
end

#has_vector?(vector) ⇒ Boolean

Check if a vector is present

Returns:

  • (Boolean)


5
6
7
# File 'lib/daru_lite/data_frame/queryable.rb', line 5

def has_vector?(vector)
  @vectors.include? vector
end

#include_values?(*values) ⇒ true, false

Check if any of given values occur in the data frame

Examples:

df = DaruLite::DataFrame.new({
  a: [1,    2,          3,   nil,        Float::NAN, nil, 1,   7],
  b: [:a,  :b,          nil, Float::NAN, nil,        3,   5,   8],
  c: ['a',  Float::NAN, 3,   4,          3,          5,   nil, 7]
}, index: 11..18)
df.include_values? nil
# => true

Parameters:

  • values (Array)

    to check for

Returns:

  • (true, false)

    true if any of the given values occur in the dataframe, false otherwise



21
22
23
# File 'lib/daru_lite/data_frame/queryable.rb', line 21

def include_values?(*values)
  @data.any? { |vec| vec.include_values?(*values) }
end