pandas-dev/pandas · error · TypeError

Cannot set a Categorical with another, without identical…

Error message

Cannot set a Categorical with another, without identical categories

What it means

Raised in Categorical._validate_listlike when assigning a Categorical value into another Categorical whose dtype (categories + ordered flag) differs. dtype equality is required because categorical semantics are tied to the exact category set; a mismatch would silently re-encode values to wrong codes. Only categoricals with identical dtype can be assigned directly.

Solutions

  1. Align categories first: df['col'] = df['col'].cat.set_categories(other.cat.categories) (and match ordered) before assigning.
  2. Use union_categoricals or pd.concat to build a shared dtype, then assign.
  3. Convert the source to plain values (.astype(object).values or .to_numpy()) if category alignment is not desired.
  4. Define a single CategoricalDtype up front and apply it to both sides with astype(shared_dtype).

Example fix

# before
c1 = pd.Categorical(['a','b'], categories=['a','b','c'])
c2 = pd.Categorical(['x'], categories=['x','y'])
c1[0] = c2[0]  # TypeError

# after
shared = pd.CategoricalDtype(['a','b','c','x','y'], ordered=False)
c1 = pd.Categorical(['a','b'], dtype=shared)
c2 = pd.Categorical(['x'], dtype=shared)
c1[0] = c2[0]
Defensive patterns

Strategy: validation

Validate before calling

if isinstance(src, pd.Categorical) and isinstance(dst, pd.Categorical):
    if src.dtype != dst.dtype:
        src = src.astype(dst.dtype)
dst[0:len(src)] = src

Type guard

def categories_compatible(a: pd.Categorical, b: pd.Categorical) -> bool:
    return a.dtype == b.dtype

Try / catch

try:
    target[mask] = source
except TypeError as e:
    if 'identical categories' in str(e):
        target = target.cat.set_categories(source.cat.categories)
        target[mask] = source
    else:
        raise

Prevention

When it happens

Trigger: cat1[0:3] = cat2 where cat1 and cat2 have different categories or different ordered flag. df.loc[i, 'catcol'] = other_cat_series with a different category set. fillna with a Categorical whose categories differ. Setting a slice via .iloc/.loc with a mismatched categorical right-hand side.

Common situations: Two columns were independently astype('category') and end up with category sets that differ by one entry. Merging data from sources where categories were defined separately. Trying to copy values between train/test splits where one side has extra categories.

Related errors


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

Appendix: 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 3b7651241d)