Skip to content

Commit 0e44192

Browse files
KenilMehtaJulianWgs
authored andcommitted
Optimising Series.nunique for Nan values pandas-dev#40865 (pandas-dev#41236)
1 parent 323fe72 commit 0e44192

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

asv_bench/benchmarks/frame_methods.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,14 @@ def time_frame_nunique(self):
563563
self.df.nunique()
564564

565565

566+
class SeriesNuniqueWithNan:
567+
def setup(self):
568+
self.ser = Series(100000 * (100 * [np.nan] + list(range(100)))).astype(float)
569+
570+
def time_series_nunique_nan(self):
571+
self.ser.nunique()
572+
573+
566574
class Duplicated:
567575
def setup(self):
568576
n = 1 << 20

doc/source/whatsnew/v1.3.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ Performance improvements
665665
- Performance improvement in the conversion of pyarrow boolean array to a pandas nullable boolean array (:issue:`41051`)
666666
- Performance improvement for concatenation of data with type :class:`CategoricalDtype` (:issue:`40193`)
667667
- Performance improvement in :meth:`.GroupBy.cummin` and :meth:`.GroupBy.cummax` with nullable data types (:issue:`37493`)
668-
-
668+
- Performance improvement in :meth:`Series.nunique` with nan values (:issue:`40865`)
669669

670670
.. ---------------------------------------------------------------------------
671671

pandas/core/base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,8 +1041,10 @@ def nunique(self, dropna: bool = True) -> int:
10411041
>>> s.nunique()
10421042
4
10431043
"""
1044-
obj = remove_na_arraylike(self) if dropna else self
1045-
return len(obj.unique())
1044+
uniqs = self.unique()
1045+
if dropna:
1046+
uniqs = remove_na_arraylike(uniqs)
1047+
return len(uniqs)
10461048

10471049
@property
10481050
def is_unique(self) -> bool:

0 commit comments

Comments
 (0)