Skip to content

Add doc for counting categorical dtype #59327

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 8 commits into from
Jul 30, 2024
Merged
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
28 changes: 28 additions & 0 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,34 @@ def value_counts(
4.0 1
NaN 1
Name: count, dtype: int64

**Categorical Dtypes**

Rows with categorical type will be counted as one group
if they have same categories and order.
In the example below, even though ``a``, ``c``, and ``d``
all have the same data types of ``category``,
only ``c`` and ``d`` will be counted as one group
since ``a`` doesn't have the same categories.

>>> df = pd.DataFrame({"a": [1], "b": ["2"], "c": [3], "d": [3]})
>>> df = df.astype({"a": "category", "c": "category", "d": "category"})
>>> df
a b c d
0 1 2 3 3

>>> df.dtypes
a category
b object
c category
d category
dtype: object

>>> df.dtypes.value_counts()
category 2
category 1
object 1
Name: count, dtype: int64
"""
return algorithms.value_counts_internal(
self,
Expand Down
Loading