Module: DF::ColumnManagement

Included in:
DataFrame
Defined in:
lib/data_frame/core/column_management.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#append!(column_name, value = nil) ⇒ Object

Adds a unique column to the table

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/data_frame/core/column_management.rb', line 23

def append!(column_name, value=nil)
  raise ArgumentError, "Can't have duplicate column names" if self.labels.include?(column_name)
  self.labels << column_name.to_underscore_sym
  if value.is_a?(Array)
    self.items.each_with_index do |item, i|
      item << value[i]
    end
  else
    self.items.each do |item|
      item << value
    end
  end
  # Because we are tainting the sub arrays, the TaintableArray doesn't know it's been changed.
  self.items.taint
end

#drop!(*labels) ⇒ Object

Drop one or more columns



65
66
67
68
69
70
# File 'lib/data_frame/core/column_management.rb', line 65

def drop!(*labels)
  labels.each do |label|
    drop_one!(label)
  end
  self
end

#drop_one!(label) ⇒ Object

Drop a single column



73
74
75
76
77
78
79
80
81
# File 'lib/data_frame/core/column_management.rb', line 73

def drop_one!(label)
  i = self.labels.index(label)
  return nil unless i
  self.items.each do |item|
    item.delete_at(i)
  end
  self.labels.delete_at(i)
  self
end

#move_to_last!(orig_name) ⇒ Object

Raises:

  • (ArgumentError)


4
5
6
7
8
9
10
# File 'lib/data_frame/core/column_management.rb', line 4

def move_to_last!(orig_name)
  raise ArgumentError, "Column not found" unless self.labels.include?(orig_name)
  new_name = (orig_name.to_s + "_a_unique_name").to_sym
  self.append!(new_name, self.render_column(orig_name))
  self.drop!(orig_name)
  self.rename!(orig_name, new_name)
end

#rename!(new_name, orig_name) ⇒ Object

In the order of alias: new_name, orig_name

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
# File 'lib/data_frame/core/column_management.rb', line 13

def rename!(new_name, orig_name)
  new_name = new_name.to_underscore_sym
  orig_name = orig_name.to_underscore_sym
  raise ArgumentError, "Column not found" unless self.labels.include?(orig_name)
  raise ArgumentError, "Cannot name #{orig_name} to #{new_name}, that column already exists." if self.labels.include?(new_name)
  i = self.labels.index(orig_name)
  self.labels[i] = new_name
end

#replace!(column, values = nil, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/data_frame/core/column_management.rb', line 39

def replace!(column, values=nil, &block)
  column = validate_column(column)
  if not values
    values = self.send(column)
    values.map! {|e| block.call(e)}
  end
  replace_column!(column, values)
  self
end

#replace_column!(column, values) ⇒ Object

Replace a single column with an array of values. It is helpful to have the values the same size as the rest of the data frame.



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/data_frame/core/column_management.rb', line 52

def replace_column!(column, values)
  column = validate_column(column)
  index = self.labels.index(column)
  list = []
  self.items.each_with_index do |item, i|
    consolidated = item
    consolidated[index] = values[i]
    list << consolidated
  end
  @items = list.dup
end

#subset_from_columns(*cols) ⇒ Object

Creates a new data frame, only with the specified columns.



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/data_frame/core/column_management.rb', line 84

def subset_from_columns(*cols)
  new_labels = self.labels.inject([]) do |list, label|
    list << label if cols.include?(label)
    list
  end
  new_data_frame = DataFrame.new(*self.labels)
  new_data_frame.import(self.items)
  self.labels.each do |label|
    new_data_frame.drop!(label) unless new_labels.include?(label)
  end
  new_data_frame
end