Skip to content

Commit

Permalink
ENH: rename histogram->value_counts and sort descending, GH #265
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Oct 20, 2011
1 parent e7b36bc commit 06130c6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
11 changes: 6 additions & 5 deletions pandas/core/series.py
Expand Up @@ -592,20 +592,21 @@ def _count_level(self, level):

return Series(result, index=level_index)

def histogram(self):
def value_counts(self):
"""
Returns Series containing counts of unique values. The result Series's
index will be the sorted unique values
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
Returns
-------
histogram : Series
counts : Series
"""
from collections import defaultdict
counter = defaultdict(lambda: 0)
for value in self.values:
counter[value] += 1
return Series(counter)
return Series(counter).order(ascending=False)

def sum(self, axis=0, dtype=None, out=None, skipna=True):
"""
Expand Down
10 changes: 5 additions & 5 deletions pandas/tests/test_series.py
Expand Up @@ -759,14 +759,14 @@ def test_count(self):

self.assertEqual(self.ts.count(), np.isfinite(self.ts).sum())

def test_histogram(self):
s = Series(['a', 'b', 'b', 'b', 'a', 'c', 'd', 'd', 'a'])
hist = s.histogram()
expected = Series([3, 3, 1, 2], index=['a', 'b', 'c', 'd'])
def test_value_counts(self):
s = Series(['a', 'b', 'b', 'b', 'b', 'a', 'c', 'd', 'd', 'a'])
hist = s.value_counts()
expected = Series([4, 3, 2, 1], index=['b', 'a', 'd', 'c'])
assert_series_equal(hist, expected)

s = Series({})
hist = s.histogram()
hist = s.value_counts()
expected = Series([])
assert_series_equal(hist, expected)

Expand Down

0 comments on commit 06130c6

Please sign in to comment.