Skip to content

Commit 3a0c571

Browse files
committed
Multi Index should return a Daru::Index when a single level is left
1 parent 3ac3d15 commit 3a0c571

File tree

5 files changed

+42
-19
lines changed

5 files changed

+42
-19
lines changed

lib/daru/index/multi_index.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,12 @@ def dup
290290
end
291291

292292
def drop_left_level by=1
293-
MultiIndex.from_arrays to_a.transpose[by..-1]
293+
arrays = to_a.transpose[by..-1]
294+
if arrays.length == 1
295+
Index.new(arrays[0])
296+
else
297+
MultiIndex.from_arrays to_a.transpose[by..-1]
298+
end
294299
end
295300

296301
def | other

spec/category_spec.rb

+2-4
Original file line numberDiff line numberDiff line change
@@ -689,15 +689,13 @@
689689
end
690690

691691
it "returns sub vector when passed first and second layer of tuple" do
692-
mi = Daru::MultiIndex.from_tuples([
693-
[:foo],
694-
[:bar]])
692+
mi = Daru::Index.new([:foo,:bar])
695693
expect(@vector[:c,:two]).to eq(Daru::Vector.new([10,11], index: mi,
696694
name: :sub_sub_vector, type: :category))
697695
end
698696

699697
it "returns sub vector not a single element when passed the partial tuple" do
700-
mi = Daru::MultiIndex.from_tuples([[:foo]])
698+
mi = Daru::Index.new([:foo])
701699
expect(@vector[:d, :one]).to eq(Daru::Vector.new([12], index: mi,
702700
name: :sub_sub_vector, type: :category))
703701
end

spec/dataframe_spec.rb

+9-5
Original file line numberDiff line numberDiff line change
@@ -507,13 +507,20 @@
507507
sub_order = Daru::MultiIndex.from_tuples([
508508
[:one, :bar],
509509
[:two, :baz]
510-
])
510+
])
511511
expect(@df_mi[:a]).to eq(Daru::DataFrame.new([
512512
@vector_arry1,
513513
@vector_arry2
514514
], index: @multi_index, order: sub_order))
515515
end
516516

517+
it "returns DataFrame with simple index when specified first and second layer of MultiIndex" do
518+
sub_order = Daru::Index.new([:baz])
519+
expect(@df_mi[:a][:two]).to eq(Daru::DataFrame.new([
520+
@vector_arry2
521+
], index: @multi_index, order: sub_order))
522+
end
523+
517524
it "returns a Vector if the last level of MultiIndex is tracked" do
518525
expect(@df_mi[:a, :one, :bar]).to eq(
519526
Daru::Vector.new(@vector_arry1, index: @multi_index))
@@ -1525,10 +1532,7 @@
15251532
end
15261533

15271534
it "returns DataFrame when specifying first and second layer of MultiIndex" do
1528-
sub_index = Daru::MultiIndex.from_tuples([
1529-
[:bar],
1530-
[:baz]
1531-
])
1535+
sub_index = Daru::Index.new([:bar,:baz])
15321536
expect(@df_mi.row[:c,:one]).to eq(Daru::DataFrame.new([
15331537
[11,12],
15341538
[1,2],

spec/index/multi_index_spec.rb

+21-3
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,16 @@
453453
])
454454
)
455455
end
456+
457+
it "returns a Daru::Index if a single level remains" do
458+
expect(
459+
Daru::MultiIndex.from_tuples([
460+
[:c,:one,:bar],
461+
[:c,:one,:baz]
462+
]).drop_left_level(2)).to eq(
463+
Daru::Index.new([:bar, :baz])
464+
)
465+
end
456466
end
457467

458468
context 'other forms of tuple list representation' do
@@ -540,12 +550,20 @@
540550
])
541551
end
542552

543-
context "multiple indexes" do
544-
subject { idx.subset :b, :one }
553+
context "first level" do
554+
subject { idx.subset :b }
545555

546556
it { is_expected.to be_a described_class }
557+
its(:size) { is_expected.to eq 4 }
558+
its(:to_a) { is_expected.to eq [[:one, :bar], [:two, :bar], [:two, :baz], [:one, :foo]] }
559+
end
560+
561+
context "first and second level" do
562+
subject { idx.subset :b, :one }
563+
564+
it { is_expected.to be_a Daru::Index }
547565
its(:size) { is_expected.to eq 2 }
548-
its(:to_a) { is_expected.to eq [[:bar], [:foo]] }
566+
its(:to_a) { is_expected.to eq [:bar, :foo] }
549567
end
550568

551569
context "multiple positional indexes" do

spec/vector_spec.rb

+4-6
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,14 @@
239239
dtype: dtype, name: :sub_vector))
240240
end
241241

242-
it "returns sub vector when passed first and second layer of tuple" do
243-
mi = Daru::MultiIndex.from_tuples([
244-
[:foo],
245-
[:bar]])
246-
expect(@vector[:c,:two]).to eq(Daru::Vector.new([10,11], index: mi,
242+
it "returns sub vector with simple index when passed first and second layer of tuple" do
243+
i = Daru::Index.new([:foo, :bar])
244+
expect(@vector[:c,:two]).to eq(Daru::Vector.new([10,11], index: i,
247245
dtype: dtype, name: :sub_sub_vector))
248246
end
249247

250248
it "returns sub vector not a single element when passed the partial tuple" do
251-
mi = Daru::MultiIndex.from_tuples([[:foo]])
249+
mi = Daru::Index.new([:foo])
252250
expect(@vector[:d, :one]).to eq(Daru::Vector.new([12], index: mi,
253251
dtype: dtype, name: :sub_sub_vector))
254252
end

0 commit comments

Comments
 (0)