pandas.CategoricalIndex.remove_categories#

CategoricalIndex.remove_categories(removals)[source]#

Remove the specified categories.

The removals argument must be a subset of the current categories. Any values that were part of the removed categories will be set to NaN.

Parameters:
removalscategory or list of categories

The categories which should be removed.

Returns:
CategoricalIndex

CategoricalIndex with removed categories.

Raises:
ValueError

If the removals are not contained in the categories

See also

CategoricalIndex.rename_categories

Rename categories.

CategoricalIndex.reorder_categories

Reorder categories.

CategoricalIndex.add_categories

Add new categories.

CategoricalIndex.remove_unused_categories

Remove categories which are not used.

CategoricalIndex.set_categories

Set the categories to the specified ones.

Examples

>>> ci = pd.CategoricalIndex(["a", "c", "b", "c", "d"])
>>> ci
CategoricalIndex(['a', 'c', 'b', 'c', 'd'], categories=['a', 'b', 'c', 'd'],
                 ordered=False, dtype='category')
>>> ci.remove_categories(["d", "a"])
CategoricalIndex([nan, 'c', 'b', 'c', nan], categories=['b', 'c'],
                 ordered=False, dtype='category')