{"record":{"id":"5077e4f7ebca12eb","repo":"pola-rs/polars","slug":"iterable-passed-to-pl-schema-contained-duplicate-n","errorCode":null,"errorMessage":"iterable passed to pl.Schema contained duplicate name '{name}'","messagePattern":"iterable passed to pl\\.Schema contained duplicate name '(.+?)'","errorType":"exception","errorClass":"DuplicateError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/schema.py","lineNumber":145,"sourceCode":"    ) -> None:\n        if _is_arrow_schema_exportable(schema) and not isinstance(schema, Schema):\n            init_polars_schema_from_arrow_c_schema(self, schema)\n            return\n\n        # `Mapping[tuple[str, SchemaInitDataType]]` is not valid at runtime, even\n        # though it is a `Iterable[tuple[str, SchemaInitDataType]]`.\n        input: Iterable[tuple[str, SchemaInitDataType] | ArrowSchemaExportable]\n        input = schema.items() if isinstance(schema, Mapping) else (schema or ())  # type: ignore[assignment]\n        for v in input:\n            name, tp = (\n                polars_schema_field_from_arrow_c_schema(v)\n                if _is_arrow_schema_exportable(v)\n                else v\n            )\n\n            if name in self:\n                msg = f\"iterable passed to pl.Schema contained duplicate name '{name}'\"\n                raise DuplicateError(msg)\n\n            if not check_dtypes:\n                super().__setitem__(name, tp)  # type: ignore[assignment]\n            elif is_polars_dtype(tp):\n                super().__setitem__(name, _check_dtype(tp))\n            else:\n                self[name] = tp\n\n    def __eq__(self, other: object) -> bool:\n        if not isinstance(other, Mapping):\n            return False\n        if len(self) != len(other):\n            return False\n        for (nm1, tp1), (nm2, tp2) in zip(self.items(), other.items(), strict=True):\n            if nm1 != nm2 or not tp1.is_(tp2):\n                return False\n        return True\n","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/schema.py#L127-L163","documentation":"When pl.Schema is built from an iterable of (name, dtype) pairs (including Arrow field exports), a repeated column name raises polars.exceptions.DuplicateError because an ordered mapping requires unique keys. Mapping input cannot trigger it since a dict is already deduplicated.","triggerScenarios":"pl.Schema([('a', pl.Int8), ('a', pl.Int64)]); zipping names/dtypes lists where names repeat; concatenating schema items from multiple frames into one list.","commonSituations":"Merging schemas from several sources (ETL column alignment); upstream data with duplicated column names; zip of columns and inferred types drifting out of sync.","solutions":["Deduplicate with last-wins semantics before constructing: pl.Schema(dict(items))","Find and fix the upstream duplication: [n for n in set(names) if names.count(n) > 1]","Rename the duplicate if both columns are legitimately distinct"],"exampleFix":"# before\npl.Schema([('a', pl.Int8), ('a', pl.Int64)])  # DuplicateError\n\n# after\npl.Schema(dict([('a', pl.Int8), ('a', pl.Int64)]))  # last wins -> {'a': Int64}","handlingStrategy":"validation","validationCode":"names = [n for n, _ in items]\nif len(set(names)) != len(names):\n    dupes = {n for n in names if names.count(n) > 1}\n    raise ValueError(f'duplicate schema names: {sorted(dupes)}')\nschema = pl.Schema(items)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate name uniqueness when assembling schemas from merged sources","dict(pairs) gives deterministic last-wins behavior if duplicates are expected"],"tags":["polars","schema","duplicates"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}