class Charty::VectorAdapters::PandasSeriesAdapter

Attributes

data[R]

Public Class Methods

new(data) click to toggle source
# File lib/charty/vector_adapters/pandas_adapter.rb, line 16
def initialize(data)
  @data = check_data(data)
end
supported?(data) click to toggle source
# 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

Public Instance Methods

[](key) click to toggle source
# File lib/charty/vector_adapters/pandas_adapter.rb, line 64
def [](key)
  case key
  when Charty::Vector
    where(key)
  else
    data[key]
  end
end
boolean?() click to toggle source
# File lib/charty/vector_adapters/pandas_adapter.rb, line 122
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?() click to toggle source
# File lib/charty/vector_adapters/pandas_adapter.rb, line 137
def categorical?
  Pandas.api.types.is_categorical_dtype(data.dtype)
end
categories() click to toggle source
# File lib/charty/vector_adapters/pandas_adapter.rb, line 141
def categories
  data.cat.categories.to_a if categorical?
end
compare_data_equality(other) click to toggle source
# 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_na() click to toggle source
# File lib/charty/vector_adapters/pandas_adapter.rb, line 172
def drop_na
  Charty::Vector.new(data.dropna)
end
each() { |iloc| ... } click to toggle source
# File lib/charty/vector_adapters/pandas_adapter.rb, line 75
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?() click to toggle source
# File lib/charty/vector_adapters/pandas_adapter.rb, line 85
def empty?
  data.size == 0
end
eq(val) click to toggle source
# File lib/charty/vector_adapters/pandas_adapter.rb, line 176
def eq(val)
  Charty::Vector.new((data == val),
                     index: index,
                     name: name)
end
group_by(grouper) click to toggle source
# File lib/charty/vector_adapters/pandas_adapter.rb, line 149
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
index() click to toggle source
# File lib/charty/vector_adapters/pandas_adapter.rb, line 24
def index
  PandasIndex.new(data.index)
end
index=(new_index) click to toggle source
# 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) click to toggle source
# File lib/charty/vector_adapters/pandas_adapter.rb, line 203
def inverse_log_scale(method)
  Charty::Vector.new(Numpy.power(10, data))
end
log_scale(method) click to toggle source
# File lib/charty/vector_adapters/pandas_adapter.rb, line 199
def log_scale(method)
  Charty::Vector.new(Numpy.log10(data))
end
mean() click to toggle source
# File lib/charty/vector_adapters/pandas_adapter.rb, line 186
def mean
  data.mean()
end
notnull() click to toggle source
# File lib/charty/vector_adapters/pandas_adapter.rb, line 182
def notnull
  Charty::Vector.new(data.notnull, index: index, name: name)
end
numeric?() click to toggle source
# File lib/charty/vector_adapters/pandas_adapter.rb, line 133
def numeric?
  Pandas.api.types.is_numeric_dtype(data.dtype)
end
percentile(q) click to toggle source
# File lib/charty/vector_adapters/pandas_adapter.rb, line 194
def percentile(q)
  q = q.map {|x| x / 100.0 }
  data.quantile(q)
end
stdev(population: false) click to toggle source
# File lib/charty/vector_adapters/pandas_adapter.rb, line 190
def stdev(population: false)
  data.std(ddof: population ? 0 : 1)
end
unique_values() click to toggle source
# File lib/charty/vector_adapters/pandas_adapter.rb, line 145
def unique_values
  data.unique.to_a
end
values_at(*indices) click to toggle source
# File lib/charty/vector_adapters/pandas_adapter.rb, line 89
def values_at(*indices)
  data.take(indices).to_a
end
where(mask) click to toggle source
# File lib/charty/vector_adapters/pandas_adapter.rb, line 93
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) click to toggle source
# File lib/charty/vector_adapters/pandas_adapter.rb, line 108
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