{"record":{"id":"a6f062cbadd41d27","repo":"pola-rs/polars","slug":"invalid-dtype-format-value-fmt-r-expected-form","errorCode":null,"errorMessage":"invalid dtype_format value: {fmt!r} (expected format string, got {qualified_type_name(fmt)!r})","messagePattern":"invalid dtype_format value: (.+?) \\(expected format string, got (.+?)\\)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/io/spreadsheet/_write_utils.py","lineNumber":457,"sourceCode":"    column_formulas = {\n        col: {\"formula\": options} if isinstance(options, str) else options\n        for col, options in (formulas or {}).items()\n    }\n\n    # normalise formats\n    column_formats = dict(column_formats or {})\n    dtype_formats = dict(dtype_formats or {})\n\n    for tp in list(dtype_formats):\n        if isinstance(tp, (tuple, frozenset)):\n            updates: dict[OneOrMoreDataTypes, str] = dict.fromkeys(\n                tp, dtype_formats.pop(tp)\n            )\n            dtype_formats.update(updates)\n    for fmt in dtype_formats.values():\n        if not isinstance(fmt, str):\n            msg = f\"invalid dtype_format value: {fmt!r} (expected format string, got {qualified_type_name(fmt)!r})\"\n            raise TypeError(msg)\n\n    # inject sparkline/row-total placeholder(s)\n    if sparklines:\n        df = _xl_inject_dummy_table_columns(df, sparklines)\n    if column_formulas:\n        df = _xl_inject_dummy_table_columns(df, column_formulas)\n    if row_totals:\n        df = _xl_inject_dummy_table_columns(df, row_total_funcs, dtype=row_totals_dtype)\n\n    # seed format cache with default fallback format\n    fmt_default = format_cache.get({\"valign\": \"vcenter\"})\n\n    if table_style is None:\n        # no table style; apply default black (+ve) & red (-ve) numeric formatting\n        int_base_fmt = _XL_DEFAULT_INTEGER_FORMAT_\n        flt_base_fmt = _XL_DEFAULT_FLOAT_FORMAT_\n    else:\n        # if we have a table style, defer the colours to that style","sourceCodeStart":439,"sourceCodeEnd":475,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/io/spreadsheet/_write_utils.py#L439-L475","documentation":"The dtype_format mapping passed to write_excel must map dtypes to xlsx number-format strings. This TypeError fires when a value in that mapping is not a str (e.g. a number, list, or format object), preventing an invalid format from reaching the xlsx writer.","triggerScenarios":"write_excel(dtype_format={pl.Int64: 0.00}) or passing a non-string such as a numeric format code or a Format object as the value of dtype_format.","commonSituations":"Copy-pasting Excel format codes as numbers, confusing dtype_format values with column_formats objects, or building the mapping programmatically where a value ends up numeric.","solutions":["Ensure every dtype_format value is a string like '0.00', 'yyyy-mm-dd', '0.0%'.","Wrap numeric format codes in quotes when writing the mapping.","Audit programmatic construction of the mapping (e.g. print types of values) before the call."],"exampleFix":"// before\ndf.write_excel('out.xlsx', dtype_format={pl.Float64: 0.00})\n// after\ndf.write_excel('out.xlsx', dtype_format={pl.Float64: '0.00'})","handlingStrategy":"validation","validationCode":"bad = {k: v for k, v in dtype_format.items() if not isinstance(v, str)}\nassert not bad, f'non-string format values: {bad}'","typeGuard":"def is_str_format_map(m: dict) -> bool:\n    return all(isinstance(v, str) for v in m.values())","tryCatchPattern":null,"preventionTips":["Quote all xlsx number-format codes as strings","Type-annotate the mapping as dict[pl.DataType, str]"],"tags":["excel","polars","type-mismatch","dtype-format"],"backgroundTag":"type-mismatch","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"}