{"record":{"id":"291ac54e42b07aab","repo":"pola-rs/polars","slug":"conversion-of-polars-data-type-dtype-r-to-python-type-not","errorCode":null,"errorMessage":"conversion of polars data type {dtype!r} to Python type not implemented","messagePattern":"conversion of polars data type (.+?) to Python type not implemented","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/datatypes/convert.py","lineNumber":279,"sourceCode":"\ndef dtype_to_ffiname(dtype: PolarsDataType) -> str:\n    \"\"\"Return FFI function name associated with the given Polars dtype.\"\"\"\n    try:\n        dtype = dtype.base_type()\n        return DataTypeMappings.DTYPE_TO_FFINAME[dtype]\n    except KeyError:  # pragma: no cover\n        msg = f\"conversion of polars data type {dtype!r} to FFI not implemented\"\n        raise NotImplementedError(msg) from None\n\n\ndef dtype_to_py_type(dtype: PolarsDataType) -> PythonDataType:\n    \"\"\"Convert a Polars dtype to a Python dtype.\"\"\"\n    try:\n        dtype = dtype.base_type()\n        return DataTypeMappings.DTYPE_TO_PY_TYPE[dtype]\n    except KeyError:  # pragma: no cover\n        msg = f\"conversion of polars data type {dtype!r} to Python type not implemented\"\n        raise NotImplementedError(msg) from None\n\n\ndef py_type_to_arrow_type(dtype: PythonDataType) -> pa.DataType:\n    \"\"\"Convert a Python dtype to an Arrow dtype.\"\"\"\n    try:\n        return DataTypeMappings.PY_TYPE_TO_ARROW_TYPE[dtype]\n    except KeyError:  # pragma: no cover\n        msg = f\"cannot parse Python data type {dtype!r} into Arrow data type\"\n        raise ValueError(msg) from None\n\n\ndef dtype_short_repr_to_dtype(dtype_string: str | None) -> PolarsDataType | None:\n    \"\"\"Map a PolarsDataType short repr (eg: 'i64', 'list[str]') back into a dtype.\"\"\"\n    if dtype_string is None:\n        return None\n\n    m = re.match(r\"^(\\w+)(?:\\[(.+)\\])?$\", dtype_string)\n    if m is None:","sourceCodeStart":261,"sourceCodeEnd":297,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/datatypes/convert.py#L261-L297","documentation":"dtype_to_py_type maps a Polars dtype to its corresponding Python type (int, float, str, ...) via DTYPE_TO_PY_TYPE; unmapped dtypes raise NotImplementedError. Called when polars needs to coerce values to Python objects matching the column dtype (to_python, maybe_cast, sequence conversion).","triggerScenarios":"Series.to_python()/to_list() paths on dtypes without a Python analogue (pl.Struct in some paths, pl.Categorical, pl.Enum, pl.Binary on old versions, custom dtypes); strict casting in sequence_to_pyseries.","commonSituations":"Converting a categorical/enum column expecting plain Python types; iterating over nested struct columns through the python-conversion API; custom extension dtypes not registered in the mapping.","solutions":["Cast to a mapped dtype first: s.cast(pl.Utf8) for categorical/enum before to_list().","Use .to_dicts() or .rows() for struct/nested data instead of per-dtype conversion.","Upgrade polars if a recently added dtype lacks the mapping in your version."],"exampleFix":"// before\ns.to_python()  # s.dtype == pl.Enum\n// after\ns.cast(pl.Utf8).to_python()","handlingStrategy":"try-catch","validationCode":"from polars.datatypes.convert import dtype_to_py_type\ndef python_convertible(dtype) -> bool:\n    try:\n        dtype_to_py_type(dtype)\n        return True\n    except NotImplementedError:\n        return False","typeGuard":null,"tryCatchPattern":"try:\n    vals = s.to_python()\nexcept NotImplementedError:\n    vals = s.cast(pl.Utf8).to_python()  # or use to_dicts()/rows() for nested types","preventionTips":["Cast categorical/enum/binary columns to base dtypes before python conversion","Use to_dicts() or rows() for struct and nested columns","Check the DTYPE_TO_PY_TYPE mapping when introducing new dtypes"],"tags":["python","polars","dtype","conversion"],"backgroundTag":"method-not-implemented","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}