-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: nunique not ignoring both None and np.nan #37607
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 9 commits
4f5f7eb
25cd909
7f91c90
8c9e2d5
1de2b48
89dc4d7
30e0f10
aff0b01
52d47e3
aaf14e2
d26e523
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 |
---|---|---|
|
@@ -33,7 +33,7 @@ | |
is_scalar, | ||
) | ||
from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries | ||
from pandas.core.dtypes.missing import isna | ||
from pandas.core.dtypes.missing import isna, remove_na_arraylike | ||
|
||
from pandas.core import algorithms | ||
from pandas.core.accessor import DirNamesMixin | ||
|
@@ -1032,11 +1032,8 @@ def nunique(self, dropna: bool = True) -> int: | |
>>> s.nunique() | ||
4 | ||
""" | ||
uniqs = self.unique() | ||
n = len(uniqs) | ||
if dropna and isna(uniqs).any(): | ||
n -= 1 | ||
return n | ||
obj = remove_na_arraylike(self) if dropna else self | ||
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. Late comment, but would it be more performant if we removed the NAs after calculating the uniques? (assume the uniques is typically a much smaller array, and not this does an extra scan and creates a copy of the full array) |
||
return len(obj.unique()) | ||
|
||
@property | ||
def is_unique(self) -> bool: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,3 +121,11 @@ def test_unique_bad_unicode(idx_or_series_w_bad_unicode): | |
else: | ||
expected = np.array(["\ud83d"], dtype=object) | ||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize("dropna", [True, False]) | ||
def test_nunique_dropna(dropna): | ||
# GH37566 | ||
s = pd.Series(["yes", "yes", pd.NA, np.nan, None, pd.NaT]) | ||
res = s.nunique(dropna=True) | ||
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. just do 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.
|
||
assert res == 1 if dropna else 5 |
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.
Could you delete spaces around
=
?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.
done