-
-
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
Closed
+44
−2
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
79dacb2
Adding Multiindex support to dataframe pivot function(Fixes #21425)
NikhilKumarM f95f854
added whatsnew entry in v0.23.2.txt file
NikhilKumarM 100a4bc
Added test case for testing dataframe pivot function with multile col…
NikhilKumarM ecc19d2
Made changes as per PEP8 requirements
NikhilKumarM 7f6c09c
changed the type of exp_columns to Index
NikhilKumarM 34fb002
Moved whatsnew entry from v0.23.2.txt to v0.24.0.txt
NikhilKumarM 787b289
Removed the comments that are not required
NikhilKumarM 358e0ea
corrected whatsnew entry
NikhilKumarM 811ab86
Changed indexes to list comprehension
NikhilKumarM 7b6b5a4
used is_list_like to check if input is list
NikhilKumarM 3bcfc16
resolving merge conflict in whatsnew
NikhilKumarM 0234b38
trying to solve merge conflict
NikhilKumarM a8f80ed
Merge branch 'master' into master
NikhilKumarM 7a050d6
Handling the case when there is no values argument in pivot func
NikhilKumarM beb768b
added one more test case for pivot function with multi column index
NikhilKumarM faab956
added whitespace to remove linting error
NikhilKumarM 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -395,15 +395,29 @@ def pivot(self, index=None, columns=None, values=None): | |
See DataFrame.pivot | ||
""" | ||
if values is None: | ||
cols = [columns] if index is None else [index, columns] | ||
if index is None: | ||
cols = [columns] | ||
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 add a comment explaining what is going on here, ideally we would have a comment for each branch of the if/else |
||
if is_list_like(index): | ||
cols = [column for column in index] | ||
else: | ||
cols = [index] | ||
cols.append(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]]) | ||
elif is_list_like(index): | ||
# Iterating through the list of multiple columns of an index | ||
indexes = [self[column] for column in index] | ||
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 | ||
|
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
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.
do we have tests for each one of these paths here? e.g. branching a lot more now
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.
Yes, this is an enhancement. I have added the test cases and need to update the docs. Thanks for your time.