Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implements DataFrame#reset_index #473

Merged
merged 4 commits into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/daru/dataframe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,19 @@ def reindex new_index
end
end

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.order = new_vectors
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
Expand Down
4 changes: 4 additions & 0 deletions lib/daru/index/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/daru/index/multi_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def map(&block)
end

attr_reader :labels
attr_reader :name

def levels
@levels.map(&:keys)
Expand Down Expand Up @@ -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
35 changes: 35 additions & 0 deletions spec/dataframe_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
13 changes: 13 additions & 0 deletions spec/index/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions spec/index/multi_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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