-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Fix Bug with NA value in Grouping for Groupby.nth #26152
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 10 commits
e62a14f
e113b55
823225b
068936f
8716764
3000d2b
84ddd6d
84d343a
6f40fbe
e2d006d
69014a7
0b7eb6c
48d90ee
7eea5e3
4dec450
ce0abcd
c2a0b8e
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 |
---|---|---|
|
@@ -12,7 +12,7 @@ class providing the base-class of operations. | |
import datetime | ||
from functools import partial, wraps | ||
import types | ||
from typing import FrozenSet, Optional, Tuple, Type | ||
from typing import cast, FrozenSet, List, Optional, Tuple, Type, Union | ||
import warnings | ||
|
||
import numpy as np | ||
|
@@ -1546,15 +1546,16 @@ def backfill(self, limit=None): | |
|
||
@Substitution(name='groupby') | ||
@Substitution(see_also=_common_see_also) | ||
def nth(self, n, dropna=None): | ||
def nth(self, | ||
n: Union[int, List[int]], | ||
dropna: Optional[str] = None) -> DataFrame: | ||
""" | ||
Take the nth row from each group if n is an int, or a subset of rows | ||
if n is a list of ints. | ||
|
||
If dropna, will take the nth non-null row, dropna is either | ||
Truthy (if a Series) or 'all', 'any' (if a DataFrame); | ||
this is equivalent to calling dropna(how=dropna) before the | ||
groupby. | ||
'all' or 'any'; this is equivalent to calling dropna(how=dropna) | ||
before the groupby. | ||
|
||
Parameters | ||
---------- | ||
|
@@ -1636,11 +1637,13 @@ def nth(self, n, dropna=None): | |
-nth_values) | ||
mask = mask_left | mask_right | ||
|
||
ids, _, _ = self.grouper.group_info | ||
mask = mask & (ids != -1) # Drop NA values in grouping | ||
|
||
out = self._selected_obj[mask] | ||
if not self.as_index: | ||
return out | ||
|
||
ids, _, _ = self.grouper.group_info | ||
out.index = self.grouper.result_index[ids[mask]] | ||
|
||
return out.sort_index() if self.sort else out | ||
|
@@ -1665,6 +1668,7 @@ def nth(self, n, dropna=None): | |
|
||
# old behaviour, but with all and any support for DataFrames. | ||
# modified in GH 7559 to have better perf | ||
n = cast(int, n) | ||
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 would really really avoid the need to do this, you can instead assign to a new variable that is the correct type 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. No problem. So in your mind would cast only be suitable for expressions without assignment or should we avoid altogether? 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 would really avoid having to cast at all; this is just plain confusing as its not 'code' but for annotation purposes. 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've refactored the branches to appease the type checker without cast but also arguably make the code more clear. Makes the diff a little larger than originally but I think for the better |
||
max_len = n if n >= 0 else - 1 - n | ||
dropped = self.obj.dropna(how=dropna, axis=self.axis) | ||
|
||
|
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 you put the comment on the prior line