diff --git a/lib/daru/index/multi_index.rb b/lib/daru/index/multi_index.rb index a37462d16..9d4cb767a 100644 --- a/lib/daru/index/multi_index.rb +++ b/lib/daru/index/multi_index.rb @@ -290,7 +290,12 @@ def dup end def drop_left_level by=1 - MultiIndex.from_arrays to_a.transpose[by..-1] + arrays = to_a.transpose[by..-1] + if arrays.length == 1 + Index.new(arrays[0]) + else + MultiIndex.from_arrays to_a.transpose[by..-1] + end end def | other diff --git a/spec/category_spec.rb b/spec/category_spec.rb index ad8753057..05a75d955 100644 --- a/spec/category_spec.rb +++ b/spec/category_spec.rb @@ -689,15 +689,13 @@ end it "returns sub vector when passed first and second layer of tuple" do - mi = Daru::MultiIndex.from_tuples([ - [:foo], - [:bar]]) + mi = Daru::Index.new([:foo,:bar]) expect(@vector[:c,:two]).to eq(Daru::Vector.new([10,11], index: mi, name: :sub_sub_vector, type: :category)) end it "returns sub vector not a single element when passed the partial tuple" do - mi = Daru::MultiIndex.from_tuples([[:foo]]) + mi = Daru::Index.new([:foo]) expect(@vector[:d, :one]).to eq(Daru::Vector.new([12], index: mi, name: :sub_sub_vector, type: :category)) end diff --git a/spec/dataframe_spec.rb b/spec/dataframe_spec.rb index 14384a809..66845a2c4 100644 --- a/spec/dataframe_spec.rb +++ b/spec/dataframe_spec.rb @@ -507,13 +507,20 @@ sub_order = Daru::MultiIndex.from_tuples([ [:one, :bar], [:two, :baz] - ]) + ]) expect(@df_mi[:a]).to eq(Daru::DataFrame.new([ @vector_arry1, @vector_arry2 ], index: @multi_index, order: sub_order)) end + it "returns DataFrame with simple index when specified first and second layer of MultiIndex" do + sub_order = Daru::Index.new([:baz]) + expect(@df_mi[:a][:two]).to eq(Daru::DataFrame.new([ + @vector_arry2 + ], index: @multi_index, order: sub_order)) + end + it "returns a Vector if the last level of MultiIndex is tracked" do expect(@df_mi[:a, :one, :bar]).to eq( Daru::Vector.new(@vector_arry1, index: @multi_index)) @@ -1525,10 +1532,7 @@ end it "returns DataFrame when specifying first and second layer of MultiIndex" do - sub_index = Daru::MultiIndex.from_tuples([ - [:bar], - [:baz] - ]) + sub_index = Daru::Index.new([:bar,:baz]) expect(@df_mi.row[:c,:one]).to eq(Daru::DataFrame.new([ [11,12], [1,2], diff --git a/spec/index/multi_index_spec.rb b/spec/index/multi_index_spec.rb index 6d371da2f..ec2e55442 100644 --- a/spec/index/multi_index_spec.rb +++ b/spec/index/multi_index_spec.rb @@ -453,6 +453,16 @@ ]) ) end + + it "returns a Daru::Index if a single level remains" do + expect( + Daru::MultiIndex.from_tuples([ + [:c,:one,:bar], + [:c,:one,:baz] + ]).drop_left_level(2)).to eq( + Daru::Index.new([:bar, :baz]) + ) + end end context 'other forms of tuple list representation' do @@ -540,12 +550,20 @@ ]) end - context "multiple indexes" do - subject { idx.subset :b, :one } + context "first level" do + subject { idx.subset :b } it { is_expected.to be_a described_class } + its(:size) { is_expected.to eq 4 } + its(:to_a) { is_expected.to eq [[:one, :bar], [:two, :bar], [:two, :baz], [:one, :foo]] } + end + + context "first and second level" do + subject { idx.subset :b, :one } + + it { is_expected.to be_a Daru::Index } its(:size) { is_expected.to eq 2 } - its(:to_a) { is_expected.to eq [[:bar], [:foo]] } + its(:to_a) { is_expected.to eq [:bar, :foo] } end context "multiple positional indexes" do diff --git a/spec/vector_spec.rb b/spec/vector_spec.rb index 536094328..db38697b2 100644 --- a/spec/vector_spec.rb +++ b/spec/vector_spec.rb @@ -239,16 +239,14 @@ dtype: dtype, name: :sub_vector)) end - it "returns sub vector when passed first and second layer of tuple" do - mi = Daru::MultiIndex.from_tuples([ - [:foo], - [:bar]]) - expect(@vector[:c,:two]).to eq(Daru::Vector.new([10,11], index: mi, + it "returns sub vector with simple index when passed first and second layer of tuple" do + i = Daru::Index.new([:foo, :bar]) + expect(@vector[:c,:two]).to eq(Daru::Vector.new([10,11], index: i, dtype: dtype, name: :sub_sub_vector)) end it "returns sub vector not a single element when passed the partial tuple" do - mi = Daru::MultiIndex.from_tuples([[:foo]]) + mi = Daru::Index.new([:foo]) expect(@vector[:d, :one]).to eq(Daru::Vector.new([12], index: mi, dtype: dtype, name: :sub_sub_vector)) end