Skip to content

Commit ba64ab5

Browse files
authored
DOC: Add examples for categorical functions (#43942)
1 parent 2c7393b commit ba64ab5

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

pandas/core/arrays/categorical.py

+39
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,17 @@ def add_categories(self, new_categories, inplace=no_default):
11631163
remove_categories : Remove the specified categories.
11641164
remove_unused_categories : Remove categories which are not used.
11651165
set_categories : Set the categories to the specified ones.
1166+
1167+
Examples
1168+
--------
1169+
>>> c = pd.Categorical(['c', 'b', 'c'])
1170+
>>> c
1171+
['c', 'b', 'c']
1172+
Categories (2, object): ['b', 'c']
1173+
1174+
>>> c.add_categories(['d', 'a'])
1175+
['c', 'b', 'c']
1176+
Categories (4, object): ['b', 'c', 'd', 'a']
11661177
"""
11671178
if inplace is not no_default:
11681179
warn(
@@ -1227,6 +1238,17 @@ def remove_categories(self, removals, inplace=no_default):
12271238
add_categories : Add new categories.
12281239
remove_unused_categories : Remove categories which are not used.
12291240
set_categories : Set the categories to the specified ones.
1241+
1242+
Examples
1243+
--------
1244+
>>> c = pd.Categorical(['a', 'c', 'b', 'c', 'd'])
1245+
>>> c
1246+
['a', 'c', 'b', 'c', 'd']
1247+
Categories (4, object): ['a', 'b', 'c', 'd']
1248+
1249+
>>> c.remove_categories(['d', 'a'])
1250+
[NaN, 'c', 'b', 'c', NaN]
1251+
Categories (2, object): ['b', 'c']
12301252
"""
12311253
if inplace is not no_default:
12321254
warn(
@@ -1286,6 +1308,23 @@ def remove_unused_categories(self, inplace=no_default):
12861308
add_categories : Add new categories.
12871309
remove_categories : Remove the specified categories.
12881310
set_categories : Set the categories to the specified ones.
1311+
1312+
Examples
1313+
--------
1314+
>>> c = pd.Categorical(['a', 'c', 'b', 'c', 'd'])
1315+
>>> c
1316+
['a', 'c', 'b', 'c', 'd']
1317+
Categories (4, object): ['a', 'b', 'c', 'd']
1318+
1319+
>>> c[2] = 'a'
1320+
>>> c[4] = 'c'
1321+
>>> c
1322+
['a', 'c', 'a', 'c', 'c']
1323+
Categories (4, object): ['a', 'b', 'c', 'd']
1324+
1325+
>>> c.remove_unused_categories()
1326+
['a', 'c', 'a', 'c', 'c']
1327+
Categories (2, object): ['a', 'c']
12891328
"""
12901329
if inplace is not no_default:
12911330
warn(

0 commit comments

Comments
 (0)