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
Show file tree
Hide file tree
Changes from 5 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
8 changes: 8 additions & 0 deletions doc/source/user_guide/categorical.rst
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ expects a ``dtype``. For example :func:`pandas.read_csv`,
array. In other words, ``dtype='category'`` is equivalent to
``dtype=CategoricalDtype()``.

.. _categorical.equalitysemantics:

Equality semantics
~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -1178,3 +1180,9 @@ Use ``copy=True`` to prevent such a behaviour or simply don't reuse ``Categorica
This also happens in some cases when you supply a NumPy array instead of a ``Categorical``:
using an int array (e.g. ``np.array([1,2,3,4])``) will exhibit the same behavior, while using
a string array (e.g. ``np.array(["a","b","c","a"])``) will not.

Counting CategoricalDtype
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you undo the changes in this file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there's still changes in this file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry! Addressed here

~~~~~~~~~~~~~~~~~~~~~~~~~

As mentioned in :ref:`Equality Semantics <categorical.equalitysemantics>`, two instances of :class:`~pandas.api.types.CategoricalDtype` compare equal
whenever they have the same categories and order. Therefore, the multiple instances of :class:`~pandas.api.types.CategoricalDtype` will be counted as one group if they have the same categories and order.
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

**categorial_dtypes**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
**categorial_dtypes**
**Categorial Dtypes**

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed here


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