pandas-dev/pandas · error · TypeError

Cannot set a Categorical with another, without identical cat

Error message

Cannot set a Categorical with another, without identical categories

What it means

Raised by Categorical._validate_listlike when assigning one Categorical into another whose categories (set or ordering) differ, i.e. self.dtype != value.dtype. Categorical assignment requires the two dtype signatures (categories AND ordered flag) to be identical so codes map consistently. If they differ only by permutation pandas can re-encode, but a full dtype mismatch is rejected here.

Source

Thrown at pandas/core/arrays/categorical.py:2465

            result = f"{body}\n{footer}"
        else:
            # In the empty case we use a comma instead of newline to get
            #  a more compact __repr__
            body = "[]"
            result = f"{body}, {footer}"

        return result

    # ------------------------------------------------------------------

    def _validate_listlike(self, value):
        # NB: here we assume scalar-like tuples have already been excluded
        value = extract_array(value, extract_numpy=True)

        # require identical categories set
        if isinstance(value, Categorical):
            if self.dtype != value.dtype:
                raise TypeError(
                    "Cannot set a Categorical with another, "
                    "without identical categories"
                )
            # dtype equality implies categories_match_up_to_permutation
            value = self._encode_with_my_categories(value)
            return value._codes

        from pandas import Index

        # tupleize_cols=False for e.g. test_fillna_iterable_category GH#41914
        to_add = Index._with_infer(value, tupleize_cols=False, copy=False).difference(
            self.categories
        )

        # no assignments of values not in categories, but it's always ok to set
        # something to np.nan
        if len(to_add) and not isna(to_add).all():
            raise TypeError(

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Unify categories first: target.cat.set_categories(other.cat.categories, ordered=other.cat.ordered) on one side.
  2. Use union_categoricals([a, b]) to build a shared category set before assignment.
  3. Cast both sides to a common non-categorical dtype (e.g. .astype(str)) if category alignment is not needed.

Example fix

// before
s = pd.Series(pd.Categorical(['a','b'], categories=['a','b']))
s[:] = pd.Categorical(['x','y'], categories=['x','y'])  # TypeError

// after
s = s.cat.set_categories(['x','y','a','b'])
s[:] = pd.Categorical(['x','y'], categories=['x','y','a','b'])
Defensive patterns

Strategy: validation

Validate before calling

def compatible_assign(target, value):
    import pandas as pd
    if isinstance(value.dtype, pd.CategoricalDtype) and target.dtype != value.dtype:
        return target.cat.set_categories(value.cat.categories, ordered=value.cat.ordered)
    return target

Type guard

import pandas as pd
from typing import Any

def categories_match(a: Any, b: Any) -> bool:
    da, db = getattr(a, 'dtype', None), getattr(b, 'dtype', None)
    if not (isinstance(da, pd.CategoricalDtype) and isinstance(db, pd.CategoricalDtype)):
        return True
    return da == db

Try / catch

try:
    target[:] = value
except TypeError as e:
    if 'without identical categories' in str(e):
        target = target.cat.set_categories(value.cat.categories, ordered=value.cat.ordered)
        target[:] = value
    else:
        raise

Prevention

When it happens

Trigger: df['a'] = other_cat where df['a'] is a category column and other_cat has different categories; setitem on a categorical Series/Index with a Categorical whose categories differ; fillna with a Categorical of a different category set.

Common situations: Joining or assigning between two DataFrames whose 'category' columns were built independently (different categories inferred from different data slices), or after refiltering where unseen categories were dropped. Also common after pd.concat then reassigning slices.

Related errors


AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07). Data as JSON: /api/errors/32e1401cb2e9e03c. Report an issue: GitHub.