pandas.CategoricalIndex.remove_categories — pandas 2.2.3 documentation (original) (raw)

CategoricalIndex.remove_categories(*args, **kwargs)[source]#

Remove the specified categories.

removals must be included in the old categories. Values which were in the removed categories will be set to NaN

Parameters:

removalscategory or list of categories

The categories which should be removed.

Returns:

Categorical

Categorical with removed categories.

Raises:

ValueError

If the removals are not contained in the categories

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']