Class: Charty::VectorAdapters::NumpyAdapter
Instance Attribute Summary collapse
Attributes included from IndexSupport
#index
Attributes included from NameSupport
#name
Class Method Summary
collapse
Instance Method Summary
collapse
#[], #[]=
Methods inherited from BaseAdapter
#==, adapter_name, #inverse_log_scale, #log_scale, #values_at, #where_in_array
Constructor Details
Returns a new instance of NumpyAdapter.
16
17
18
19
|
# 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
|
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
21
22
23
|
# File 'lib/charty/vector_adapters/numpy_adapter.rb', line 21
def data
@data
end
|
Class Method Details
.supported?(data) ⇒ Boolean
6
7
8
9
10
11
12
13
14
|
# 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
|
Instance Method Details
#boolean? ⇒ Boolean
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# 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
else
return false
end
i += 1
end
true
else
false
end
end
|
#categorical? ⇒ Boolean
95
96
97
|
# File 'lib/charty/vector_adapters/numpy_adapter.rb', line 95
def categorical?
false
end
|
#categories ⇒ Object
99
100
101
|
# File 'lib/charty/vector_adapters/numpy_adapter.rb', line 99
def categories
nil
end
|
#compare_data_equality(other) ⇒ Object
25
26
27
28
29
30
31
32
33
34
|
# 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 ⇒ Object
129
130
131
132
133
134
135
136
|
# 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 ⇒ Object
54
55
56
57
58
59
60
61
62
|
# 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? ⇒ Boolean
64
65
66
|
# File 'lib/charty/vector_adapters/numpy_adapter.rb', line 64
def empty?
data.size == 0
end
|
#eq(val) ⇒ Object
138
139
140
141
142
|
# 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) ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# 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) }
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 ⇒ Object
159
160
161
|
# File 'lib/charty/vector_adapters/numpy_adapter.rb', line 159
def mean
Numpy.mean(data)
end
|
#notnull ⇒ Object
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
# 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? ⇒ Boolean
90
91
92
93
|
# File 'lib/charty/vector_adapters/numpy_adapter.rb', line 90
def numeric?
PyCall.builtins.issubclass(data.dtype.type, PyCall.tuple([Numpy.number, Numpy.bool_]))
end
|
#percentile(q) ⇒ Object
167
168
169
|
# File 'lib/charty/vector_adapters/numpy_adapter.rb', line 167
def percentile(q)
Numpy.nanpercentile(data, q)
end
|
#stdev(population: false) ⇒ Object
163
164
165
|
# File 'lib/charty/vector_adapters/numpy_adapter.rb', line 163
def stdev(population: false)
Numpy.std(data, ddof: population ? 0 : 1)
end
|
#unique_values ⇒ Object
103
104
105
|
# File 'lib/charty/vector_adapters/numpy_adapter.rb', line 103
def unique_values
Numpy.unique(data).to_a
end
|
#where(mask) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# 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
|