Class: Daru::Core::MergeFrame

Inherits:
Object
  • Object
show all
Defined in:
lib/daru/core/merge.rb

Defined Under Namespace

Classes: NilSorter

Instance Method Summary collapse

Constructor Details

#initialize(left_df, right_df, opts = {}) ⇒ MergeFrame

Returns a new instance of MergeFrame.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/daru/core/merge.rb', line 20

def initialize left_df, right_df, opts={}
  @on = opts[:on]
  @keep_left, @keep_right = extract_left_right(opts[:how])

  validate_on!(left_df, right_df)

  key_sanitizer = ->(h) { sanitize_merge_keys(h.values_at(*on)) }

  @left = df_to_a(left_df)
  @left.sort_by!(&key_sanitizer)
  @left_key_values = @left.map(&key_sanitizer)

  @right = df_to_a(right_df)
  @right.sort_by!(&key_sanitizer)
  @right_key_values = @right.map(&key_sanitizer)

  @left_keys, @right_keys = merge_keys(left_df, right_df, on)
end

Instance Method Details

#joinObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/daru/core/merge.rb', line 39

def join
  res = []

  until left.empty? && right.empty?
    lkey = first_left_key
    rkey = first_right_key

    row(lkey, rkey).tap { |r| res << r if r }
  end

  Daru::DataFrame.new(res, order: left_keys.values + on + right_keys.values)
end