Skip to content

Commit f58048d

Browse files
author
Kenil Mehta
committed
PERF: Optimising Series.nunique() for NaN values pandas-dev#40865
1 parent 59b2db1 commit f58048d

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

pandas/core/base.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,8 +1040,11 @@ def nunique(self, dropna: bool = True) -> int:
10401040
>>> s.nunique()
10411041
4
10421042
"""
1043-
obj = remove_na_arraylike(self) if dropna else self
1044-
return len(obj.unique())
1043+
uniqs = self.unique()
1044+
if dropna:
1045+
return (~np.isnan(uniqs)).sum()
1046+
else:
1047+
return len(uniqs)
10451048

10461049
@property
10471050
def is_unique(self) -> bool:

0 commit comments

Comments
 (0)