Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pandas · Function

pd.Categorical

pd.Categorical constructs a categorical array from values, with optional explicit categories and ordering. Explicit categories define permitted values; out-of-category inputs become missing.

How it works

pd.Categorical creates a categorical array from values, optionally using an explicit unique category set and an ordering flag. With ordered=True, sorting and comparisons use the category order; values outside an explicit category set become missing.

Syntax

pandas.Categorical(values, categories=None, ordered=False, dtype=None, fastpath=<no_default>)

Parameters

NameTypeDescription
valueslist-likeInput values used to construct the categorical.
categoriesIndex-like, optionalUnique category labels; if omitted, pandas infers them from values.
orderedbool, default FalseWhether the categorical has an ordered relationship; default False.
dtypeCategoricalDtype or None, optionalOptional categorical dtype information used during construction.

Returns

Categorical

Key points

  • The constructor produces a pandas categorical array suited to variables whose values come from a bounded set.
  • Categorical data is appropriate when the domain contains a small, generally stable collection of labels.
  • Without an explicit category list, pandas derives categories from distinct input values.
  • Inferred categories are sorted when feasible; otherwise their input appearance order is retained.
  • The ordered flag determines whether pandas treats the category labels as having an ordering.
  • Sorting an ordered categorical follows the sequence stored in categories.

Common mistakes

  • Providing categories is restrictive: any input value absent from that category list is converted to missing data.
  • The category collection must be unique; duplicate category labels are not a valid category definition.

Example: Create an ordered categorical

import pandas as pd

values = pd.Categorical(
    ["low", "high", "medium", "unknown"],
    categories=["low", "medium", "high"],
    ordered=True,
)

print(values.categories.tolist())
print(values.ordered)
print(values.tolist())

Creates an ordered categorical with an explicit category set; the value outside that set becomes missing.

When to use it

Encode fixed categories

Represent a bounded label domain such as workflow states or product tiers.

Control ordered sorting

Ensure sorting follows a business-defined sequence rather than lexical order.

Validate category membership

Normalize observed values against an allowed vocabulary and expose invalid labels as missing.

Interview questions

QWhat does pd.Categorical create, and what do its main options control?

pd.Categorical represents a categorical variable with a limited set of possible values. Its categories can be inferred from the input or supplied explicitly, and ordered controls whether category order is meaningful.

Follow-up: What happens to an input value omitted from an explicit categories list? It becomes NaN.

QHow are categories determined when categories is not provided?

When categories are omitted, pandas derives them from the unique input values, sorting them when possible and otherwise preserving their appearance order. Supplying categories lets you define the permitted vocabulary explicitly.

Follow-up: Why supply categories during construction? To establish a fixed set and control its order independently of the observed values.

QWhat effect does ordered=True have?

An ordered categorical respects the sequence in its categories attribute when sorted. With ordered=False, the categorical is not treated as having an ordered relationship.

Follow-up: How can the category order be changed after construction? Use methods such as reorder_categories or set_categories.

  • pandas.CategoricalDtype
  • pandas.CategoricalIndex
  • Categorical.from_codes
  • Categorical.as_ordered
  • Categorical.as_unordered
  • Categorical.set_categories
  • Categorical.reorder_categories

Source: pandas.Categorical. Checked 2026-10-15.