cudf.core.column.categorical.CategoricalAccessor.reorder_categories#

CategoricalAccessor.reorder_categories(new_categories: Any, ordered: bool = False, inplace: bool = False) SeriesOrIndex | None#

Reorder categories as specified in new_categories.

new_categories need to include all old categories and no new category items.

Parameters:
new_categoriesIndex-like

The categories in new order.

orderedbool, optional

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

inplacebool, default False

Whether or not to reorder the categories inplace or return a copy of this categorical with reordered categories.

Deprecated since version 23.04: The inplace parameter is is deprecated and will be removed in a future version of cudf. Reordering categories will always return a new Categorical object.

Returns:
cat

Categorical with reordered categories or None if inplace.

Raises:
ValueError

If the new categories do not contain all old category items or any new ones.

Examples

>>> import cudf
>>> s = cudf.Series([10, 1, 1, 2, 10, 2, 10], dtype="category")
>>> s
0    10
1     1
2     1
3     2
4    10
5     2
6    10
dtype: category
Categories (3, int64): [1, 2, 10]
>>> s.cat.reorder_categories([10, 1, 2])
0    10
1     1
2     1
3     2
4    10
5     2
6    10
dtype: category
Categories (3, int64): [10, 1, 2]
>>> s.cat.reorder_categories([10, 1])
ValueError: items in new_categories are not the same as in
old categories