Skip to content

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

Merged
merged 11 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ Other
- Bug in :meth:`Index.union` behaving differently depending on whether operand is a :class:`Index` or other list-like (:issue:`36384`)
- Passing an array with 2 or more dimensions to the :class:`Series` constructor now raises the more specific ``ValueError``, from a bare ``Exception`` previously (:issue:`35744`)
- Bug in ``accessor.DirNamesMixin``, where ``dir(obj)`` wouldn't show attributes defined on the instance (:issue:`37173`).
- Bug in :meth:`Series.nunique` with ``dropna=True`` was returning incorrect results when both ``NA`` and ``None`` missing values were present (:issue:`37566`)

.. ---------------------------------------------------------------------------

Expand Down
9 changes: 3 additions & 6 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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:
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/base/test_unique.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
assert res == 1 if dropna else 5