-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from all commits
4609d11
acde4ab
6b45d2f
87ec227
888b079
5d9d26a
2c28076
41bc34d
7e108de
6e60af8
562087b
bf2f342
a1850a7
eb5b64c
aeef7d9
f984a9c
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 |
---|---|---|
|
@@ -2446,7 +2446,7 @@ def backfill(self, limit=None): | |
@Substitution(see_also=_common_see_also) | ||
def nth( | ||
self, n: int | list[int], dropna: Literal["any", "all", None] = None | ||
) -> DataFrame: | ||
) -> NDFrameT: | ||
""" | ||
Take the nth row from each group if n is an int, or a subset of rows | ||
if n is a list of ints. | ||
|
@@ -2545,18 +2545,22 @@ def nth( | |
# Drop NA values in grouping | ||
mask = mask & (ids != -1) | ||
|
||
out = self._selected_obj[mask] | ||
out = self._mask_selected_obj(mask) | ||
|
||
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]] | ||
|
||
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 +2603,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 | ||
|
||
|
@@ -3317,10 +3323,7 @@ def head(self, n=5): | |
""" | ||
self._reset_group_selection() | ||
mask = self._cumcount_array() < n | ||
if self.axis == 0: | ||
return self._selected_obj[mask] | ||
else: | ||
return self._selected_obj.iloc[:, mask] | ||
return self._mask_selected_obj(mask) | ||
|
||
@final | ||
@Substitution(name="groupby") | ||
|
@@ -3355,6 +3358,23 @@ def tail(self, n=5): | |
""" | ||
self._reset_group_selection() | ||
mask = self._cumcount_array(ascending=False) < n | ||
return self._mask_selected_obj(mask) | ||
|
||
@final | ||
def _mask_selected_obj(self, mask: np.ndarray) -> NDFrameT: | ||
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.
|
||
""" | ||
Return _selected_obj with mask applied to the correct axis. | ||
|
||
Parameters | ||
---------- | ||
mask : np.ndarray | ||
Boolean mask to apply. | ||
|
||
Returns | ||
------- | ||
Series or DataFrame | ||
Filtered _selected_obj. | ||
""" | ||
if self.axis == 0: | ||
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 doc-string here 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 |
||
return self._selected_obj[mask] | ||
else: | ||
|
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.
can we re-use any of the _wrap_foo_result methods?