Pandas Categorical Ordered Quick Fix
You will learn how to create ordered categorical columns in pandas so that sorting, comparison operators, and min/max respect your category hierarchy instead of falling back to alphabetical order.
The Problem
By default, pandas categoricals are unordered. Sorting a categorical column sorts categories alphabetically, ignoring any natural hierarchy like ["low", "medium", "high"]. This leads to incorrect results in summaries, plots, and feature engineering.
The Wrong Way
import pandas as pd
s = pd.Series(["high", "low", "medium"], dtype="category")
print(s.sort_values())
Output:
1 low
2 medium
0 high
dtype: category
Categories (3, object): ['high', 'low', 'medium']
Alphabetical sorting puts high before low, which is semantically wrong.
The Right Way
from pandas.api.types import CategoricalDtype
cat_ord = CategoricalDtype(categories=["low", "medium", "high"], ordered=True)
s = pd.Series(["high", "low", "medium"], dtype=cat_ord)
print(s.sort_values())
print(s.min(), "<", s.max())
Output:
1 low
2 medium
0 high
dtype: category
Categories (3, object): ['low' < 'medium' < 'high']
low < high
Sorting and comparisons now follow the defined category order.
Step-by-Step Fix
1. Define a CategoricalDtype with ordered=True
dtype = CategoricalDtype(categories=["cold", "warm", "hot"], ordered=True)
2. Apply to your data
temps = pd.Series(["hot", "cold", "warm"], dtype=dtype)
3. Use comparison operators
print(temps > "cold") # True for warm and hot
Prevention Tips
- Set
ordered=Truewhenever categories have a natural rank (e.g., sizes, ratings, priority levels) - Always specify
categoriesexplicitly inCategoricalDtypeto fix the order - Use
.cat.reorder_categories()to change order on existing categoricals - Verify ordering with
.cat.orderedwhich returns a boolean
Common Mistakes
- Assuming
pd.Categorical()preserves insertion order -- it does not unlesscategoriesis explicitly provided - Using
sort_values()on unordered categoricals -- yields alphabetical or insertion order - Comparing categoricals with
<and>on unordered types -- raisesTypeError
These mistakes appear frequently in real-world pandas code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
FAQ
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro. DodaTech tools integrate seamlessly with pandas for enhanced productivity and security.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro