From 268a175df3b9d1d8fcccfc578a7e49cd15eae85e Mon Sep 17 00:00:00 2001 From: Andrew Eckart Date: Sat, 9 Oct 2021 11:50:56 -0500 Subject: [PATCH 1/2] DOC: Add simple examples for add_categories, remove_categories, and remove_unused_categories --- pandas/core/arrays/categorical.py | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 7e3bf33f411bb..3403d7b917379 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -1163,6 +1163,16 @@ def add_categories(self, new_categories, inplace=no_default): remove_categories : Remove the specified categories. remove_unused_categories : Remove categories which are not used. set_categories : Set the categories to the specified ones. + + Examples + -------- + >>> c = pd.Categorical(['c', 'b', 'c']) + >>> c + ['c', 'b', 'c'] + Categories (2, object): ['b', 'c'] + >>> c.add_categories(['d', 'a']) + ['c', 'b', 'c'] + Categories (4, object): ['b', 'c', 'd', 'a'] """ if inplace is not no_default: warn( @@ -1227,6 +1237,16 @@ def remove_categories(self, removals, inplace=no_default): add_categories : Add new categories. remove_unused_categories : Remove categories which are not used. set_categories : Set the categories to the specified ones. + + Examples + -------- + >>> c = pd.Categorical(['a', 'c', 'b', 'c', 'd']) + >>> c + ['a', 'c', 'b', 'c', 'd'] + Categories (4, object): ['a', 'b', 'c', 'd'] + >>> c.remove_categories(['d', 'a']) + [NaN, 'c', 'b', 'c', NaN] + Categories (2, object): ['b', 'c'] """ if inplace is not no_default: warn( @@ -1286,6 +1306,21 @@ def remove_unused_categories(self, inplace=no_default): add_categories : Add new categories. remove_categories : Remove the specified categories. set_categories : Set the categories to the specified ones. + + Examples + -------- + >>> c = pd.Categorical(['a', 'c', 'b', 'c', 'd']) + >>> c + ['a', 'c', 'b', 'c', 'd'] + Categories (4, object): ['a', 'b', 'c', 'd'] + >>> c[2] = 'a' + >>> c[4] = 'c' + >>> c + ['a', 'c', 'a', 'c', 'c'] + Categories (4, object): ['a', 'b', 'c', 'd'] + >>> c.remove_unused_categories() + ['a', 'c', 'a', 'c', 'c'] + Categories (2, object): ['a', 'c'] """ if inplace is not no_default: warn( From ec7af6aaf2fb975ec539c92bbd2efc5dbb15d43a Mon Sep 17 00:00:00 2001 From: Andrew Eckart Date: Sun, 10 Oct 2021 17:39:08 -0500 Subject: [PATCH 2/2] DOC: Add blank lines per PR feedback --- pandas/core/arrays/categorical.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 3403d7b917379..a4d6c0f3cd832 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -1170,6 +1170,7 @@ def add_categories(self, new_categories, inplace=no_default): >>> c ['c', 'b', 'c'] Categories (2, object): ['b', 'c'] + >>> c.add_categories(['d', 'a']) ['c', 'b', 'c'] Categories (4, object): ['b', 'c', 'd', 'a'] @@ -1244,6 +1245,7 @@ def remove_categories(self, removals, inplace=no_default): >>> c ['a', 'c', 'b', 'c', 'd'] Categories (4, object): ['a', 'b', 'c', 'd'] + >>> c.remove_categories(['d', 'a']) [NaN, 'c', 'b', 'c', NaN] Categories (2, object): ['b', 'c'] @@ -1313,11 +1315,13 @@ def remove_unused_categories(self, inplace=no_default): >>> c ['a', 'c', 'b', 'c', 'd'] Categories (4, object): ['a', 'b', 'c', 'd'] + >>> c[2] = 'a' >>> c[4] = 'c' >>> c ['a', 'c', 'a', 'c', 'c'] Categories (4, object): ['a', 'b', 'c', 'd'] + >>> c.remove_unused_categories() ['a', 'c', 'a', 'c', 'c'] Categories (2, object): ['a', 'c']