pola-rs/polars · error · TypeError
invalid dtype_format value
Error message
invalid dtype_format value: {fmt!r} (expected format string, got {qualified_type_name(fmt)!r}) What it means
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.
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.
Example fix
// before
df.write_excel('out.xlsx', dtype_format={pl.Float64: 0.00})
// after
df.write_excel('out.xlsx', dtype_format={pl.Float64: '0.00'}) Defensive patterns
Strategy: validation
Validate before calling
bad = {k: v for k, v in dtype_format.items() if not isinstance(v, str)}
assert not bad, f'non-string format values: {bad}' Type guard
def is_str_format_map(m: dict) -> bool:
return all(isinstance(v, str) for v in m.values()) Prevention
- Quote all xlsx number-format codes as strings
- Type-annotate the mapping as dict[pl.DataType, str]
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- `arctan2` expected a `str` or `Expr` got a
- `arctan2` expected a `str` or `Expr` got a
- can only convert date/datetime to NaiveDateTime
- can only convert date/datetime to NaiveTime
- can't convert to Decimal
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/a6f062cbadd41d27.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/io/spreadsheet/_write_utils.py:457
column_formulas = {
col: {"formula": options} if isinstance(options, str) else options
for col, options in (formulas or {}).items()
}
# normalise formats
column_formats = dict(column_formats or {})
dtype_formats = dict(dtype_formats or {})
for tp in list(dtype_formats):
if isinstance(tp, (tuple, frozenset)):
updates: dict[OneOrMoreDataTypes, str] = dict.fromkeys(
tp, dtype_formats.pop(tp)
)
dtype_formats.update(updates)
for fmt in dtype_formats.values():
if not isinstance(fmt, str):
msg = f"invalid dtype_format value: {fmt!r} (expected format string, got {qualified_type_name(fmt)!r})"
raise TypeError(msg)
# inject sparkline/row-total placeholder(s)
if sparklines:
df = _xl_inject_dummy_table_columns(df, sparklines)
if column_formulas:
df = _xl_inject_dummy_table_columns(df, column_formulas)
if row_totals:
df = _xl_inject_dummy_table_columns(df, row_total_funcs, dtype=row_totals_dtype)
# seed format cache with default fallback format
fmt_default = format_cache.get({"valign": "vcenter"})
if table_style is None:
# no table style; apply default black (+ve) & red (-ve) numeric formatting
int_base_fmt = _XL_DEFAULT_INTEGER_FORMAT_
flt_base_fmt = _XL_DEFAULT_FLOAT_FORMAT_
else:
# if we have a table style, defer the colours to that styleView on GitHub (pinned to fe841f959e)