pola-rs/polars · error

Map requires a key and a value type, e.g…

Error message

Map requires a key and a value type, e.g. `pl.Map(pl.String, pl.Int64)`

What it means

The Map dtype in Polars is parameterized by key and value types (e.g. pl.Map(pl.String, pl.Int64)). Passing the bare pl.Map class as a dtype gives no key/value types, so `sequence_to_pyseries` raises TypeError explaining the required form.

Solutions

  1. Instantiate the dtype: pl.Map(key_type, value_type), e.g. pl.Map(pl.String, pl.Int64).
  2. If the data is just dicts, consider letting it infer as Struct instead of Map.
  3. Check dtype declarations in schema dicts/typing helpers for bare pl.Map usages.

Example fix

// before
pl.Series([{'a': 1}], dtype=pl.Map)
// after
pl.Series([{'a': 1}], dtype=pl.Map(pl.String, pl.Int64))
Defensive patterns

Strategy: type-guard

Validate before calling

if dtype == pl.Map:
    raise TypeError('pl.Map must be instantiated: pl.Map(key_dtype, value_dtype)')

Type guard

def is_instantiated_map(dtype) -> bool:
    import polars as pl
    return isinstance(dtype, pl.DataType) and type(dtype) is pl.Map

Try / catch

try:
    s = pl.Series(values, dtype=dtype)
except TypeError as e:
    if 'Map requires a key and a value type' in str(e):
        s = pl.Series(values, dtype=pl.Map(pl.String, pl.Int64))
    else:
        raise

Prevention

When it happens

Trigger: pl.Series(values, dtype=pl.Map) or pl.DataFrame(..., schema={'m': pl.Map}) — using Map without instantiating key/value types.

Common situations: Copy-pasting dtype annotations; declaring Map columns in schemas without arguments; migrating code from dict-inference to explicit Map typing.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/a19d4d3b6543f2e4. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/_utils/construction/series.py:203

            elif pyseries.dtype().is_integer() or pyseries.dtype() == Null:
                pyseries = pyseries.cast(
                    Decimal(scale=0), strict=strict, wrap_numerical=False
                )
            elif not isinstance(pyseries.dtype(), Decimal):
                msg = f"can't convert {pyseries.dtype()} to Decimal"
                raise TypeError(msg)

        return pyseries

    elif isinstance(dtype, Map):
        # A dict is otherwise inferred as a Struct, so the Map dtype has to drive this.
        return PySeries.new_from_any_values_and_dtype(
            name, values, dtype, strict=strict
        )

    elif dtype == Map:
        msg = "Map requires a key and a value type, e.g. `pl.Map(pl.String, pl.Int64)`"
        raise TypeError(msg)

    elif dtype == Struct:
        # This is very bad. Goes via rows? And needs to do outer nullability separate.
        # It also has two data passes.
        # TODO: eventually go into struct builder
        struct_schema = dtype.to_schema() if isinstance(dtype, Struct) else None
        empty = {}  # type: ignore[var-annotated]

        data = []
        invalid = []
        for i, v in enumerate(values):
            if v is None:
                invalid.append(i)
                data.append(empty)
            else:
                data.append(v)

        return plc.sequence_to_pydf(

View on GitHub (pinned to fe841f959e)