pola-rs/polars · error
duplicate column names found
Error message
duplicate column names found: {series.columns.tolist()!s} What it means
`is_simple_numpy_backed_pandas_series` detects whether a pandas object can go to Polars without pyarrow. If the object's shape has more than one dimension it is actually a DataFrame (pandas returns a DataFrame when selecting a duplicated column label), so ValueError is raised listing the duplicated column names.
Solutions
- De-duplicate columns first: df = df.loc[:, ~df.columns.duplicated()].
- Pick one of the duplicates explicitly: df['dup'].iloc[:, 0].
- Rename duplicates before conversion with df.rename().
Example fix
// before pl.Series(df['a']) # 'a' duplicated -> df['a'] is a DataFrame // after df = df.loc[:, ~df.columns.duplicated()] pl.Series(df['a'])
Defensive patterns
Strategy: validation
Validate before calling
if isinstance(obj, pd.DataFrame) or getattr(obj, 'ndim', 1) > 1:
raise ValueError(f'duplicate column names: {obj.columns.tolist()}') Type guard
def is_1d_series(obj) -> bool:
import pandas as pd
return isinstance(obj, pd.Series) and obj.ndim == 1 Try / catch
try:
s = pl.Series(df['a'])
except ValueError as e:
if 'duplicate column names' in str(e):
df = df.loc[:, ~df.columns.duplicated()]
s = pl.Series(df['a'])
else:
raise Prevention
- Validate pandas frames for duplicate labels at ingestion time.
- Use df.loc[:, ~df.columns.duplicated()] after concat/merge operations.
- Fix duplicate headers in source CSVs before loading.
When it happens
Trigger: pl.Series(df['dup']) or pl.from_pandas(df['dup']) where pandas df has duplicate column labels, so df['dup'] is a DataFrame; the check fires during pandas_to_pyseries/pandas_to_pydf.
Common situations: pandas frames with repeated column labels from concatenation or CSVs with repeated headers; code assuming df['x'] always returns a Series.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- duplicate column names found
- column appears more than once in the query/result cursor
- `df` of type does not support the dataframe interchange…
- expected pandas DataFrame or Series, got
- pandas>=1.5.0 is required for…
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/10f1c5b341e6a456.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/_utils/construction/utils.py:123
def contains_nested(value: Any, is_nested: Callable[[Any], bool]) -> bool:
"""Determine if value contains (or is) nested structured data."""
if is_nested(value):
return True
elif isinstance(value, dict):
return any(contains_nested(v, is_nested) for v in value.values())
elif isinstance(value, (list, tuple)):
return any(contains_nested(v, is_nested) for v in value)
return False
def is_simple_numpy_backed_pandas_series(
series: pd.Series[Any] | pd.Index[Any] | pd.DatetimeIndex,
) -> bool:
if len(series.shape) > 1:
# Pandas Series is actually a Pandas DataFrame when the original DataFrame
# contains duplicated columns and a duplicated column is requested with df["a"].
msg = f"duplicate column names found: {series.columns.tolist()!s}" # type: ignore[union-attr]
raise ValueError(msg)
return (str(series.dtype) in PANDAS_SIMPLE_NUMPY_DTYPES) or (
series.dtype == "object"
and not series.hasnans
and not series.empty
and isinstance(next(iter(series)), str)
)
View on GitHub (pinned to fe841f959e)