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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f72d838a7ae6b15ca8d5d4d2942f733ee731815a432dec1b76e51296cd7d5fe7
4
- data.tar.gz: 5653ee135b2fcf671eb28ad8af54cb11e1d8d6187bdfdef284d425b23d0da290
3
+ metadata.gz: c158868ddcdfad0c071d5ad8b583605e86cdd3a005714df2071d0fc122e88278
4
+ data.tar.gz: 56d1feb6d2b95f6a4c897589c56c32e8890fe72c9eb2e72d6b71a91ffc9e95ae
5
5
  SHA512:
6
- metadata.gz: b3780b06dcb392bd866af2424ea0a19ddf263cb125b13092c3d6674ab8d6d1c576bbf6b78e6bfb794cc21aac73ac1f1af4faacb0905196c6cfb5a5190be56455
7
- data.tar.gz: 4e39865f597bf9ecb63270c9f6df963ad4e8bdf6fecda6de0a305fb81b5c81b58787e28267a7e84644194a4bd63442e98fb56f642c03ef327e08b888210637ca
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.each do |vector|
111
- delete_vector(vector) unless yield(@data[@vectors[vector]], vector)
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
@@ -1,3 +1,3 @@
1
1
  module DaruLite
2
- VERSION = '0.3.2'.freeze
2
+ VERSION = '0.3.3'.freeze
3
3
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: daru_lite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Naude-Filonnière