Skip to content

PERF: use unique and isnull in nunique instead of value_counts. #9134

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 1 commit into from
Dec 23, 2014
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
5 changes: 5 additions & 0 deletions doc/source/whatsnew/v0.16.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ Performance

.. _whatsnew_0160.performance:


- Fixed a severe performance regression for ``.loc`` indexing with an array or list (:issue:9126:).

- Improved the speed of `nunique` by calling `unique` instead of `value_counts` (:issue:`9129`, :issue:`7771`)


Bug Fixes
~~~~~~~~~

Expand Down Expand Up @@ -114,3 +118,4 @@ Bug Fixes

- DataFrame now properly supports simultaneous ``copy`` and ``dtype`` arguments in constructor (:issue:`9099`)
- Bug in read_csv when using skiprows on a file with CR line endings with the c engine. (:issue:`9079`)
- isnull now detects NaT in PeriodIndex (:issue:`9129`)
7 changes: 6 additions & 1 deletion pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,12 @@ def nunique(self, dropna=True):
-------
nunique : int
"""
return len(self.value_counts(dropna=dropna))
uniqs = self.unique()
n = len(uniqs)
if dropna and com.isnull(uniqs).any():
n -= 1
return n


def factorize(self, sort=False, na_sentinel=-1):
"""
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def _isnull_ndarraylike(obj):
vec = lib.isnullobj(values.ravel())
result[...] = vec.reshape(shape)

elif dtype in _DATELIKE_DTYPES:
elif is_datetimelike(obj):
# this is the NaT pattern
result = values.view('i8') == tslib.iNaT
else:
Expand Down Expand Up @@ -2366,6 +2366,9 @@ def is_datetime_arraylike(arr):
return arr.dtype == object and lib.infer_dtype(arr) == 'datetime'
return getattr(arr, 'inferred_type', None) == 'datetime'

def is_datetimelike(arr):
return arr.dtype in _DATELIKE_DTYPES or isinstance(arr, ABCPeriodIndex)

def _coerce_to_dtype(dtype):
""" coerce a string / np.dtype to a dtype """
if is_categorical_dtype(dtype):
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ def test_isnull_datetime():
assert(mask[0])
assert(not mask[1:].any())

# GH 9129
pidx = idx.to_period(freq='M')
mask = isnull(pidx)
assert(mask[0])
assert(not mask[1:].any())

mask = isnull(pidx[1:])
assert(not mask.any())


class TestIsNull(tm.TestCase):
def test_0d_array(self):
Expand Down