Skip to content

Commit

Permalink
ENH: implement Series.unique, disable comparison-alignment with Serie…
Browse files Browse the repository at this point in the history
…s, GH #658
  • Loading branch information
wesm committed Jan 20, 2012
1 parent 849a77c commit ced23fd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
3 changes: 1 addition & 2 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ pandas 0.7.0
yielding an aggregated result with hierarchical columns (GH #166)
- Add integer-indexing functions ``iget`` in Series and ``irow`` / ``iget``
in DataFrame (GH #628)
- Add automatic realignment functionality (when possible) to comparisons and
logical operators for Series
- Add new ``Series.unique`` function (GH #658)

**API Changes**

Expand Down
15 changes: 15 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,21 @@ def value_counts(self):
counter[value] += 1
return Series(counter).order(ascending=False)

def unique(self):
"""
Return array of unique values in the Series. Significantly faster than
numpy.unique
Returns
-------
uniques : ndarray
"""
values = self.values
if not values.dtype == np.object_:
values = values.astype('O')
uniques = lib.list_to_object_array(lib.fast_unique(values))
return lib.maybe_convert_objects(uniques)

def nunique(self):
"""
Return count of unique elements in the Series
Expand Down
7 changes: 5 additions & 2 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,8 +999,7 @@ def _check_op(other, op, pos_only=False):
tm.assert_almost_equal(cython_or_numpy, python)

def check(other):
simple_ops = ['add', 'sub', 'mul', 'truediv', 'floordiv',
'gt', 'ge', 'lt', 'le']
simple_ops = ['add', 'sub', 'mul', 'truediv', 'floordiv']

for opname in simple_ops:
_check_op(other, getattr(operator, opname))
Expand Down Expand Up @@ -1980,6 +1979,10 @@ def test_dropna_preserve_name(self):
result = self.ts.dropna()
self.assertEquals(result.name, self.ts.name)

def test_numpy_unique(self):
# it works!
result = np.unique(self.ts)

if __name__ == '__main__':
nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],
exit=False)
Expand Down

0 comments on commit ced23fd

Please sign in to comment.