daru_lite 0.3.2 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/daru_lite/data_frame/filterable.rb +15 -2
- data/lib/daru_lite/version.rb +1 -1
- data/spec/data_frame/filterable_example.rb +47 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c158868ddcdfad0c071d5ad8b583605e86cdd3a005714df2071d0fc122e88278
|
|
4
|
+
data.tar.gz: 56d1feb6d2b95f6a4c897589c56c32e8890fe72c9eb2e72d6b71a91ffc9e95ae
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5ac43007818f83995648bdbc65c5debc0d8999c9623271f0d6ca7c70bccc99904b74ec48fd99860a38ad6d02f99493fecdffb6560cbf92fd6a6682eb1190c0a5
|
|
7
|
+
data.tar.gz: 3f980b1dd60846f9aba374663e34fbd0118396079e1366f7e0046c288087adbf0f7f606853dec915dd9b42109f30e2f25033ea277533a711f5be8ab2948e58f3
|
|
@@ -107,9 +107,22 @@ module DaruLite
|
|
|
107
107
|
end
|
|
108
108
|
|
|
109
109
|
def keep_vector_if
|
|
110
|
-
@vectors.
|
|
111
|
-
|
|
110
|
+
vector_names = @vectors.to_a
|
|
111
|
+
kept_positions = @vectors.size.times.select do |position|
|
|
112
|
+
yield(@data[position], vector_names[position])
|
|
112
113
|
end
|
|
114
|
+
kept_names = kept_positions.map { |position| vector_names[position] }
|
|
115
|
+
|
|
116
|
+
@data = kept_positions.map { |position| @data[position] }
|
|
117
|
+
@vectors =
|
|
118
|
+
if @vectors.is_a?(DaruLite::MultiIndex)
|
|
119
|
+
# An empty result can't form a MultiIndex, so it falls back to a plain Index.
|
|
120
|
+
kept_names.any? ? DaruLite::MultiIndex.from_tuples(kept_names) : DaruLite::Index.new(kept_names)
|
|
121
|
+
else
|
|
122
|
+
@vectors.class.new(kept_names)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
self
|
|
113
126
|
end
|
|
114
127
|
|
|
115
128
|
# creates a new vector with the data of a given field which the block returns true
|
data/lib/daru_lite/version.rb
CHANGED
|
@@ -340,6 +340,53 @@ shared_examples_for 'a filterable DataFrame' do
|
|
|
340
340
|
expect(df).to eq(DaruLite::DataFrame.new({a: [1,2,3,4,5]}, order: [:a],
|
|
341
341
|
index: [:one, :two, :three, :four, :five]))
|
|
342
342
|
end
|
|
343
|
+
|
|
344
|
+
context "with duplicate tuples in a MultiIndex" do
|
|
345
|
+
let(:multi_index_df) do
|
|
346
|
+
DaruLite::DataFrame.new(
|
|
347
|
+
[[1, 4], [2, 5], [3, 6]],
|
|
348
|
+
order: DaruLite::MultiIndex.from_tuples([[:count, 'A'], [:count, 'A'], [:pct, 'B']]),
|
|
349
|
+
index: %w[r1 r2]
|
|
350
|
+
)
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
it "keeps the matching vectors without raising on the ambiguous lookup" do
|
|
354
|
+
multi_index_df.keep_vector_if { |_vector, (_metric, label)| label != 'B' }
|
|
355
|
+
|
|
356
|
+
expect(multi_index_df.vectors).to be_a(DaruLite::MultiIndex)
|
|
357
|
+
expect(multi_index_df.vectors.to_a).to eq([[:count, 'A'], [:count, 'A']])
|
|
358
|
+
end
|
|
359
|
+
|
|
360
|
+
it "does not raise when no vector is kept" do
|
|
361
|
+
expect { multi_index_df.keep_vector_if { |_vector, _tuple| false } }.not_to raise_error
|
|
362
|
+
expect(multi_index_df.vectors.to_a).to eq([])
|
|
363
|
+
end
|
|
364
|
+
end
|
|
365
|
+
|
|
366
|
+
context "with duplicate categories in a CategoricalIndex" do
|
|
367
|
+
let(:categorical_df) do
|
|
368
|
+
DaruLite::DataFrame.new(
|
|
369
|
+
[[1, 4], [2, 5], [3, 6]],
|
|
370
|
+
order: DaruLite::CategoricalIndex.new([:a, :a, :b]),
|
|
371
|
+
index: %w[r1 r2]
|
|
372
|
+
)
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
it "keeps the matching vectors and preserves the CategoricalIndex" do
|
|
376
|
+
categorical_df.keep_vector_if { |_vector, category| category == :a }
|
|
377
|
+
|
|
378
|
+
expect(categorical_df.vectors).to be_a(DaruLite::CategoricalIndex)
|
|
379
|
+
expect(categorical_df.vectors.to_a).to eq([:a, :a])
|
|
380
|
+
expect(categorical_df.map_vectors(&:to_a)).to eq([[1, 4], [2, 5]])
|
|
381
|
+
end
|
|
382
|
+
|
|
383
|
+
it "preserves the CategoricalIndex when no vector is kept" do
|
|
384
|
+
categorical_df.keep_vector_if { |_vector, _category| false }
|
|
385
|
+
|
|
386
|
+
expect(categorical_df.vectors).to be_a(DaruLite::CategoricalIndex)
|
|
387
|
+
expect(categorical_df.vectors.to_a).to eq([])
|
|
388
|
+
end
|
|
389
|
+
end
|
|
343
390
|
end
|
|
344
391
|
|
|
345
392
|
describe "#filter_vectors" do
|