{"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/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/categorical.py#L778-L814","documentation":"Raised by `Categorical.from_codes` when neither the `categories` argument nor a `CategoricalDtype` with non-null `.categories` was supplied. `from_codes` takes raw integer codes, so it has no way to infer category labels from the values themselves — the caller must tell it what each code means.","triggerScenarios":"Calling `pd.Categorical.from_codes([0, 1, 0])` with no `categories=` and no `dtype=`, or passing a `CategoricalDtype(categories=None)`.","commonSituations":"Refactoring code that previously built a Categorical via the normal constructor (which can infer categories); deserialization pipelines that read codes from a binary blob but forgot to also read the label table; copy-paste errors omitting the keyword.","solutions":["Pass an explicit `categories` list whose length exceeds the max code: `pd.Categorical.from_codes([0, 1, 0], categories=['a', 'b'])`.","Pass a fully-formed `CategoricalDtype`: `from_codes(codes, dtype=dtype)`.","Validate that the loaded dtype has non-null `.categories` before calling `from_codes`.","If you have raw values (not codes), use `pd.Categorical(values)` instead."],"exampleFix":"# before\nimport pandas as pd\ncat = pd.Categorical.from_codes([0, 1, 0, 1])  # ValueError\n\n# after\ncat = pd.Categorical.from_codes([0, 1, 0, 1], categories=['a', 'b'])","handlingStrategy":"validation","validationCode":"def from_codes_safe(codes, categories=None, dtype=None):\n    if categories is None and (dtype is None or dtype.categories is None):\n        raise ValueError('supply categories= or a CategoricalDtype with categories')\n    return pd.Categorical.from_codes(codes, categories=categories, dtype=dtype)","typeGuard":"def has_categories(categories, dtype) -> bool:\n    return categories is not None or (dtype is not None and dtype.categories is not None)","tryCatchPattern":null,"preventionTips":["Always pass `categories=` to `from_codes` — there is no inference path.","Assert `dtype.categories is not None` when loading a dtype from disk before calling `from_codes`.","If you have value labels rather than codes, use `pd.Categorical(values)`."],"tags":["categorical","from-codes","categories","valueerror"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}