{"record":{"id":"5f84a4b62f5cebc3","repo":"pandas-dev/pandas","slug":"items-in-new-categories-are-not-the-same-as-in-old","errorCode":null,"errorMessage":"items in new_categories are not the same as in old categories","messagePattern":"items in new_categories are not the same as in old categories","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/categorical.py","lineNumber":1362,"sourceCode":"        3   a\n        dtype: category\n        Categories (3, str): ['c' < 'b' < 'a']\n\n        For :class:`pandas.CategoricalIndex`:\n\n        >>> ci = pd.CategoricalIndex([\"a\", \"b\", \"c\", \"a\"])\n        >>> ci\n        CategoricalIndex(['a', 'b', 'c', 'a'], categories=['a', 'b', 'c'],\n                         ordered=False, dtype='category')\n        >>> ci.reorder_categories([\"c\", \"b\", \"a\"], ordered=True)\n        CategoricalIndex(['a', 'b', 'c', 'a'], categories=['c', 'b', 'a'],\n                         ordered=True, dtype='category')\n        \"\"\"\n        if (\n            len(self.categories) != len(new_categories)\n            or not self.categories.difference(new_categories).empty\n        ):\n            raise ValueError(\n                \"items in new_categories are not the same as in old categories\"\n            )\n        return self.set_categories(new_categories, ordered=ordered)\n\n    def add_categories(self, new_categories) -> Self:\n        \"\"\"\n        Add new categories.\n\n        `new_categories` will be included at the last/highest place in the\n        categories and will be unused directly after this call.\n\n        Parameters\n        ----------\n        new_categories : category or list-like of category\n            The new categories to be included.\n\n        Returns\n        -------","sourceCodeStart":1344,"sourceCodeEnd":1380,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/categorical.py#L1344-L1380","documentation":"Raised by `reorder_categories` when `new_categories` either has a different length than the current categories or contains values not present in the current categories. `reorder_categories` is strictly a permutation — the set of labels must be identical, only their order may change.","triggerScenarios":"`cat.reorder_categories(['a', 'b', 'd'])` on a Categorical whose categories are `['a', 'b', 'c']`, or any call that adds/drops a label rather than reordering.","commonSituations":"Confusing reorder with set/add/remove; datasets whose categories shifted between runs; calling reorder with a list built from a different column.","solutions":["Use `set_categories(new_list, ordered=...)` when the membership legitimately changes.","Pass the same set of categories in the desired order: build `new_categories` as a permutation of `list(cat.categories)`.","Sort the current categories to derive the new order safely: `cat.reorder_categories(sorted(cat.categories), ordered=True)`.","Validate `set(new_categories) == set(cat.categories)` and `len` equality before calling."],"exampleFix":"# before\ncat = pd.Categorical(['a', 'b', 'c'], categories=['a', 'b', 'c'])\ncat = cat.reorder_categories(['a', 'b', 'd'])  # ValueError\n\n# after (true reorder)\ncat = cat.reorder_categories(['c', 'b', 'a'], ordered=True)\n# after (intentionally changing membership)\ncat = cat.set_categories(['a', 'b', 'd'])","handlingStrategy":"validation","validationCode":"def safe_reorder(cat, new_categories, ordered=None):\n    new_set = set(new_categories)\n    if new_set != set(cat.categories) or len(new_categories) != len(cat.categories):\n        return cat.set_categories(new_categories, ordered=ordered)\n    return cat.reorder_categories(new_categories, ordered=ordered)","typeGuard":"def is_permutation_of_categories(cat, new_categories) -> bool:\n    new = list(new_categories)\n    return len(new) == len(cat.categories) and set(new) == set(cat.categories)","tryCatchPattern":"try:\n    cat = cat.reorder_categories(new_list)\nexcept ValueError as e:\n    if 'not the same' in str(e):\n        cat = cat.set_categories(new_list)\n    else:\n        raise","preventionTips":["Pre-check `set(new) == set(cat.categories)` and matching length before `reorder_categories`.","Use `set_categories` when membership legitimately changes.","Derive the new order from `sorted(cat.categories)` or a known permutation to avoid accidental membership drift."],"tags":["categorical","reorder-categories","categories","valueerror"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}