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

Pandas · Pipe

cat accessor

Series.cat provides access to categorical properties and operations. In a DataFrame workflow, select a categorical column and use .cat on the resulting Series.

How it works

The cat accessor exposes categorical properties and operations for a Series whose values use pandas' categorical dtype. Access it through a categorical DataFrame column, then call accessor attributes or methods such as categories and codes.

Syntax

Series.cat

Parameters

NameTypeDescription
dataSeries or CategoricalIndexThe Series or CategoricalIndex to which the categorical accessor is attached.

Returns

An accessor object for categorical properties of the Series values.

Key points

  • cat is an accessor object for categorical properties.
  • A DataFrame column can carry categorical data, making it the usual entry point for using cat in DataFrame workflows.
  • Use the categorical dtype during construction when creating categorical data from the start.
  • Standard DataFrame indexing methods continue to work with categorical columns.
  • Assignments to categorical data are restricted to values already listed in its categories.
  • Slicing that returns a DataFrame or a column Series preserves the categorical dtype.

Common mistakes

  • cat applies to categorical Series values; a regular object- or string-typed column must first be converted to categorical dtype.
  • Other accessors depend on the types of the categories, so categorical data does not automatically make every accessor operation valid.

Example: Inspect categorical column metadata

import pandas as pd

df = pd.DataFrame({"animal": ["cat", "dog", "cat"]})
df["animal"] = df["animal"].astype("category")
print(df["animal"].cat.categories.tolist())
print(df["animal"].cat.codes.tolist())

Convert a column to categorical data, then inspect its categories and integer codes.

When to use it

Read category metadata

Inspect the distinct labels and internal integer representation of a categorical DataFrame column.

Manipulate categories

Apply category-level transformations to a selected DataFrame column.

Process categorical DataFrame data

Work with categorical columns while retaining pandas indexing and slicing behavior.

Interview questions

QWhat does the pandas cat accessor provide?

cat is an accessor for categorical properties of Series values. For a DataFrame, select a categorical column first, for example df["animal"].cat.categories.

Follow-up: How would you make a non-categorical column usable with cat? Convert it with astype("category") or assign a categorical dtype.

QHow do you expose cat from a DataFrame?

A DataFrame column can be categorical when constructed with dtype="category" or converted afterward. Selecting that column returns a Series on which the accessor is available.

Follow-up: What must be preserved when assigning categorical values? The assigned values must already belong to the column's categories.

QIs cat a DataFrame-wide accessor?

The accessor exposes categorical metadata and transformations, including category labels and category codes. Operations are performed on the Series selected from the DataFrame rather than directly on the DataFrame object.

Follow-up: What happens if a row selection changes the result to a non-categorical Series? The category dtype may not be preserved for a single-row result, so the accessor may no longer be available.

  • pandas.Series.cat.categories
  • pandas.Series.cat.codes
  • pandas.Series.cat.rename_categories
  • pandas.Series.cat.set_categories
  • pandas.Series.cat.remove_unused_categories

Source: pandas.Series.cat. Checked 2026-10-15.