{"record":{"id":"a19d4d3b6543f2e4","repo":"pola-rs/polars","slug":"map-requires-a-key-and-a-value-type-e-g-pl-map","errorCode":null,"errorMessage":"Map requires a key and a value type, e.g. `pl.Map(pl.String, pl.Int64)`","messagePattern":"Map requires a key and a value type, e\\.g\\. `pl\\.Map\\(pl\\.String, pl\\.Int64\\)`","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/construction/series.py","lineNumber":203,"sourceCode":"            elif pyseries.dtype().is_integer() or pyseries.dtype() == Null:\n                pyseries = pyseries.cast(\n                    Decimal(scale=0), strict=strict, wrap_numerical=False\n                )\n            elif not isinstance(pyseries.dtype(), Decimal):\n                msg = f\"can't convert {pyseries.dtype()} to Decimal\"\n                raise TypeError(msg)\n\n        return pyseries\n\n    elif isinstance(dtype, Map):\n        # A dict is otherwise inferred as a Struct, so the Map dtype has to drive this.\n        return PySeries.new_from_any_values_and_dtype(\n            name, values, dtype, strict=strict\n        )\n\n    elif dtype == Map:\n        msg = \"Map requires a key and a value type, e.g. `pl.Map(pl.String, pl.Int64)`\"\n        raise TypeError(msg)\n\n    elif dtype == Struct:\n        # This is very bad. Goes via rows? And needs to do outer nullability separate.\n        # It also has two data passes.\n        # TODO: eventually go into struct builder\n        struct_schema = dtype.to_schema() if isinstance(dtype, Struct) else None\n        empty = {}  # type: ignore[var-annotated]\n\n        data = []\n        invalid = []\n        for i, v in enumerate(values):\n            if v is None:\n                invalid.append(i)\n                data.append(empty)\n            else:\n                data.append(v)\n\n        return plc.sequence_to_pydf(","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_utils/construction/series.py#L185-L221","documentation":"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.","triggerScenarios":"pl.Series(values, dtype=pl.Map) or pl.DataFrame(..., schema={'m': pl.Map}) — using Map without instantiating key/value types.","commonSituations":"Copy-pasting dtype annotations; declaring Map columns in schemas without arguments; migrating code from dict-inference to explicit Map typing.","solutions":["Instantiate the dtype: pl.Map(key_type, value_type), e.g. pl.Map(pl.String, pl.Int64).","If the data is just dicts, consider letting it infer as Struct instead of Map.","Check dtype declarations in schema dicts/typing helpers for bare pl.Map usages."],"exampleFix":"// before\npl.Series([{'a': 1}], dtype=pl.Map)\n// after\npl.Series([{'a': 1}], dtype=pl.Map(pl.String, pl.Int64))","handlingStrategy":"type-guard","validationCode":"if dtype == pl.Map:\n    raise TypeError('pl.Map must be instantiated: pl.Map(key_dtype, value_dtype)')","typeGuard":"def is_instantiated_map(dtype) -> bool:\n    import polars as pl\n    return isinstance(dtype, pl.DataType) and type(dtype) is pl.Map","tryCatchPattern":"try:\n    s = pl.Series(values, dtype=dtype)\nexcept TypeError as e:\n    if 'Map requires a key and a value type' in str(e):\n        s = pl.Series(values, dtype=pl.Map(pl.String, pl.Int64))\n    else:\n        raise","preventionTips":["Never use bare pl.Map in schema declarations; always parameterize.","Centralize dtype definitions in one module so Map is instantiated once.","Grep schemas for bare `pl.Map` occurrences in code review."],"tags":["polars","map-dtype","invalid-argument"],"backgroundTag":"invalid-argument-value","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"}