Skip to content

Commit 1348bdc

Browse files
committed
Move example to docstring instead
1 parent e4be568 commit 1348bdc

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

doc/source/user_guide/categorical.rst

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,12 +1185,4 @@ Counting CategoricalDtype
11851185
~~~~~~~~~~~~~~~~~~~~~~~~~
11861186

11871187
As mentioned in :ref:`Equality Semantics <categorical.equalitysemantics>`, two instances of :class:`~pandas.api.types.CategoricalDtype` compare equal
1188-
whenever they have the same categories and order. Therefore, when counting data types, the multiple instances of :class:`~pandas.api.types.CategoricalDtype` will be counted as one group if they have the same categories and order.
1189-
In the example below, even though ``a``, ``c``, and ``d`` all have data types of ``category``, they will not be counted as one group since they don't have the same categories.
1190-
1191-
.. ipython:: python
1192-
1193-
df = pd.DataFrame({'a': [1], 'b': ['2'], 'c': [3], 'd': [3]}).astype({'a': 'category', 'c': 'category', 'd': 'category'})
1194-
df
1195-
df.dtypes
1196-
df.dtypes.value_counts()
1188+
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.

pandas/core/base.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,34 @@ def value_counts(
10491049
4.0 1
10501050
NaN 1
10511051
Name: count, dtype: int64
1052+
1053+
**categorial_dtypes**
1054+
1055+
Rows with categorical type will be counted as one group\
1056+
if they have same categories and order.\
1057+
In the example below, even though ``a``, ``c``, and ``d``\
1058+
all have the same data types of ``category``,\
1059+
only ``c`` and ``d`` will be counted as one group\
1060+
since ``a`` doesn't have the same categories.
1061+
1062+
>>> df = pd.DataFrame({"a": [1], "b": ["2"], "c": [3], "d": [3]})
1063+
.astype({'a': 'category', 'c': 'category', 'd': 'category'})
1064+
>>> df
1065+
a b c d
1066+
0 1 2 3 3
1067+
1068+
>>> df.dtypes
1069+
a category
1070+
b object
1071+
c category
1072+
d category
1073+
dtype: object
1074+
1075+
>>> df.dtypes.value_counts()
1076+
category 2
1077+
category 1
1078+
object 1
1079+
Name: count, dtype: int64
10521080
"""
10531081
return algorithms.value_counts_internal(
10541082
self,

0 commit comments

Comments
 (0)