-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Adding Multiindex support to dataframe pivot function(Fixes #21425) #21467
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 7 commits
79dacb2
f95f854
100a4bc
ecc19d2
7f6c09c
34fb002
787b289
358e0ea
811ab86
7b6b5a4
3bcfc16
0234b38
a8f80ed
7a050d6
beb768b
faab956
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 |
---|---|---|
|
@@ -392,12 +392,21 @@ def pivot(self, index=None, columns=None, values=None): | |
cols = [columns] if index is None else [index, columns] | ||
append = index is None | ||
indexed = self.set_index(cols, append=append) | ||
|
||
else: | ||
if index is None: | ||
index = self.index | ||
index = MultiIndex.from_arrays([index, self[columns]]) | ||
# added this case to handle multi-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. can you make a comment that reflects what is here (not added) 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. Done |
||
elif isinstance(index, 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. use 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. Sure. I will do it. |
||
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. can you make this a list-comprehension 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. sure! |
||
for i in index: | ||
indexes.append(self[i]) | ||
indexes.append(self[columns]) | ||
index = MultiIndex.from_arrays(indexes) | ||
else: | ||
index = self[index] | ||
index = MultiIndex.from_arrays([index, self[columns]]) | ||
index = MultiIndex.from_arrays([index, self[columns]]) | ||
|
||
if is_list_like(values) and not isinstance(values, tuple): | ||
# Exclude tuple because it is seen as a single column name | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -283,6 +283,23 @@ def test_pivot_multi_functions(self): | |
expected = concat([means, stds], keys=['mean', 'std'], axis=1) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_pivot_multiple_columns_as_index(self): | ||
# 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]}) | ||
result = df.pivot(index=['lev1', 'lev2'], | ||
columns='lev3', | ||
values='values') | ||
exp_index = pd.MultiIndex.from_product([[1, 2], [1, 2]], | ||
names=['lev1', 'lev2']) | ||
exp_columns = Index([1, 2], name='lev3') | ||
expected = DataFrame([[0, 1], [2, 3], [4, 5], [6, 7]], | ||
exp_index, | ||
exp_columns) | ||
tm.assert_frame_equal(result, expected) | ||
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 add an example where the |
||
|
||
def test_pivot_index_with_nan(self): | ||
# GH 3588 | ||
nan = np.nan | ||
|
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.
'columns as an index'
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.
Sure. I will make those changes.
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.
corrected