pandas-dev/pandas · error · ValueError
items in new_categories are not the same as in old categorie
Error message
items in new_categories are not the same as in old categories
What it means
Raised by reorder_categories when the supplied new_categories differ in length or in members from the current categories. reorder_categories only permutes existing categories; it cannot add, drop, or rename them. The check uses both length equality and difference() to catch any deviation.
Source
Thrown at pandas/core/arrays/categorical.py:1362
3 a
dtype: category
Categories (3, str): ['c' < 'b' < 'a']
For :class:`pandas.CategoricalIndex`:
>>> ci = pd.CategoricalIndex(["a", "b", "c", "a"])
>>> ci
CategoricalIndex(['a', 'b', 'c', 'a'], categories=['a', 'b', 'c'],
ordered=False, dtype='category')
>>> ci.reorder_categories(["c", "b", "a"], ordered=True)
CategoricalIndex(['a', 'b', 'c', 'a'], categories=['c', 'b', 'a'],
ordered=True, dtype='category')
"""
if (
len(self.categories) != len(new_categories)
or not self.categories.difference(new_categories).empty
):
raise ValueError(
"items in new_categories are not the same as in old categories"
)
return self.set_categories(new_categories, ordered=ordered)
def add_categories(self, new_categories) -> Self:
"""
Add new categories.
`new_categories` will be included at the last/highest place in the
categories and will be unused directly after this call.
Parameters
----------
new_categories : category or list-like of category
The new categories to be included.
Returns
-------View on GitHub (pinned to 71959b8cb9)
Solutions
- Pass exactly the current categories in the new order: `cat.reorder_categories(cat.categories[::-1])`.
- To rename use `rename_categories(...)`; to change membership use `set_categories(...)`.
- Verify `set(new_categories) == set(cat.categories)` before calling reorder_categories.
Example fix
# before cat = pd.Categorical(['a','b','c'], categories=['a','b','c']) cat.reorder_categories(['c','b','a','d']) # after cat = pd.Categorical(['a','b','c'], categories=['a','b','c']) cat.reorder_categories(['c','b','a'])
Defensive patterns
Strategy: validation
Validate before calling
def safe_reorder(cat, new_categories):
if set(new_categories) != set(cat.categories) or len(new_categories) != len(cat.categories):
raise ValueError("new_categories must be a permutation of current categories")
return cat.reorder_categories(new_categories) Type guard
def is_permutation_of_categories(cat, new_categories) -> bool:
return set(new_categories) == set(cat.categories) and len(new_categories) == len(cat.categories) Try / catch
try:
cat = cat.reorder_categories(new_cats)
except ValueError as e:
if 'not the same as in old categories' in str(e):
cat = cat.set_categories(new_cats)
else:
raise Prevention
- Use reorder_categories only for permutations; use set_categories otherwise.
- Derive the new order from cat.categories to avoid drift.
- Check set equality before reordering.
When it happens
Trigger: `cat.reorder_categories(['b','a','d'])` where 'd' is not an existing category, or where one is missing, or where extra items are included.
Common situations: Confusing reorder with rename or set_categories; passing a reordered list that was built from a superset (e.g. all unique values rather than current categories).
Related errors
- Lengths must match.
- Cannot convert float NaN to integer
- Cannot cast {self.categories.dtype} dtype to {dtype}
- The categories must be provided in 'categories' or 'dtype'.
- new categories need to have the same number of items as the
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/5f84a4b62f5cebc3.
Report an issue: GitHub.