-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: allow index of col names in set_index GH10797 #11944
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
Closed
StephenKappel
wants to merge
2
commits into
pandas-dev:master
from
StephenKappel:10797-col-name-index
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2583,6 +2583,42 @@ def test_set_index_empty_column(self): | |
result = df.set_index(['a', 'x']) | ||
repr(result) | ||
|
||
def test_set_index_with_index(self): | ||
# GH10797: It should be possible to use a slice of the column index as | ||
# the `keys` parameter in set_index(). | ||
|
||
# Test that setting the first two columns as the index can be done | ||
# either with a list of column labels or a slice of the column index. | ||
df = DataFrame({'col1': [1, 2, 3, 4, 5, 6], | ||
'col2': ['a', 'b', 'c', 'a', 'b', 'c'], | ||
'col3': [0.0, 0.0, 1.0, 1.0, 2.0, 2.0]}) | ||
expected_index = MultiIndex(levels=[['a', 'b', 'c'], [0.0, 1.0, 2.0]], | ||
labels=[[0, 1, 2, 0, 1, 2], | ||
[0, 0, 1, 1, 2, 2]], | ||
names=['col2', 'col3']) | ||
expected_df = DataFrame(data={'col1': [1, 2, 3, 4, 5, 6]}, | ||
index=expected_index) | ||
list_df = df.set_index(['col2', 'col3']) | ||
assert_frame_equal(expected_df, list_df) | ||
index_df = df.set_index(df.columns[1:]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so add some tests here where this could fail. |
||
assert_frame_equal(expected_df, index_df) | ||
|
||
# Test that passing the entire index results in an empty dataframe (i.e. | ||
# all columns become part of the index). | ||
empty_df = df.set_index(df.columns) | ||
assert_equal(len(empty_df.columns), 0) | ||
assert_equal(empty_df.index.nlevels, 3) | ||
|
||
# Test that an index that is created independently of the column index | ||
# is used as a new index - not as a set of column labels. | ||
new_index = Index(data=['col1', 'col1', 'col2', 'col2', 'col3', 'col3']) | ||
expected_df2 = DataFrame({'col1': [1, 2, 3, 4, 5, 6], | ||
'col2': ['a', 'b', 'c', 'a', 'b', 'c'], | ||
'col3': [0.0, 0.0, 1.0, 1.0, 2.0, 2.0]}, | ||
index=new_index) | ||
col_name_index_df = df.set_index(new_index) | ||
assert_frame_equal(expected_df2, col_name_index_df) | ||
|
||
def test_set_columns(self): | ||
cols = Index(np.arange(len(self.mixed_frame.columns))) | ||
self.mixed_frame.columns = cols | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not a reliable approach, nor should you be using the private
.base
.I am not convinced we can do this in a reliable way.