lib/daru/vector.rb in daru-0.1.4.1 vs lib/daru/vector.rb in daru-0.1.5
- old
+ new
@@ -188,13 +188,15 @@
def plotting_library= lib
case lib
when :gruff, :nyaplot
@plotting_library = lib
- extend Module.const_get(
- "Daru::Plotting::Vector::#{lib.to_s.capitalize}Library"
- ) if Daru.send("has_#{lib}?".to_sym)
+ if Daru.send("has_#{lib}?".to_sym)
+ extend Module.const_get(
+ "Daru::Plotting::Vector::#{lib.to_s.capitalize}Library"
+ )
+ end
else
raise ArguementError, "Plotting library #{lib} not supported. "\
'Supported libraries are :nyaplot and :gruff'
end
end
@@ -289,11 +291,11 @@
modify_vector(indexes, val)
update_position_cache
end
- # Two vectors are equal if the have the exact same index values corresponding
+ # Two vectors are equal if they have the exact same index values corresponding
# with the exact same elements. Name is ignored.
def == other
case other
when Daru::Vector
@index == other.index && size == other.size &&
@@ -351,10 +353,11 @@
mod.apply_vector_operator operator, self, other
else
mod.apply_scalar_operator operator, @data,other
end
end
+ alias_method operator, method if operator != :== && operator != :!=
end
alias :gt :mt
alias :gteq :mteq
# Comparator for checking if any of the elements in *other* exist in self.
@@ -591,19 +594,10 @@
else
lv <=> rv
end
}
- def resort_index vector_index, opts
- if block_given?
- vector_index.sort { |(lv, _li), (rv, _ri)| yield(lv, rv) }
- else
- vector_index.sort(&DEFAULT_SORTER)
- end
- .tap { |res| res.reverse! unless opts[:ascending] }
- end
-
# Just sort the data and get an Array in return using Enumerable#sort.
# Non-destructive.
# :nocov:
def sorted_data &block
@data.to_a.sort(&block)
@@ -832,12 +826,14 @@
# dv.to_nmatrix
# # =>
# # [
# # [1, 2, 3] ]
def to_nmatrix axis=:horizontal
- raise ArgumentError, 'Can not convert to nmatrix'\
- 'because the vector is numeric' unless numeric? && !include?(nil)
+ unless numeric? && !include?(nil)
+ raise ArgumentError, 'Can not convert to nmatrix'\
+ 'because the vector is numeric'
+ end
case axis
when :horizontal
NMatrix.new [1, size], to_a
when :vertical
@@ -971,11 +967,12 @@
# # a 1
# # b 2
# # c 3
def reorder! order
@index = @index.reorder order
- @data = order.map { |i| @data[i] }
+ data_array = order.map { |i| @data[i] }
+ @data = cast_vector_to @dtype, data_array, @nm_dtype
update_position_cache
self
end
# Non-destructive version of #reorder!
@@ -988,16 +985,21 @@
def reindex new_index
dup.reindex!(new_index)
end
def index= idx
- raise ArgumentError,
- "Size of supplied index #{index.size} does not match size of DataFrame" if
- idx.size != size
- raise ArgumentError, 'Can only assign type Index and its subclasses.' unless
- idx.is_a?(Daru::Index)
+ idx = Index.coerce idx
+ if idx.size != size
+ raise ArgumentError,
+ "Size of supplied index #{idx.size} does not match size of Vector"
+ end
+
+ unless idx.is_a?(Daru::Index)
+ raise ArgumentError, 'Can only assign type Index and its subclasses.'
+ end
+
@index = idx
self
end
# Give the vector a new name
@@ -1326,10 +1328,14 @@
else
size.times.select { |i| include_with_nan? values, @data[i] }
end
end
+ def group_by(*args)
+ to_df.group_by(*args)
+ end
+
private
def nil_positions
@nil_positions ||
@nil_positions = size.times.select { |i| @data[i].nil? }
@@ -1535,8 +1541,17 @@
end
def update_position_cache
@nil_positions = nil
@nan_positions = nil
+ end
+
+ def resort_index vector_index, opts
+ if block_given?
+ vector_index.sort { |(lv, _li), (rv, _ri)| yield(lv, rv) }
+ else
+ vector_index.sort(&DEFAULT_SORTER)
+ end
+ .tap { |res| res.reverse! unless opts[:ascending] }
end
end
end