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
-
#clone(*vectors_to_clone) ⇒ Object
Returns a ‘view’ of the DataFrame, i.e the object ID’s of vectors are preserved.
-
#clone_only_valid ⇒ Object
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.
-
#clone_structure ⇒ Object
Only clone the structure of the DataFrame.
-
#dup(vectors_to_dup = nil) ⇒ Object
Duplicate the DataFrame entirely.
-
#dup_only_valid(vecs = nil) ⇒ Object
Creates a new duplicate dataframe containing only rows without a single missing value.
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.
33 34 35 36 37 38 39 |
# File 'lib/daru_lite/data_frame/duplicatable.rb', line 33 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_valid ⇒ Object
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.
43 44 45 46 47 48 49 |
# File 'lib/daru_lite/data_frame/duplicatable.rb', line 43 def clone_only_valid if include_values?(*DaruLite::MISSING_VALUES) reject_values(*DaruLite::MISSING_VALUES) else clone end end |
#clone_structure ⇒ Object
Only clone the structure of the DataFrame.
22 23 24 |
# File 'lib/daru_lite/data_frame/duplicatable.rb', line 22 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 |
# File 'lib/daru_lite/data_frame/duplicatable.rb', line 12 def dup(vectors_to_dup = nil) vectors_to_dup ||= @vectors.to_a 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.
53 54 55 56 57 58 59 60 |
# File 'lib/daru_lite/data_frame/duplicatable.rb', line 53 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 |