{"record":{"id":"fdbf0532a36144d9","repo":"pola-rs/polars","slug":"pandas-indices-and-column-names-must-not-overlap","errorCode":null,"errorMessage":"Pandas indices and column names must not overlap.","messagePattern":"Pandas indices and column names must not overlap\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/construction/dataframe.py","lineNumber":1091,"sourceCode":"    stringified_index: set[str] = (\n        {str(idx) for idx in data.index.names} if include_index else set()\n    )\n\n    non_unique_cols: bool = len(stringified_cols) < len(data.columns)\n    non_unique_indices: bool = (\n        (len(stringified_index) < len(data.index.names)) if include_index else False\n    )\n    if non_unique_cols or non_unique_indices:\n        msg = (\n            \"Pandas dataframe contains non-unique indices and/or column names. \"\n            \"Polars dataframes require unique string names for columns.\"\n        )\n        raise ValueError(msg)\n\n    overlapping_cols_and_indices: set[str] = stringified_cols & stringified_index\n    if len(overlapping_cols_and_indices) > 0:\n        msg = \"Pandas indices and column names must not overlap.\"\n        raise ValueError(msg)\n\n\ndef pandas_to_pydf(\n    data: pd.DataFrame,\n    schema: SchemaDefinition | None = None,\n    *,\n    schema_overrides: SchemaDict | None = None,\n    strict: bool = True,\n    rechunk: bool = True,\n    nan_to_null: bool = True,\n    include_index: bool = False,\n) -> PyDataFrame:\n    \"\"\"Construct a PyDataFrame from a pandas DataFrame.\"\"\"\n    _check_pandas_columns(data, include_index=include_index)\n\n    convert_index = include_index and not _pandas_has_default_index(data)\n\n    if not convert_index:","sourceCodeStart":1073,"sourceCodeEnd":1109,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_utils/construction/dataframe.py#L1073-L1109","documentation":"Raised by _check_pandas_columns when, after stringifying labels, a pandas DataFrame's index level names overlap its column names. Since polars stores index values as regular columns on conversion, name collisions are ambiguous and rejected with ValueError.","triggerScenarios":"pl.from_pandas(df) where df.index.name == some df column name (e.g. both 'id'), with include_index/index conversion enabled.","commonSituations":"df.reset_index() leaving an index named the same as an existing column before conversion, naming an index to match a key column for readability, automated pipelines copying index names from column names.","solutions":["Rename the pandas index: df.index.name = 'index_id' (or any non-colliding name)","Set df.index.name = None before conversion","Call pl.from_pandas(df, include_index=False) to drop the index"],"exampleFix":"// before\n_df.index.name = 'id'  # collides with column 'id'\npl.from_pandas(_df)\n// after\n_df.index.name = None\npl.from_pandas(_df)","handlingStrategy":"validation","validationCode":"if df.index.name is not None:\n    assert df.index.name not in df.columns, f'index name {df.index.name!r} collides with a column'","typeGuard":null,"tryCatchPattern":"try:\n    df_pl = pl.from_pandas(df)\nexcept ValueError as e:\n    if 'must not overlap' in str(e):\n        df.index.name = None\n        df_pl = pl.from_pandas(df)\n    else:\n        raise","preventionTips":["Set df.index.name = None before conversion unless the index is data","Never name an index after an existing column","Include index names in pre-conversion column-collision checks"],"tags":["python","polars","pandas","conversion","naming"],"backgroundTag":"schema-validation-failed","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"}