{"record":{"id":"eee877f216c02cbd","repo":"pola-rs/polars","slug":"pandas-dataframe-contains-non-unique-indices-and-o","errorCode":null,"errorMessage":"Pandas dataframe contains non-unique indices and/or column names. Polars dataframes require unique string names for columns.","messagePattern":"Pandas dataframe contains non-unique indices and/or column names\\. Polars dataframes require unique string names for columns\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/construction/dataframe.py","lineNumber":1086,"sourceCode":"\n\ndef _check_pandas_columns(data: pd.DataFrame, *, include_index: bool) -> None:\n    \"\"\"Check pandas dataframe columns can be converted to polars.\"\"\"\n    stringified_cols: set[str] = {str(col) for col in data.columns}\n    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.\"\"\"","sourceCodeStart":1068,"sourceCodeEnd":1104,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_utils/construction/dataframe.py#L1068-L1104","documentation":"Raised by _check_pandas_columns when a pandas DataFrame being converted to Polars has duplicate column names and/or duplicate index values. Polars requires unique string column names, so pandas_to_pydf refuses the conversion with ValueError.","triggerScenarios":"pd.concat of overlapping frames producing duplicate column labels, a DataFrame with duplicated index values, then calling pl.from_pandas(df) or pl.DataFrame(df).","commonSituations":"Joins/concatenation in pandas creating duplicate column labels, MultiIndex columns flattened to duplicates, frames assembled from multiple sources sharing column names.","solutions":["Deduplicate pandas columns first, e.g. df.columns = make_unique(df.columns) or df = df.loc[:, ~df.columns.duplicated()]","Reset or deduplicate the index: df = df.reset_index(drop=True)","Drop the pandas index entirely via pl.from_pandas(df, include_index=False)"],"exampleFix":"// before\npl.from_pandas(pd.concat([df1, df2], axis=1))  # duplicate 'a' columns\n// after\nmerged = pd.concat([df1, df2], axis=1)\nmerged.columns = [f'{c}_{i}' for i, c in enumerate(merged.columns)]\npl.from_pandas(merged)","handlingStrategy":"validation","validationCode":"assert df.columns.duplicated().sum() == 0, 'duplicate pandas column names'\nassert not df.index.duplicated().any(), 'duplicate pandas index values'","typeGuard":null,"tryCatchPattern":"try:\n    df_pl = pl.from_pandas(df)\nexcept ValueError as e:\n    if 'non-unique indices' in str(e):\n        df_pl = pl.from_pandas(df.loc[:, ~df.columns.duplicated()].reset_index(drop=True))\n    else:\n        raise","preventionTips":["Deduplicate columns after pd.concat/join before converting","reset_index(drop=True) before conversion when the index is not meaningful","Validate uniqueness with df.columns.is_unique and df.index.is_unique"],"tags":["python","polars","pandas","conversion","uniqueness"],"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"}