class Charty::VectorAdapters::NumpyAdapter

Attributes

data[R]

Public Class Methods

new(data) click to toggle source
# File lib/charty/vector_adapters/numpy_adapter.rb, line 16
def initialize(data)
  @data = check_data(data)
  self.index = index || RangeIndex.new(0 ... length)
end
supported?(data) click to toggle source
# File lib/charty/vector_adapters/numpy_adapter.rb, line 6
def self.supported?(data)
  return false unless defined?(Numpy::NDArray)
  case data
  when Numpy::NDArray
    true
  else
    false
  end
end

Public Instance Methods

boolean?() click to toggle source
# File lib/charty/vector_adapters/numpy_adapter.rb, line 68
def boolean?
  builtins = PyCall.builtins
  case
  when builtins.issubclass(data.dtype.type, Numpy.bool_)
    true
  when builtins.issubclass(data.dtype.type, Numpy.object_)
    i, n = 0, data.size
    while i < n
      case data[i]
      when nil, true, false
        # do nothing
      else
        return false
      end
      i += 1
    end
    true
  else
    false
  end
end
categorical?() click to toggle source
# File lib/charty/vector_adapters/numpy_adapter.rb, line 95
def categorical?
  false
end
categories() click to toggle source
# File lib/charty/vector_adapters/numpy_adapter.rb, line 99
def categories
  nil
end
compare_data_equality(other) click to toggle source
# File lib/charty/vector_adapters/numpy_adapter.rb, line 25
def compare_data_equality(other)
  case other
  when NumpyAdapter, PandasSeriesAdapter
    Numpy.all(data == other.data)
  when BaseAdapter
    Numpy.all(data == other.data.to_a)
  else
    false
  end
end
drop_na() click to toggle source
# File lib/charty/vector_adapters/numpy_adapter.rb, line 129
def drop_na
  where_is_na = if numeric?
                  Numpy.isnan(data)
                else
                  (data == nil)
                end
  Charty::Vector.new(data[Numpy.logical_not(where_is_na)])
end
each() { |data| ... } click to toggle source
# File lib/charty/vector_adapters/numpy_adapter.rb, line 54
def each
  return enum_for(__method__) unless block_given?

  i, n = 0, data.size
  while i < n
    yield data[i]
    i += 1
  end
end
empty?() click to toggle source
# File lib/charty/vector_adapters/numpy_adapter.rb, line 64
def empty?
  data.size == 0
end
eq(val) click to toggle source
# File lib/charty/vector_adapters/numpy_adapter.rb, line 138
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/numpy_adapter.rb, line 107
def group_by(grouper)
  case grouper
  when Numpy::NDArray,
       ->(x) { defined?(Pandas::Series) && x.is_a?(Pandas::Series) }
    # Nothing todo
  when Charty::Vector
    case grouper.data
    when Numpy::NDArray
      grouper = grouper.data
    else
      grouper = Numpy.asarray(grouper.to_a)
    end
  else
    grouper = Numpy.asarray(Array.try_convert(grouper))
  end

  group_keys = Numpy.unique(grouper).to_a
  group_keys.map { |g|
    [g, Charty::Vector.new(data[grouper == g])]
  }.to_h
end
mean() click to toggle source
# File lib/charty/vector_adapters/numpy_adapter.rb, line 159
def mean
  Numpy.mean(data)
end
notnull() click to toggle source
# File lib/charty/vector_adapters/numpy_adapter.rb, line 144
def notnull
  case
  when PyCall.builtins.issubclass(data.dtype.type, Numpy.object_)
    i, n = 0, length
    notnull_data = Numpy::NDArray.new(n, dtype: :bool)
    while i < n
      notnull_data[i] = ! Util.missing?(data[i])
      i += 1
    end
  else
    notnull_data = Numpy.isnan(data)
  end
  Charty::Vector.new(notnull_data, index: index, name: name)
end
numeric?() click to toggle source
# File lib/charty/vector_adapters/numpy_adapter.rb, line 90
def numeric?
  # TODO: Handle object array
  PyCall.builtins.issubclass(data.dtype.type, PyCall.tuple([Numpy.number, Numpy.bool_]))
end
percentile(q) click to toggle source
# File lib/charty/vector_adapters/numpy_adapter.rb, line 167
def percentile(q)
  Numpy.nanpercentile(data, q)
end
stdev(population: false) click to toggle source
# File lib/charty/vector_adapters/numpy_adapter.rb, line 163
def stdev(population: false)
  Numpy.std(data, ddof: population ? 0 : 1)
end
unique_values() click to toggle source
# File lib/charty/vector_adapters/numpy_adapter.rb, line 103
def unique_values
  Numpy.unique(data).to_a
end
where(mask) click to toggle source
# File lib/charty/vector_adapters/numpy_adapter.rb, line 39
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 = Numpy.asarray(masked_data, dtype: data.dtype)
  end
  Charty::Vector.new(masked_data, index: masked_index, name: name)
end