-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
MultiIndex Support for DataFrame.pivot #25330
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
Changes from all commits
a92c29c
08650a2
c6dddf2
b3dad89
def0e2d
6215146
565c41c
d795e86
0a6e4be
ad4a761
38d545c
064c085
2e5dd67
caed2cd
406b690
b1db902
2108301
aaa21bb
1d0ce5d
0c25bba
a4e8d38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -368,18 +368,34 @@ def _convert_by(by): | |
@Appender(_shared_docs['pivot'], indents=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. Also think you will need to update the docstring |
||
def pivot(data, index=None, columns=None, values=None): | ||
if values is None: | ||
cols = [columns] if index is None else [index, columns] | ||
# Make acceptable for multiple column indexes. | ||
# cols = [] | ||
if is_list_like(index): | ||
cols = index # cols.extend(index) | ||
elif index is not None: | ||
cols = [index] | ||
else: | ||
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. can you make this an if/elsif/else |
||
cols = [] | ||
cols.append(columns) | ||
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. what happens when columns=None? |
||
|
||
append = index is None | ||
indexed = data.set_index(cols, append=append) | ||
|
||
else: | ||
if index is None: | ||
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. same here |
||
index = data.index | ||
index = MultiIndex.from_arrays([data.index, data[columns]]) | ||
elif is_list_like(index): | ||
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. I think it would help readability if the conditions here were refactored to be in the same sequence as the conditions in the branch above, as IIUC we essentially evaluate the same conditions in both branches |
||
# Iterating through the list of multiple columns of an index. | ||
indexes = [data[column] for column in index] | ||
indexes.append(data[columns]) | ||
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. What does this do? |
||
index = MultiIndex.from_arrays(indexes) | ||
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. names? 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. I don't think so but let me know if I should. |
||
else: | ||
# Build multi-indexes if index is not None and not a list. | ||
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. can you make this an if/elseif/else 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. I am not sure about the last if/else statement. I can pull if/elif/else for the first three but I still need to have another condition for the last two. 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. these are comments are not very useful, you are just repeating the code, can you make more informative |
||
index = data[index] | ||
index = MultiIndex.from_arrays([index, data[columns]]) | ||
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. Can't you construct "the first draft" of |
||
index = MultiIndex.from_arrays([index, data[columns]]) | ||
|
||
if is_list_like(values) and not isinstance(values, tuple): | ||
# Exclude tuple because it is seen as a single column name | ||
# Exclude tuple because it is seen as a single column name. | ||
indexed = data._constructor(data[values].values, index=index, | ||
columns=values) | ||
else: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -301,6 +301,33 @@ def test_pivot_multi_functions(self): | |
expected = concat([means, stds], keys=['mean', 'std'], axis=1) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
@pytest.mark.parametrize('method', [True, False]) | ||
def test_pivot_multiple_columns_as_index(self, method): | ||
# adding the test case for multiple columns as index (#21425) | ||
df = DataFrame({'lev1': [1, 1, 1, 1, 2, 2, 2, 2], | ||
'lev2': [1, 1, 2, 2, 1, 1, 2, 2], | ||
'lev3': [1, 2, 1, 2, 1, 2, 1, 2], | ||
'values': [0, 1, 2, 3, 4, 5, 6, 7]}) | ||
data = [[0, 1], [2, 3], [4, 5], [6, 7]] | ||
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. can you do the test caess 1 after the other and use the standard result= |
||
exp_index = pd.MultiIndex.from_product([[1, 2], [1, 2]], | ||
names=['lev1', 'lev2']) | ||
if method: | ||
result = df.pivot(index=['lev1', 'lev2'], | ||
columns='lev3', | ||
values='values') | ||
exp_columns = Index([1, 2], name='lev3') | ||
|
||
else: | ||
result = df.pivot(index=['lev1', 'lev2'], | ||
columns='lev3') | ||
exp_columns = MultiIndex(levels=[['values'], [1, 2]], | ||
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. Can you use one of the more idiomatic |
||
codes=[[0, 0], [0, 1]], | ||
names=[None, 'lev3']) | ||
thoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
expected = DataFrame(data=data, index=exp_index, | ||
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. is there a test for columns=None and list-like index? |
||
columns=exp_columns) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@pytest.mark.parametrize('method', [True, False]) | ||
def test_pivot_index_with_nan(self, method): | ||
# GH 3588 | ||
|
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.
Add a versionadded tag here.