Class: Charty::VectorAdapters::PandasSeriesAdapter

Inherits:
BaseAdapter
  • Object
show all
Defined in:
lib/charty/vector_adapters/pandas_adapter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseAdapter

#==, adapter_name

Constructor Details

#initialize(data) ⇒ PandasSeriesAdapter

Returns a new instance of PandasSeriesAdapter.



16
17
18
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 16

def initialize(data)
  @data = check_data(data)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



20
21
22
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 20

def data
  @data
end

Class Method Details

.supported?(data) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
9
10
11
12
13
14
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 6

def self.supported?(data)
  return false unless defined?(Pandas::Series)
  case data
  when Pandas::Series
    true
  else
    false
  end
end

Instance Method Details

#[](key) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 68

def [](key)
  case key
  when Charty::Vector
    where(key)
  else
    data[key]
  end
end

#boolean?Boolean

Returns:

  • (Boolean)


126
127
128
129
130
131
132
133
134
135
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 126

def boolean?
  case
  when Pandas.api.types.is_bool_dtype(data.dtype)
    true
  when Pandas.api.types.is_object_dtype(data.dtype)
    data.isin([nil, false, true]).all()
  else
    false
  end
end

#categorical?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 141

def categorical?
  Pandas.api.types.is_categorical_dtype(data.dtype)
end

#categoriesObject



145
146
147
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 145

def categories
  data.cat.categories.to_a if categorical?
end

#compare_data_equality(other) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 41

def compare_data_equality(other)
  case other
  when PandasSeriesAdapter
    return data.equals(other.data)
  when NumpyAdapter
    other = other.data
  when NArrayAdapter
    case other.data
    when Numo::Bit
      other = other.data.to_a
      other.map! {|x| [false, true][x] }
    else
      other = other.data.to_a
    end
  when BaseAdapter
    other = other.data.to_a
  else
    return false
  end

  data.equals(Pandas::Series.new(other, index: data.index))
end

#drop_naObject



176
177
178
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 176

def drop_na
  Charty::Vector.new(data.dropna)
end

#eachObject



79
80
81
82
83
84
85
86
87
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 79

def each
  return enum_for(__method__) unless block_given?

  i, n = 0, data.size
  while i < n
    yield data.iloc[i]
    i += 1
  end
end

#empty?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 89

def empty?
  data.size == 0
end

#eq(val) ⇒ Object



180
181
182
183
184
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 180

def eq(val)
  Charty::Vector.new((data == val),
                     index: index,
                     name: name)
end

#group_by(grouper) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 153

def group_by(grouper)
  case grouper
  when Pandas::Series
    group_keys = grouper.unique.to_a
    groups = data.groupby(grouper)
    group_keys.map {|g|
      g_vals = groups.get_group(g) rescue []
      [g, Charty::Vector.new(g_vals)]
    }.to_h
  when Charty::Vector
    case grouper.adapter
    when self.class
      group_by(grouper.data)
    else
      grouper = Pandas::Series.new(grouper.to_a)
      group_by(grouper)
    end
  else
    grouper = Pandas::Series.new(Array(grouper))
    group_by(grouper)
  end
end

#iloc(i) ⇒ Object



64
65
66
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 64

def iloc(i)
  data.iloc[i]
end

#indexObject



24
25
26
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 24

def index
  PandasIndex.new(data.index)
end

#index=(new_index) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 28

def index=(new_index)
  case new_index
  when PandasIndex
    data.index = new_index.values
  when Index
    data.index = new_index.to_a
  else
    data.index = new_index
  end
end

#inverse_log_scale(method) ⇒ Object



207
208
209
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 207

def inverse_log_scale(method)
  Charty::Vector.new(Numpy.power(10, data))
end

#log_scale(method) ⇒ Object



203
204
205
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 203

def log_scale(method)
  Charty::Vector.new(Numpy.log10(data))
end

#meanObject



190
191
192
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 190

def mean
  data.mean()
end

#notnullObject



186
187
188
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 186

def notnull
  Charty::Vector.new(data.notnull, index: index, name: name)
end

#numeric?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 137

def numeric?
  Pandas.api.types.is_numeric_dtype(data.dtype)
end

#percentile(q) ⇒ Object



198
199
200
201
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 198

def percentile(q)
  q = q.map {|x| x / 100.0 }
  data.quantile(q)
end

#stdev(population: false) ⇒ Object



194
195
196
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 194

def stdev(population: false)
  data.std(ddof: population ? 0 : 1)
end

#unique_valuesObject



149
150
151
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 149

def unique_values
  data.unique.to_a
end

#values_at(*indices) ⇒ Object



93
94
95
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 93

def values_at(*indices)
  data.take(indices).to_a
end

#where(mask) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 97

def where(mask)
  mask = check_mask_vector(mask)
  case mask.data
  when Numpy::NDArray,
       ->(x) { defined?(Pandas::Series) && x.is_a?(Pandas::Series) }
    mask_data = Numpy.asarray(mask.data, dtype: :bool)
    masked_data = data[mask_data]
    masked_index = mask_data.nonzero()[0].to_a.map {|i| index[i] }
  else
    masked_data, masked_index = where_in_array(mask)
    masked_data = Pandas::Series.new(masked_data, dtype: data.dtype)
  end
  Charty::Vector.new(masked_data, index: masked_index, name: name)
end

#where_in_array(mask) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/charty/vector_adapters/pandas_adapter.rb', line 112

def where_in_array(mask)
  mask = check_mask_vector(mask)
  masked_data = []
  masked_index = []
  mask.each_with_index do |f, i|
    case f
    when true, 1
      masked_data << data.iloc[i]
      masked_index << index[i]
    end
  end
  return masked_data, masked_index
end