Skip to content

Commit 0ffef1d

Browse files
committed
Merge pull request #8153 from JanSchulz/categorical_fixups_2
Fixups for categoricals
2 parents ec331f0 + 3f282da commit 0ffef1d

21 files changed

+1449
-966
lines changed

doc/source/10min.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -652,11 +652,11 @@ Since version 0.15, pandas can include categorical data in a ``DataFrame``. For
652652
# Alternative: df["grade"] = df["raw_grade"].astype("category")
653653
df["grade"]
654654
655-
# Rename the levels
656-
df["grade"].cat.levels = ["very good", "good", "very bad"]
655+
# Rename the categories inplace
656+
df["grade"].cat.categories = ["very good", "good", "very bad"]
657657
658-
# Reorder the levels and simultaneously add the missing levels
659-
df["grade"].cat.reorder_levels(["very bad", "bad", "medium", "good", "very good"])
658+
# Reorder the categories and simultaneously add the missing categories
659+
df["grade"] = df["grade"].cat.set_categories(["very bad", "bad", "medium", "good", "very good"])
660660
df["grade"]
661661
df.sort("grade")
662662
df.groupby("grade").size()

doc/source/api.rst

+14-7
Original file line numberDiff line numberDiff line change
@@ -555,26 +555,33 @@ Categorical
555555

556556
.. currentmodule:: pandas.core.categorical
557557

558-
If the Series is of dtype ``category``, ``Series.cat`` can be used to access the the underlying
559-
``Categorical``. This accessor is similar to the ``Series.dt`` or ``Series.str``and has the
558+
If the Series is of dtype ``category``, ``Series.cat`` can be used to change the the categorical
559+
data. This accessor is similar to the ``Series.dt`` or ``Series.str`` and has the
560560
following usable methods and properties (all available as ``Series.cat.<method_or_property>``).
561561

562562
.. autosummary::
563563
:toctree: generated/
564564

565-
Categorical.levels
565+
Categorical.categories
566566
Categorical.ordered
567-
Categorical.reorder_levels
568-
Categorical.remove_unused_levels
567+
Categorical.rename_categories
568+
Categorical.reorder_categories
569+
Categorical.add_categories
570+
Categorical.remove_categories
571+
Categorical.remove_unused_categories
572+
Categorical.set_categories
573+
Categorical.codes
574+
575+
To create a Series of dtype ``category``, use ``cat = s.astype("category")``.
569576

570-
The following methods are considered API when using ``Categorical`` directly:
577+
The following two ``Categorical`` constructors are considered API but should only be used when
578+
adding ordering information or special categories is need at creation time of the categorical data:
571579

572580
.. autosummary::
573581
:toctree: generated/
574582

575583
Categorical
576584
Categorical.from_codes
577-
Categorical.codes
578585

579586
``np.asarray(categorical)`` works by implementing the array interface. Be aware, that this converts
580587
the Categorical back to a numpy array, so levels and order information is not preserved!

0 commit comments

Comments
 (0)