Skip to content

Commit 68dc76b

Browse files
committed
Series.value_counts: Preserve original ordering
This fixes pandas-dev#12679.
1 parent cd51bdd commit 68dc76b

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

pandas/core/algorithms.py

+2
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,8 @@ def value_counts(values, sort=True, ascending=False, normalize=False,
458458
if not isinstance(keys, Index):
459459
keys = Index(keys)
460460
result = Series(counts, index=keys, name=name)
461+
# Use same index as the original values
462+
result = result.reindex(values.unique())
461463

462464
if bins is not None:
463465
# TODO: This next line should be more efficient
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from pandas import Series
2+
import pandas.util.testing as tm
3+
4+
5+
def test_original_ordering_value_counts():
6+
# All items occour exactly once. No matter if sorted or not, the resulting
7+
# values should be in the same order.
8+
s = Series(list('bacdef'))
9+
10+
# Garantee the same index if value_counts(sort=False) is used
11+
vc = s.value_counts(sort=False)
12+
tm.assert_series_equal(Series(vc.index), s)
13+
14+
15+
def test_original_ordering_value_counts2():
16+
# 'a' is there twice. Unsorted, it should be there at the top, unsorted it
17+
# should stay where it is.
18+
s = Series(list('bacaef'))
19+
ref_nonsorted = Series(list('bacef'))
20+
21+
# Garantee the same index if value_counts(sort=False) is used
22+
vc = s.value_counts(sort=False)
23+
tm.assert_series_equal(Series(vc.index), ref_nonsorted)
24+
25+
26+
def test_original_ordering_value_counts_ascending():
27+
# All items occour exactly once. No matter if sorted or not, the resulting
28+
# values should be in the same order.
29+
s = Series(list('bacdef'))
30+
31+
# Garantee the same index if value_counts(sort=False) is used
32+
vc = s.value_counts(sort=False, ascending=True)
33+
tm.assert_series_equal(Series(vc.index), s)
34+
35+
36+
def test_original_ordering_value_counts_ascending2():
37+
# 'a' is there twice. Unsorted, it should be there at the bottom, unsorted it
38+
# should stay where it is.
39+
s = Series(list('bacaef'))
40+
ref = Series(list('bacef'))
41+
42+
# Garantee the same index if value_counts(sort=False) is used
43+
vc = s.value_counts(sort=False, ascending=True)
44+
tm.assert_series_equal(Series(vc.index), ref)

0 commit comments

Comments
 (0)