-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Series.value_counts: Preserve original ordering #24302
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
Changes from all commits
0270839
c857119
e966aa7
b827df6
453d3df
405ac0e
355f872
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -667,6 +667,7 @@ def value_counts(values, sort=True, ascending=False, normalize=False, | |
|
||
""" | ||
from pandas.core.series import Series, Index | ||
from pandas.core.dtypes.generic import ABCCategoricalIndex | ||
name = getattr(values, 'name', None) | ||
|
||
if bins is not None: | ||
|
@@ -695,19 +696,25 @@ def value_counts(values, sort=True, ascending=False, normalize=False, | |
if is_extension_array_dtype(values) or is_sparse(values): | ||
|
||
# handle Categorical and sparse, | ||
result = Series(values)._values.value_counts(dropna=dropna) | ||
vals = Series(values) | ||
uniq = vals._values.unique() | ||
result = vals._values.value_counts(dropna=dropna) | ||
result.name = name | ||
counts = result.values | ||
|
||
else: | ||
keys, counts = _value_counts_arraylike(values, dropna) | ||
uniq = unique(values) | ||
|
||
if not isinstance(keys, Index): | ||
keys = Index(keys) | ||
result = Series(counts, index=keys, name=name) | ||
|
||
if sort: | ||
result = result.sort_values(ascending=ascending) | ||
elif bins is None: | ||
if not isinstance(result.index, ABCCategoricalIndex): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should not be necessary There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It unfortunately is for cases, where the categories contain more elements than the values: #24302 (comment) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is not necessary otherwise the impl is incorrect categoricals uniq already handles this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't seem so as
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://github.com/pandas-dev/pandas/blob/master/pandas/core/arrays/categorical.py#L2267 also mentions that unused categories are not returned. It would be consistent to not return it in the |
||
result = result.reindex(uniq) | ||
|
||
if normalize: | ||
result = result / float(counts.sum()) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move to 0.25