class Charty::PandasIndex

Public Class Methods

try_convert(obj) click to toggle source
# File lib/charty/index.rb, line 145
def self.try_convert(obj)
  case obj
  when PandasIndex
    obj
  when ->(x) { defined?(Pandas) && x.is_a?(Pandas::Index) }
    PandasIndex.new(obj)
  when RangeIndex, Range
    obj = obj.values if obj.is_a?(RangeIndex)
    stop = if obj.exclude_end?
             obj.end
           else
             obj.end + 1
           end
    PandasIndex.new(Pandas.RangeIndex.new(obj.begin, stop))
  when ->(x) { defined?(Enumerator::ArithmeticSequence) && x.is_a?(Enumerator::ArithmeticSequence) }
    stop = if obj.exclude_end?
             obj.end
           else
             obj.end + 1
           end
    PandasIndex.new(Pandas::RangeIndex.new(obj.begin, stop, obj.step))
  when Index, Array, DaruIndex, ->(x) { defined?(Daru) && x.is_a?(Daru::Index) }
    obj = obj.values if obj.is_a?(Index)
    PandasIndex.new(Pandas::Index.new(obj.to_a))
  else
    nil
  end
end

Public Instance Methods

==(other) click to toggle source
Calls superclass method Charty::Index#==
# File lib/charty/index.rb, line 180
def ==(other)
  case other
  when PandasIndex
    Numpy.all(values == other.values)
  when Index
    return false if length != other.length
    Numpy.all(values == other.values.to_a)
  else
    super
  end
end
each() { |self| ... } click to toggle source
# File lib/charty/index.rb, line 192
def each(&block)
  return enum_for(__method__) unless block_given?

  i, n = 0, length
  while i < n
    yield self[i]
    i += 1
  end
end
length() click to toggle source
# File lib/charty/index.rb, line 176
def length
  size
end
loc(key) click to toggle source
Calls superclass method Charty::Index#loc
# File lib/charty/index.rb, line 202
def loc(key)
  case values
  when Pandas::Index
    values.get_loc(key)
  else
    super
  end
end
union(other) click to toggle source
# File lib/charty/index.rb, line 211
def union(other)
  other = PandasIndex.try_convert(other)
  # NOTE: Using `sort=False` in pandas.Index#union does not produce pandas.RangeIndex.
  # TODO: Reconsider to use `sort=True` here.
  PandasIndex.new(values.union(other.values, sort: false))
end

Private Instance Methods

arithmetic_sequence?(x) click to toggle source
# File lib/charty/index.rb, line 218
        def arithmetic_sequence?(x)
  defined?(Enumerator::ArithmeticSequence) && x.is_a?(Enumerator::ArithmeticSequence)
end