{"record":{"id":"2ab425809af5374a","repo":"pola-rs/polars","slug":"invalid-key-type-type-key-expected-str-or-int","errorCode":null,"errorMessage":"invalid key type {type(key)}; expected str or int","messagePattern":"invalid key type (.+?); expected str or int","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/datatypes/classes.py","lineNumber":851,"sourceCode":"            raise RuntimeError(msg)\n\n    def is_global(self) -> bool:\n        \"\"\"Returns whether this refers to the global categories.\"\"\"\n        return self._categories.is_global()\n\n    def __getitem__(self, key: str | int | None) -> str | int | None:\n        # TODO: In 2.0, this should raise KeyError instead of returning if key is a str.\n        # and an IndexError should be raised if the int key is larger than self.len().\n        # TODO: In 2.0, this should raise TypeError instead of returning if key is None.\n        if key is None:\n            return key\n        elif isinstance(key, str):\n            return self._categories.get_cat(key)\n        elif isinstance(key, int):\n            return self._categories.cat_to_str(key)\n        else:\n            msg = f\"invalid key type {type(key)}; expected str or int\"\n            raise TypeError(msg)\n\n    def __contains__(self, item: str | int) -> bool:\n        if isinstance(item, str):\n            return self._categories.get_cat(item) is not None\n        elif isinstance(item, int):\n            return self._categories.cat_to_str(item) is not None\n        else:\n            return False\n\n    def __iter__(self) -> Iterator[str | None]:\n        for i in range(self._categories.num_cats_upper_bound()):\n            yield self._categories.cat_to_str(i)\n\n    def to_series(self) -> Series:\n        \"\"\"\n        Return a :class:`Series` containing all categories in this `Categories`.\n\n        The categories are ordered by their physical category value.","sourceCodeStart":833,"sourceCodeEnd":869,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/datatypes/classes.py#L833-L869","documentation":"Error \"invalid key type {type(key)}; expected str or int\" thrown in pola-rs/polars.","triggerScenarios":"Thrown at py-polars/src/polars/datatypes/classes.py:851 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}