Class: CADataFrame::LocAccessor

Inherits:
Object
  • Object
show all
Defined in:
lib/carray-dataframe/loc_accessor.rb

Instance Method Summary collapse

Constructor Details

#initialize(dataframe) ⇒ LocAccessor

Returns a new instance of LocAccessor.



5
6
7
# File 'lib/carray-dataframe/loc_accessor.rb', line 5

def initialize (dataframe)
  @dataframe = dataframe
end

Instance Method Details

#[](*argv) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/carray-dataframe/loc_accessor.rb', line 9

def [] (*argv)
  @dataframe.instance_eval {
    index = argv.first
    case index
    when nil
    when CArray
    when Integer      ### df[3]
      index = [index]
    when Range        ### df["a".."d"]
      if @row_index
        idx1 = @row_index.search(index.begin)
        idx2 = @row_index.search(index.end)
        if idx1 and idx2
          index = idx1..idx2
        elsif index.begin.is_a?(Integer) || index.end.is_a?(Integer)
          iloc[*argv]
        else
          raise "invalid index '#{index}'"
        end
      end
    when Array
      if index[0].is_a?(Range)
        if @row_index
          range = index[0]
          idx1 = @row_index.search(range.begin)
          idx2 = @row_index.search(range.end)
          if idx1 and idx2
            range = idx1..idx2
          elsif range.begin.is_a?(Integer) || range.end.is_a?(Integer)
          else
            raise "invalid index '#{index}'"
          end
          index[0] = range
        end            
      end
    else
      if @row_index
        idx1 = @row_index.search(index)
        if idx1
          index = [idx1]
        else
          raise "invalid index '#{index}'"
        end
      end
    end
    column_selector = select_columns(argv[1])
    columns = {}
    column_selector.each do |name|
      columns[name] = @column_data[name][index] ### df[...]
    end
    return CADataFrame.new(columns, index: @row_index ? @row_index[index] : nil)
  }
end

#[]=(*argv) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/carray-dataframe/loc_accessor.rb', line 63

def []= (*argv)
  value = argv.pop
  @dataframe.instance_eval {
    index = argv.first
    case index
    when nil
    when CArray          
    when Range         ### df["a".."d"] = value
      if @row_index
        idx1 = @row_index.search(index.begin)
        idx2 = @row_index.search(index.end)
        if idx1 and idx2
          index = idx1..idx2
        elsif index.begin.is_a?(Integer) || index.end.is_a?(Integer)
          iloc[*argv] = value
          return
        else
          raise "invalid index '#{index}'"
        end
      end
    when Array
      if index[0].is_a?(Range)
        if @row_index
          range = index[0]
          idx1 = @row_index.search(range.begin)
          idx2 = @row_index.search(range.end)
          if idx1 and idx2
            range = idx1..idx2
          elsif range.begin.is_a?(Integer) || range.end.is_a?(Integer)
          else
            raise "invalid index '#{index}'"
          end
          index[0] = range
        end            
      end
    else
      if @row_index
        idx1 = @row_index.search(index)
        if idx1
          index = [idx1]
        else
          raise "invalid index '#{index}'"
        end
      end
    end
    column_selector = select_columns(argv[1])
    case value
    when Hash          ### value = {"a"=> [1,2,3], ... }
      value = value.map{|k,v| [k.to_s, v]}.to_h
      column_selector.each do |name|
        @column_data[name][index] = value[name]
      end
    when Array
      case value.first
      when Hash        ### value = [{"a"=>1,"b"=>11}, {"a"=>2,""=>12} ...]
        table = {}
        column_selector.each do |name|
          table[name] = []
        end
        value.each do |hash|
          hash = hash.map{|k,v| [k.to_s, v]}.to_h
          column_selector.each do |name|
            table[name] << hash[name]
          end
        end
        column_selector.each do |name|
          @column_data[name][index] = table[name]
        end
      else             ### value = [[1,11],[2,12],...]
        value = value.transpose
        column_selector.each_with_index do |name, k|
          @column_data[name][index] = value[k]
        end
      end
    else               ### value = any value
      column_selector.each_with_index do |name, k|
        @column_data[name][index] = value
      end
    end
  }
end