-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Fix groupby nth with axis=1 #43926
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
Merged
jreback
merged 16 commits into
pandas-dev:master
from
johnzangwill:fix_groupby_nth_column_axis
Oct 13, 2021
Merged
Changes from 4 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
4609d11
Fix nth with axis=1 and add minimal test
johnzangwill acde4ab
Add the PR number to the test
johnzangwill 6b45d2f
Fix axis=1 nth with dropna
johnzangwill 87ec227
pep8 line length
johnzangwill 888b079
factor out _mask_selected_obj
johnzangwill 5d9d26a
Resolve output types
johnzangwill 2c28076
Merge branch 'pandas-dev:master' into fix_groupby_nth_column_axis
johnzangwill 41bc34d
Add to whatsnew
johnzangwill 7e108de
Merge branch 'fix_groupby_nth_column_axis' of https://github.com/john…
johnzangwill 6e60af8
Changed whatsnew
johnzangwill 562087b
Merge branch 'pandas-dev:master' into fix_groupby_nth_column_axis
johnzangwill bf2f342
Merge branch 'pandas-dev:master' into fix_groupby_nth_column_axis
johnzangwill a1850a7
Merge branch 'pandas-dev:master' into fix_groupby_nth_column_axis
johnzangwill eb5b64c
Merge branch 'pandas-dev:master' into fix_groupby_nth_column_axis
johnzangwill aeef7d9
Merge branch 'master' into fix_groupby_nth_column_axis
johnzangwill f984a9c
Add docstring. Add GH number to whatsnew.
johnzangwill 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 |
---|---|---|
|
@@ -2545,18 +2545,25 @@ def nth( | |
# Drop NA values in grouping | ||
mask = mask & (ids != -1) | ||
|
||
out = self._selected_obj[mask] | ||
if self.axis == 0: | ||
out = self._selected_obj[mask] | ||
else: | ||
out = self._selected_obj.iloc[:, mask] | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if not self.as_index: | ||
return out | ||
|
||
result_index = self.grouper.result_index | ||
out.index = result_index[ids[mask]] | ||
if self.axis == 0: | ||
out.index = result_index[ids[mask]] | ||
if not self.observed and isinstance(result_index, CategoricalIndex): | ||
out = out.reindex(result_index) | ||
|
||
if not self.observed and isinstance(result_index, CategoricalIndex): | ||
out = out.reindex(result_index) | ||
out = self._reindex_output(out) | ||
else: | ||
out.columns = result_index[ids[mask]] | ||
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 we re-use any of the _wrap_foo_result methods? |
||
|
||
out = self._reindex_output(out) | ||
return out.sort_index() if self.sort else out | ||
return out.sort_index(axis=self.axis) if self.sort else out | ||
|
||
# dropna is truthy | ||
if isinstance(n, valid_containers): | ||
|
@@ -2599,7 +2606,9 @@ def nth( | |
mutated=self.mutated, | ||
) | ||
|
||
grb = dropped.groupby(grouper, as_index=self.as_index, sort=self.sort) | ||
grb = dropped.groupby( | ||
grouper, as_index=self.as_index, sort=self.sort, axis=self.axis | ||
) | ||
sizes, result = grb.size(), grb.nth(n) | ||
mask = (sizes < max_len)._values | ||
|
||
|
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.
actually let's make a method
as i think we use this in several places.
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.
@jreback, I had already factored these out in my other branch but I have done it here too, and made them both
_mask_selected_obj(mask)