Skip to content

Commit 7cb3f1b

Browse files
committed
document prefix handling in categorical.rst
1 parent 5aa3313 commit 7cb3f1b

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

doc/source/user_guide/categorical.rst

+15-8
Original file line numberDiff line numberDiff line change
@@ -136,26 +136,33 @@ with each row having False in all but one column (True).
136136
These are called `dummy variables <https://en.wikipedia.org/wiki/Dummy_variable_(statistics)>`_, or one-hot encoding.
137137
:class:`pandas.Categorical` objects can easily be converted to and from such an encoding.
138138

139+
:meth:`pandas.Categorical.get_dummies` produces a dataframe of dummy variables.
140+
It works in the same way and supports most of the same arguments as :func:`pandas.get_dummies`.
141+
142+
.. ipython:: python
143+
144+
cat = pd.Categorical(["a", "b", "b", "c"])
145+
cat
146+
147+
cat.get_dummies()
148+
139149
The :meth:`pandas.Categorical.from_dummies` class method accepts a dataframe
140150
whose dtypes are coercible to boolean, and an ``ordered`` argument
141151
for whether the resulting ``Categorical`` should be considered ordered
142152
(like the ``Categorical`` constructor).
143153
A column with a NA index will be ignored.
144154
Any row which is entirely falsey, or has a missing value,
145155
will be uncategorised.
146-
147-
:meth:`pandas.Categorical.get_dummies` produces a dataframe of dummy variables.
148-
It works in the same way and supports most of the same arguments as :func:`pandas.get_dummies`.
156+
In the same way that :func:`pandas.get_dummies` can add a prefix to string category names,
157+
:meth:`~pandas.Categorical.from_dummies` can filter a dataframe for columns with a prefix:
158+
the resulting ``Categorical`` will have the prefix stripped from its categories.
149159

150160
.. ipython:: python
151161
152-
cat = pd.Categorical(["a", "b", "b", "c"])
153-
cat
154-
155-
dummies = cat.get_dummies()
162+
dummies = pd.get_dummies(["a", "b", "b", "c"], prefix="cat")
156163
dummies
157164
158-
pd.Categorical.from_dummies(dummies)
165+
pd.Categorical.from_dummies(dummies, prefix="cat")
159166
160167
161168
.. versionadded:: 1.2.0

0 commit comments

Comments
 (0)