pandas.Categorical.from_codes

pandas 3.1.1 · Arrays, scalars, and data types · method

Build a categorical object using integer codes and existing categories.

Call syntax

Categorical.from_codes(codes, categories=None, ordered=None, dtype=None, validate=True)

Run the example

Edit the example and select Run Python to see the result.

Parameters

ParameterRequirementAccepted values and behavior
codes Required
Positional or keyword
array-like of int

Provide integer positions that refer to the supplied category or index level labels.

categories Optional
Positional or keyword
Default None
index-like
ordered Optional
Positional or keyword
Default None
bool

Choose whether the category sequence defines an ordering.

dtype Optional
Positional or keyword
Default None
CategoricalDtype or "category"

Choose the data type for the input conversion or result.

validate Optional
Positional or keyword
Default True
bool

Check that key multiplicity matches the required relationship, such as "one_to_one" or "many_to_one".

Returns

Categorical