Module: DaruLite::DataFrame::Duplicatable

Extended by:
Gem::Deprecate
Included in:
DaruLite::DataFrame
Defined in:
lib/daru_lite/data_frame/duplicatable.rb

Instance Method Summary collapse

Instance Method Details

#clone(*vectors_to_clone) ⇒ Object

Returns a 'view' of the DataFrame, i.e the object ID's of vectors are preserved.

Arguments

vectors_to_clone - Names of vectors to clone. Optional. Will return a view of the whole data frame otherwise.



39
40
41
42
43
44
45
# File 'lib/daru_lite/data_frame/duplicatable.rb', line 39

def clone(*vectors_to_clone)
  vectors_to_clone.flatten! if ArrayHelper.array_of?(vectors_to_clone, Array)
  vectors_to_clone = @vectors.to_a if vectors_to_clone.empty?

  h = vectors_to_clone.map { |vec| [vec, self[vec]] }.to_h
  DaruLite::DataFrame.new(h, clone: false, order: vectors_to_clone, name: @name)
end

#clone_only_validObject

Returns a 'shallow' copy of DataFrame if missing data is not present, or a full copy of only valid data if missing data is present.



49
50
51
52
53
54
55
# File 'lib/daru_lite/data_frame/duplicatable.rb', line 49

def clone_only_valid
  if include_values?(*DaruLite::MISSING_VALUES)
    reject_values(*DaruLite::MISSING_VALUES)
  else
    clone
  end
end

#clone_structureObject

Only clone the structure of the DataFrame.



28
29
30
# File 'lib/daru_lite/data_frame/duplicatable.rb', line 28

def clone_structure
  DaruLite::DataFrame.new([], order: @vectors.dup, index: @index.dup, name: @name)
end

#dup(vectors_to_dup = nil) ⇒ Object

Duplicate the DataFrame entirely.

Arguments

  • vectors_to_dup - An Array specifying the names of Vectors to be duplicated. Will duplicate the entire DataFrame if not specified.


12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/daru_lite/data_frame/duplicatable.rb', line 12

def dup(vectors_to_dup = nil)
  # No argument: copy by position so a MultiIndex with duplicate tuples (which cannot be
  # re-looked-up unambiguously by name) is preserved intact.
  if vectors_to_dup.nil?
    return DaruLite::DataFrame.new(
      @data.map(&:dup), order: @vectors.dup, index: @index.dup, name: @name, clone: true
    )
  end

  src = vectors_to_dup.map { |vec| @data[@vectors.pos(vec)].dup }
  new_order = DaruLite::Index.new(vectors_to_dup)

  DaruLite::DataFrame.new src, order: new_order, index: @index.dup, name: @name, clone: true
end

#dup_only_valid(vecs = nil) ⇒ Object

Creates a new duplicate dataframe containing only rows without a single missing value.



59
60
61
62
63
64
65
66
# File 'lib/daru_lite/data_frame/duplicatable.rb', line 59

def dup_only_valid(vecs = nil)
  rows_with_nil = @data.map { |vec| vec.indexes(*DaruLite::MISSING_VALUES) }
                       .inject(&:concat)
                       .uniq

  row_indexes = @index.to_a
  (vecs.nil? ? self : dup(vecs)).row[*(row_indexes - rows_with_nil)]
end