Module: Daru::Core::Query

Defined in:
lib/daru/core/query.rb

Defined Under Namespace

Classes: BoolArray

Class Method Summary collapse

Class Method Details

.apply_scalar_operator(operator, data, other) ⇒ Object



41
42
43
# File 'lib/daru/core/query.rb', line 41

def apply_scalar_operator operator, data, other
  BoolArray.new(data.map { |d| !!d.send(operator, other) if d.respond_to?(operator) })
end

.apply_vector_operator(operator, vector, other) ⇒ Object



45
46
47
# File 'lib/daru/core/query.rb', line 45

def apply_vector_operator operator, vector, other
  BoolArray.new(vector.zip(other).map { |d, o| !!d.send(operator, o) })
end

.df_where(data_frame, bool_array) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/daru/core/query.rb', line 49

def df_where data_frame, bool_array
  vecs = data_frame.map do |vector|
    vector.where(bool_array)
  end

  Daru::DataFrame.new(
    vecs, order: data_frame.vectors, index: vecs[0].index, clone: false
  )
end

.vector_apply_where(dv, bool_array) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/daru/core/query.rb', line 73

def vector_apply_where dv, bool_array
  _data, new_index = fetch_new_data_and_index dv, bool_array
  all_index        = dv.index
  all_data         = all_index.map { |idx| new_index.include?(idx) ? yield(dv[idx]) : dv[idx] }

  resultant_dv = Daru::Vector.new all_data,
    index: dv.index.class.new(all_index),
    dtype: dv.dtype,
    type: dv.type,
    name: dv.name

  # Preserve categories order for category vector
  resultant_dv.categories = dv.categories if dv.category?
  resultant_dv
end

.vector_where(dv, bool_array) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/daru/core/query.rb', line 59

def vector_where dv, bool_array
  new_data, new_index = fetch_new_data_and_index dv, bool_array

  resultant_dv = Daru::Vector.new new_data,
    index: dv.index.class.new(new_index),
    dtype: dv.dtype,
    type: dv.type,
    name: dv.name

  # Preserve categories order for category vector
  resultant_dv.categories = dv.categories if dv.category?
  resultant_dv
end