From 51b1324c5d6b2d852b5b7cd2c157583df3d3fb9d Mon Sep 17 00:00:00 2001 From: Yuki INOUE Date: Mon, 3 Dec 2018 16:54:17 +0900 Subject: [PATCH 1/4] Index#to_df, DataFrame#reset_index implemented --- lib/daru/dataframe.rb | 11 +++++++++++ lib/daru/index/index.rb | 4 ++++ lib/daru/index/multi_index.rb | 5 +++++ 3 files changed, 20 insertions(+) diff --git a/lib/daru/dataframe.rb b/lib/daru/dataframe.rb index be3880637..cc1c86097 100644 --- a/lib/daru/dataframe.rb +++ b/lib/daru/dataframe.rb @@ -1572,6 +1572,17 @@ def reindex new_index end end + def reset_index + index_df = index.to_df + names = index.name + names = [names] unless names.instance_of?(Array) + names.each do |name| + self[name] = index_df[name] + end + self.index = index_df.index + self + end + # Reassign index with a new index of type Daru::Index or any of its subclasses. # # @param [Daru::Index] idx New index object on which the rows of the dataframe diff --git a/lib/daru/index/index.rb b/lib/daru/index/index.rb index c256caa00..9a887c949 100644 --- a/lib/daru/index/index.rb +++ b/lib/daru/index/index.rb @@ -294,6 +294,10 @@ def sort opts={} self.class.new(new_index) end + def to_df + Daru::DataFrame.new(name => to_a) + end + private def guess_index index diff --git a/lib/daru/index/multi_index.rb b/lib/daru/index/multi_index.rb index 05428b737..b4dd8d20c 100644 --- a/lib/daru/index/multi_index.rb +++ b/lib/daru/index/multi_index.rb @@ -9,6 +9,7 @@ def map(&block) end attr_reader :labels + attr_reader :name def levels @levels.map(&:keys) @@ -365,5 +366,9 @@ def sparse_tuples [nil] * (cur.size - left.size) + left.map(&:first) } end + + def to_df + Daru::DataFrame.new(@name.zip(to_a.transpose).to_h) + end end end From 4e457cfc312be32cd4978478d06f5de6c5057960 Mon Sep 17 00:00:00 2001 From: Yuki INOUE Date: Mon, 3 Dec 2018 17:01:13 +0900 Subject: [PATCH 2/4] Index#to_df rspec --- spec/index/index_spec.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/spec/index/index_spec.rb b/spec/index/index_spec.rb index a54ca560c..858406733 100644 --- a/spec/index/index_spec.rb +++ b/spec/index/index_spec.rb @@ -388,4 +388,17 @@ end end + + context '#to_df' do + let(:idx) do + Daru::Index.new(['speaker', 'mic', 'guitar', 'amp'], + name: 'instruments') + end + subject { idx.to_df } + + it { is_expected.to eq Daru::DataFrame.new( + 'instruments' => ['speaker', 'mic', 'guitar', 'amp'] + ) + } + end end From 31dc9eb2026bdaf36fcc0ece711001048017abcd Mon Sep 17 00:00:00 2001 From: Yuki INOUE Date: Mon, 3 Dec 2018 17:10:44 +0900 Subject: [PATCH 3/4] MultiIndex#to_df spec --- spec/index/multi_index_spec.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/spec/index/multi_index_spec.rb b/spec/index/multi_index_spec.rb index 704869265..6d371da2f 100644 --- a/spec/index/multi_index_spec.rb +++ b/spec/index/multi_index_spec.rb @@ -659,4 +659,22 @@ it { expect(idx.valid? :a, :three).to eq false } end end + + context '#to_df' do + let(:idx) do + described_class.from_tuples([ + %w[a one bar], + %w[a two bar], + %w[b two baz], + %w[b one foo] + ]).tap { |idx| idx.name = %w[col1 col2 col3] } + end + + subject { idx.to_df } + it { is_expected.to eq Daru::DataFrame.new( + 'col1' => %w[a a b b], + 'col2' => %w[one two two one], + 'col3' => %w[bar bar baz foo] + )} + end end From b0668b5050e6bf013a93f9b1daf1dcacdc46ec57 Mon Sep 17 00:00:00 2001 From: Yuki INOUE Date: Mon, 3 Dec 2018 17:45:47 +0900 Subject: [PATCH 4/4] implement reset_index --- lib/daru/dataframe.rb | 4 +++- spec/dataframe_spec.rb | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/daru/dataframe.rb b/lib/daru/dataframe.rb index cc1c86097..63ff045e3 100644 --- a/lib/daru/dataframe.rb +++ b/lib/daru/dataframe.rb @@ -1576,10 +1576,12 @@ def reset_index index_df = index.to_df names = index.name names = [names] unless names.instance_of?(Array) + new_vectors = names + vectors.to_a + self.index = index_df.index names.each do |name| self[name] = index_df[name] end - self.index = index_df.index + self.order = new_vectors self end diff --git a/spec/dataframe_spec.rb b/spec/dataframe_spec.rb index 91afab2d0..8b3931b8a 100644 --- a/spec/dataframe_spec.rb +++ b/spec/dataframe_spec.rb @@ -3673,6 +3673,41 @@ def create_test(*args, &_proc) end end + context '#reset_index' do + context 'when Index' do + subject do + Daru::DataFrame.new( + {'vals' => [1,2,3,4,5]}, + index: Daru::Index.new(%w[a b c d e], name: 'indices') + ).reset_index + end + + it { is_expected.to eq Daru::DataFrame.new( + 'indices' => %w[a b c d e], + 'vals' => [1,2,3,4,5] + )} + end + + context 'when MultiIndex' do + subject do + mi = Daru::MultiIndex.from_tuples([ + [0, 'a'], [0, 'b'], [1, 'a'], [1, 'b'] + ]) + mi.name = %w[nums alphas] + Daru::DataFrame.new( + {'vals' => [1,2,3,4]}, + index: mi + ).reset_index + end + + it { is_expected.to eq Daru::DataFrame.new( + 'nums' => [0,0,1,1], + 'alphas' => %w[a b a b], + 'vals' => [1,2,3,4] + )} + end + end + context "#set_index" do before(:each) do @df = Daru::DataFrame.new({