pola-rs/polars · error · ValueError

Pandas dataframe contains non-unique indices and/or column…

Error message

Pandas dataframe contains non-unique indices and/or column names. Polars dataframes require unique string names for columns.

What it means

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.

Solutions

  1. Deduplicate pandas columns first, e.g. df.columns = make_unique(df.columns) or df = df.loc[:, ~df.columns.duplicated()]
  2. Reset or deduplicate the index: df = df.reset_index(drop=True)
  3. Drop the pandas index entirely via pl.from_pandas(df, include_index=False)

Example fix

// before
pl.from_pandas(pd.concat([df1, df2], axis=1))  # duplicate 'a' columns
// after
merged = pd.concat([df1, df2], axis=1)
merged.columns = [f'{c}_{i}' for i, c in enumerate(merged.columns)]
pl.from_pandas(merged)
Defensive patterns

Strategy: validation

Validate before calling

assert df.columns.duplicated().sum() == 0, 'duplicate pandas column names'
assert not df.index.duplicated().any(), 'duplicate pandas index values'

Try / catch

try:
    df_pl = pl.from_pandas(df)
except ValueError as e:
    if 'non-unique indices' in str(e):
        df_pl = pl.from_pandas(df.loc[:, ~df.columns.duplicated()].reset_index(drop=True))
    else:
        raise

Prevention

When it happens

Trigger: 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).

Common situations: Joins/concatenation in pandas creating duplicate column labels, MultiIndex columns flattened to duplicates, frames assembled from multiple sources sharing column names.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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

Appendix: source

Thrown at py-polars/src/polars/_utils/construction/dataframe.py:1086


def _check_pandas_columns(data: pd.DataFrame, *, include_index: bool) -> None:
    """Check pandas dataframe columns can be converted to polars."""
    stringified_cols: set[str] = {str(col) for col in data.columns}
    stringified_index: set[str] = (
        {str(idx) for idx in data.index.names} if include_index else set()
    )

    non_unique_cols: bool = len(stringified_cols) < len(data.columns)
    non_unique_indices: bool = (
        (len(stringified_index) < len(data.index.names)) if include_index else False
    )
    if non_unique_cols or non_unique_indices:
        msg = (
            "Pandas dataframe contains non-unique indices and/or column names. "
            "Polars dataframes require unique string names for columns."
        )
        raise ValueError(msg)

    overlapping_cols_and_indices: set[str] = stringified_cols & stringified_index
    if len(overlapping_cols_and_indices) > 0:
        msg = "Pandas indices and column names must not overlap."
        raise ValueError(msg)


def pandas_to_pydf(
    data: pd.DataFrame,
    schema: SchemaDefinition | None = None,
    *,
    schema_overrides: SchemaDict | None = None,
    strict: bool = True,
    rechunk: bool = True,
    nan_to_null: bool = True,
    include_index: bool = False,
) -> PyDataFrame:
    """Construct a PyDataFrame from a pandas DataFrame."""

View on GitHub (pinned to fe841f959e)