{"record":{"id":"6b5ea518977723c1","repo":"pandas-dev/pandas","slug":"the-categories-must-be-provided-in-categories-or","errorCode":null,"errorMessage":"The categories must be provided in 'categories' or 'dtype'. Both were None.","messagePattern":"The categories must be provided in 'categories' or 'dtype'\\. Both were None\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/categorical.py","lineNumber":796,"sourceCode":"        codes : The category codes of the categorical.\n        CategoricalIndex : An Index with an underlying ``Categorical``.\n\n        Examples\n        --------\n        >>> dtype = pd.CategoricalDtype([\"a\", \"b\"], ordered=True)\n        >>> pd.Categorical.from_codes(codes=[0, 1, 0, 1], dtype=dtype)\n        ['a', 'b', 'a', 'b']\n        Categories (2, str): ['a' < 'b']\n        \"\"\"\n        dtype = CategoricalDtype._from_values_or_dtype(\n            categories=categories, ordered=ordered, dtype=dtype\n        )\n        if dtype.categories is None:\n            msg = (\n                \"The categories must be provided in 'categories' or \"\n                \"'dtype'. Both were None.\"\n            )\n            raise ValueError(msg)\n\n        if validate:\n            # beware: non-valid codes may segfault\n            codes = cls._validate_codes_for_dtype(codes, dtype=dtype)\n\n        return cls._simple_new(codes, dtype=dtype)\n\n    # ------------------------------------------------------------------\n    # Categories/Codes/Ordered\n\n    @property\n    def categories(self) -> Index:\n        \"\"\"\n        The categories of this categorical.\n\n        Setting assigns new values to each category (effectively a rename of\n        each individual category).\n","sourceCodeStart":778,"sourceCodeEnd":814,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/categorical.py#L778-L814","documentation":"Raised by Categorical.from_codes when neither `categories` nor a `dtype` (CategoricalDtype) carrying categories is supplied. from_codes constructs a categorical directly from integer codes, so it cannot infer categories from data; the categories must be provided explicitly to give the codes meaning.","triggerScenarios":"`pd.Categorical.from_codes([0,1,0])` with no categories= and no dtype=. The _from_values_or_dtype helper returns a dtype with categories=None and from_codes refuses to proceed.","commonSituations":"Refactoring code that used the regular constructor; copying only the codes array during serialization without persisting the category list.","solutions":["Pass categories: `pd.Categorical.from_codes([0,1,0], categories=['a','b'])`.","Pass a CategoricalDtype: `pd.Categorical.from_codes([0,1,0], dtype=pd.CategoricalDtype(['a','b']))`.","If categories are unknown, use the normal constructor `pd.Categorical(values)` instead of from_codes."],"exampleFix":"# before\npd.Categorical.from_codes([0, 1, 0])\n# after\npd.Categorical.from_codes([0, 1, 0], categories=['a', 'b'])","handlingStrategy":"validation","validationCode":"def from_codes_safe(codes, categories=None, dtype=None):\n    import pandas as pd\n    if categories is None and (dtype is None or getattr(dtype, 'categories', None) is None):\n        raise ValueError(\"supply categories= or a CategoricalDtype\")\n    return pd.Categorical.from_codes(codes, categories=categories, dtype=dtype)","typeGuard":"def has_categories(categories, dtype) -> bool:\n    import pandas as pd\n    return categories is not None or (isinstance(dtype, pd.CategoricalDtype) and dtype.categories is not None)","tryCatchPattern":"try:\n    cat = pd.Categorical.from_codes(codes)\nexcept ValueError as e:\n    if \"categories\" in str(e):\n        cat = pd.Categorical.from_codes(codes, categories=inferred_categories)\n    else:\n        raise","preventionTips":["Always pass categories with from_codes; persist them alongside codes.","Consider the regular pd.Categorical(values) constructor when categories are unknown.","Validate that dtype.categories is not None before calling."],"tags":["categorical","from-codes","missing-categories","valueerror"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}