Skip to content

ENH: added nunique function to Index #6734

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
Apr 6, 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
6 changes: 4 additions & 2 deletions doc/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -348,16 +348,16 @@ Computations / Descriptive Stats
Series.median
Series.min
Series.mode
Series.nunique
Series.pct_change
Series.prod
Series.quantile
Series.rank
Series.skew
Series.std
Series.sum
Series.unique
Series.var
Series.unique
Series.nunique
Series.value_counts

Reindexing / Selection / Label manipulation
Expand Down Expand Up @@ -1053,6 +1053,8 @@ Modifying and Computations
Index.repeat
Index.set_names
Index.unique
Index.nunique
Index.value_counts

Conversion
~~~~~~~~~~
Expand Down
2 changes: 2 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ API Changes
- Arithmetic ops are now disallowed when passed two bool dtype Series or
DataFrames (:issue:`6762`).

- Added ``nunique`` and ``value_counts`` functions to ``Index`` for counting unique elements. (:issue:`6734`)

Deprecations
~~~~~~~~~~~~

Expand Down
1 change: 1 addition & 0 deletions doc/source/v0.14.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ API changes
- ``Series.iteritems()`` is now lazy (returns an iterator rather than a list). This was the documented behavior prior to 0.14. (:issue:`6760`)
- ``Panel.shift`` now uses ``NDFrame.shift``. It no longer drops the ``nan`` data and retains its original shape. (:issue:`4867`)

- Added ``nunique`` and ``value_counts`` functions to ``Index`` for counting unique elements. (:issue:`6734`)

MultiIndexing Using Slicers
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
50 changes: 50 additions & 0 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,56 @@ def min(self):
self._is_allowed_index_op('min')
return self.values.min()

def value_counts(self, normalize=False, sort=True, ascending=False,
bins=None):
"""
Returns object containing counts of unique values. The resulting object
will be in descending order so that the first element is the most
frequently-occurring element. Excludes NA values.

Parameters
----------
normalize : boolean, default False
If True then the object returned will contain the relative
frequencies of the unique values.
sort : boolean, default True
Sort by values
ascending : boolean, default False
Sort in ascending order
bins : integer, optional
Rather than count values, group them into half-open bins,
a convenience for pd.cut, only works with numeric data

Returns
-------
counts : Series
"""
from pandas.core.algorithms import value_counts
return value_counts(self.values, sort=sort, ascending=ascending,
normalize=normalize, bins=bins)

def unique(self):
"""
Return array of unique values in the object. Significantly faster than
numpy.unique. Includes NA values.

Returns
-------
uniques : ndarray
"""
from pandas.core.nanops import unique1d
return unique1d(self.values)

def nunique(self):
"""
Return count of unique elements in the object. Excludes NA values.

Returns
-------
nunique : int
"""
return len(self.value_counts())

date = _field_accessor('date','Returns numpy array of datetime.date. The date part of the Timestamps')
time = _field_accessor('time','Returns numpy array of datetime.time. The time part of the Timestamps')
year = _field_accessor('year', "The year of the datetime")
Expand Down
12 changes: 0 additions & 12 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1102,18 +1102,6 @@ def sym_diff(self, other, result_name=None):
the_diff = sorted(set((self - other) + (other - self)))
return Index(the_diff, name=result_name)

def unique(self):
"""
Return array of unique values in the Index. Significantly faster than
numpy.unique

Returns
-------
uniques : ndarray
"""
from pandas.core.nanops import unique1d
return unique1d(self.values)

def get_loc(self, key):
"""
Get integer location for requested label
Expand Down
49 changes: 0 additions & 49 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1095,34 +1095,6 @@ def count(self, level=None):

return notnull(_values_from_object(self)).sum()

def value_counts(self, normalize=False, sort=True, ascending=False,
bins=None):
"""
Returns Series containing counts of unique values. The resulting Series
will be in descending order so that the first element is the most
frequently-occurring element. Excludes NA values

Parameters
----------
normalize : boolean, default False
If True then the Series returned will contain the relative
frequencies of the unique values.
sort : boolean, default True
Sort by values
ascending : boolean, default False
Sort in ascending order
bins : integer, optional
Rather than count values, group them into half-open bins,
a convenience for pd.cut, only works with numeric data

Returns
-------
counts : Series
"""
from pandas.core.algorithms import value_counts
return value_counts(self.values, sort=sort, ascending=ascending,
normalize=normalize, bins=bins)

def mode(self):
"""Returns the mode(s) of the dataset.

Expand All @@ -1143,27 +1115,6 @@ def mode(self):
from pandas.core.algorithms import mode
return mode(self)

def unique(self):
"""
Return array of unique values in the Series. Significantly faster than
numpy.unique

Returns
-------
uniques : ndarray
"""
return nanops.unique1d(self.values)

def nunique(self):
"""
Return count of unique elements in the Series

Returns
-------
nunique : int
"""
return len(self.value_counts())

def drop_duplicates(self, take_last=False, inplace=False):
"""
Return Series with duplicate values removed
Expand Down
Loading