cat.set_categories(new_categories: Union[pandas.core.indexes.base.Index, List], ordered: Optional[bool] = None, rename: bool = False) → Optional[ps.Series]

Set the categories to the specified new_categories.

new_categories can include new categories (which will result in unused categories) or remove old categories (which results in values set to NaN). If rename==True, the categories will simply be renamed (less or more items than in old categories will result in values set to NaN or in unused categories respectively).

This method can be used to perform more than one action of adding, removing, and reordering simultaneously and is therefore faster than performing the individual steps via the more specialised methods.

On the other hand this methods does not do checks (e.g., whether the old categories are included in the new categories on a reorder), which can result in surprising changes, for example when using special string dtypes, which does not consider a S1 string equal to a single char python string.


The categories in new order.

orderedbool, default False

Whether or not the categorical is treated as an ordered categorical. If not given, do not change the ordered information.

renamebool, default False

Whether or not the new_categories should be considered as a rename of the old categories or as reordered categories.

Series with reordered categories or None if inplace.

If new_categories does not validate as categories

See also


Rename categories.


Reorder categories.


Add new categories.


Remove the specified categories.


Remove categories which are not used.


>>> s = ps.Series(list("abbccc"), dtype="category")
>>> s  
0    a
1    b
2    b
3    c
4    c
5    c
dtype: category
Categories (3, object): ['a', 'b', 'c']
>>>['b', 'c'])  
0    NaN
1      b
2      b
3      c
4      c
5      c
dtype: category
Categories (2, object): ['b', 'c']
>>>[1, 2, 3], rename=True)  
0    1
1    2
2    2
3    3
4    3
5    3
dtype: category
Categories (3, int64): [1, 2, 3]
>>>[1, 2, 3], rename=True, ordered=True)  
0    1
1    2
2    2
3    3
4    3
5    3
dtype: category
Categories (3, int64): [1 < 2 < 3]