{"record":{"id":"f601cc87739a3caa","repo":"pola-rs/polars","slug":"cannot-parse-python-data-type-dtype-r-into-arrow-data-type","errorCode":null,"errorMessage":"cannot parse Python data type {dtype!r} into Arrow data type","messagePattern":"cannot parse Python data type (.+?) into Arrow data type","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/datatypes/convert.py","lineNumber":288,"sourceCode":"\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:\n        return None\n\n    dtype_base, subtype = m.groups()\n    dtype = DataTypeMappings.REPR_TO_DTYPE.get(dtype_base)\n    if dtype and subtype:\n        # TODO: further-improve handling for nested types (such as List,Struct)\n        try:\n            if dtype == Decimal:\n                subtype = (None, int(subtype))","sourceCodeStart":270,"sourceCodeEnd":306,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/datatypes/convert.py#L270-L306","documentation":"py_type_to_arrow_type maps a Python builtin type to a pyarrow DataType via PY_TYPE_TO_ARROW_TYPE; unknown types raise ValueError. It is the Python-type -> Arrow direction of the datatype bridge used when exporting/annotating Arrow schemas.","triggerScenarios":"Calling py_type_to_arrow_type with a type not in the mapping, e.g. bytes on versions lacking a binary entry, Decimal, NoneType, or custom/user classes; passing a polars dtype where a Python type was expected.","commonSituations":"Building Arrow schemas from user-supplied Python type annotations (pydantic/dataclass fields with exotic types); version skew where the mapping table lacks a newer type.","solutions":["Map the type to a supported one first (int, float, str, bool, bytes, datetime.date, datetime.datetime, datetime.timedelta).","Use pyarrow directly: pa.array(values) infers the Arrow type for arbitrary types.","Upgrade polars to get a broader PY_TYPE_TO_ARROW_TYPE table."],"exampleFix":"// before\npy_type_to_arrow_type(mycustomlib.Foo)\n// after\npy_type_to_arrow_type(str)  # or let pa.array(values) infer","handlingStrategy":"type-guard","validationCode":"ARROW_MAPPABLE = {int, float, str, bool, bytes, list, dict}\ndef arrow_mappable(t: type) -> bool:\n    return t in ARROW_MAPPABLE or getattr(t, '__module__', '').startswith('datetime')","typeGuard":"def is_arrow_mappable_type(t: object) -> TypeGuard[type[int | float | str | bool | bytes]]:\n    return isinstance(t, type) and t in {int, float, str, bool, bytes}","tryCatchPattern":"try:\n    arrow_type = py_type_to_arrow_type(py_t)\nexcept ValueError:\n    arrow_type = pa.array([], dtype=object).type  # let pyarrow infer instead","preventionTips":["Restrict schema annotations to Python types polars maps to Arrow","Let pyarrow infer types from sample values for exotic types","Normalize user-supplied type hints at the config boundary"],"tags":["python","polars","arrow","pyarrow"],"backgroundTag":"unsupported-operation","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"}