Class: Charty::VectorAdapters::ArrayAdapter
Instance Attribute Summary
Attributes included from IndexSupport
#index
Attributes included from NameSupport
#name
Attributes inherited from BaseAdapter
#data
Class Method Summary
collapse
Instance Method Summary
collapse
#[], #[]=
Methods inherited from BaseAdapter
#==, adapter_name, #mean, #stdev, #where_in_array
#missing_value?
Constructor Details
#initialize(data, index: nil) ⇒ ArrayAdapter
Returns a new instance of ArrayAdapter.
25
26
27
28
|
# File 'lib/charty/vector_adapters/array_adapter.rb', line 25
def initialize(data, index: nil)
@data = check_data(data)
self.index = index || RangeIndex.new(0 ... length)
end
|
Class Method Details
.supported?(data) ⇒ Boolean
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/charty/vector_adapters/array_adapter.rb', line 11
def self.supported?(data)
case data
when Array
case data[0]
when Numeric, String, Time, Date, DateTime, true, false, nil
true
else
false
end
else
false
end
end
|
Instance Method Details
#boolean? ⇒ Boolean
45
46
47
48
49
50
51
52
|
# File 'lib/charty/vector_adapters/array_adapter.rb', line 45
def boolean?
case first_nonnil
when true, false
true
else
false
end
end
|
#categorical? ⇒ Boolean
63
64
65
|
# File 'lib/charty/vector_adapters/array_adapter.rb', line 63
def categorical?
false
end
|
#categories ⇒ Object
67
68
69
|
# File 'lib/charty/vector_adapters/array_adapter.rb', line 67
def categories
nil
end
|
#drop_na ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/charty/vector_adapters/array_adapter.rb', line 81
def drop_na
if numeric?
Charty::Vector.new(data.reject { |x|
case x
when Float
x.nan?
else
x.nil?
end
})
else
Charty::Vector.new(data.compact)
end
end
|
#eq(val) ⇒ Object
96
97
98
99
100
|
# File 'lib/charty/vector_adapters/array_adapter.rb', line 96
def eq(val)
Charty::Vector.new(data.map {|x| x == val },
index: index,
name: name)
end
|
#first_nonnil ⇒ Object
41
42
43
|
# File 'lib/charty/vector_adapters/array_adapter.rb', line 41
def first_nonnil
data.drop_while(&:nil?).first
end
|
#group_by(grouper) ⇒ Object
73
74
75
76
77
78
79
|
# File 'lib/charty/vector_adapters/array_adapter.rb', line 73
def group_by(grouper)
groups = data.each_index.group_by {|i| grouper[i] }
groups.map { |g, vals|
vals.collect! {|i| self[i] }
[g, Charty::Vector.new(vals)]
}.to_h
end
|
#notnull ⇒ Object
102
103
104
105
106
|
# File 'lib/charty/vector_adapters/array_adapter.rb', line 102
def notnull
Charty::Vector.new(data.map {|x| ! missing_value?(x) },
index: index,
name: name)
end
|
#numeric? ⇒ Boolean
54
55
56
57
58
59
60
61
|
# File 'lib/charty/vector_adapters/array_adapter.rb', line 54
def numeric?
case first_nonnil
when Numeric
true
else
false
end
end
|
#where(mask) ⇒ Object
36
37
38
39
|
# File 'lib/charty/vector_adapters/array_adapter.rb', line 36
def where(mask)
masked_data, masked_index = where_in_array(mask)
Charty::Vector.new(masked_data, index: masked_index, name: name)
end
|