What is an ordered categorical, and how does it enable comparison and sorting?

Quick Overview

Explain what an ordered categorical is in pandas, how to create one, and how it changes comparisons, sorting and min/max compared with unordered categoricals and plain strings. It tests working knowledge of the category dtype and its common pitfalls.

What is an ordered categorical, and how does it enable comparison and sorting?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

What is an ordered categorical in pandas, and how does it enable comparison and sorting? Explain how to create one and how it behaves differently from an unordered categorical and from a plain string column. ```hint Where the order lives Look at what a categorical stores internally and which operations need a ranking, as opposed to only equality. ``` ### What a Strong Answer Covers - What makes a categorical ordered and where its order comes from - How to create one, and how to add or change the order on an existing column - Which operations an ordered categorical supports that an unordered one does not - How sorting a categorical differs from sorting plain strings - At least one pitfall, such as values missing from the category list or inferred alphabetical order ### Follow-up Questions - What does `pd.Categorical(["low", "Medium", "high"], categories=["low", "medium", "high"], ordered=True)` produce, and why is that dangerous? - Can you compare two ordered categorical Series whose categories are the same labels in a different order? - Why would you convert a column to an ordered categorical before a groupby or a plot, and what does the `observed` argument of `groupby` change? - How would you encode an ordered categorical feature for a model, and when is one-hot encoding the better choice?

Overview: Explain what an ordered categorical is in pandas, how to create one, and how it changes comparisons, sorting and min/max compared with unordered categoricals and plain strings. It tests working knowledge of the category dtype and its common pitfalls.

General logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

What is an ordered categorical in pandas, and how does it enable comparison and sorting? Explain how to create one and how it behaves differently from an unordered categorical and from a plain string column.

What a Strong Answer Covers Guidance

  • What makes a categorical ordered and where its order comes from
  • How to create one, and how to add or change the order on an existing column
  • Which operations an ordered categorical supports that an unordered one does not
  • How sorting a categorical differs from sorting plain strings
  • At least one pitfall, such as values missing from the category list or inferred alphabetical order

Follow-up Questions Guidance

  • What does pd.Categorical(["low", "Medium", "high"], categories=["low", "medium", "high"], ordered=True) produce, and why is that dangerous?
  • Can you compare two ordered categorical Series whose categories are the same labels in a different order?
  • Why would you convert a column to an ordered categorical before a groupby or a plot, and what does the observed argument of groupby change?
  • How would you encode an ordered categorical feature for a model, and when is one-hot encoding the better choice?