pola-rs/polars · error
Pandas indices and column names must not overlap.
Error message
Pandas indices and column names must not overlap.
What it means
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.
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
Example fix
// before _df.index.name = 'id' # collides with column 'id' pl.from_pandas(_df) // after _df.index.name = None pl.from_pandas(_df)
Defensive patterns
Strategy: validation
Validate before calling
if df.index.name is not None:
assert df.index.name not in df.columns, f'index name {df.index.name!r} collides with a column' Try / catch
try:
df_pl = pl.from_pandas(df)
except ValueError as e:
if 'must not overlap' in str(e):
df.index.name = None
df_pl = pl.from_pandas(df)
else:
raise Prevention
- 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
When it happens
Trigger: pl.from_pandas(df) where df.index.name == some df column name (e.g. both 'id'), with include_index/index conversion enabled.
Common situations: 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.
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
- Pandas dataframe contains non-unique indices and/or column…
- conversion of polars data type
- dimensions of columns arg must match data dimensions
- expected pandas DataFrame or Series, got
- pyarrow is required for converting a pandas dataframe to…
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/fdbf0532a36144d9.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/_utils/construction/dataframe.py:1091
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."""
_check_pandas_columns(data, include_index=include_index)
convert_index = include_index and not _pandas_has_default_index(data)
if not convert_index:View on GitHub (pinned to fe841f959e)