dotnet/machinelearning · error · ArgumentException
string.Format(Strings.DuplicateColumnName, column.Name)
Error message
string.Format(Strings.DuplicateColumnName, column.Name)
What it means
Thrown when a column being inserted into a DataFrameColumnCollection has a name that already exists in the DataFrame. The library enforces unique column names because columns are looked up by name via an internal name-to-index dictionary. It is an ArgumentException naming the offending 'column' parameter.
Source
Thrown at src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs:87
protected override void InsertItem(int columnIndex, DataFrameColumn column)
{
column = column ?? throw new ArgumentNullException(nameof(column));
if (Count == 0)
{
//change RowCount on inserting first row to dataframe
RowCount = column.Length;
}
else if (column.Length != RowCount)
{
//check all columns in the dataframe have the same length (amount of rows)
throw new ArgumentException(Strings.MismatchedColumnLengths, nameof(column));
}
if (_columnNameToIndexDictionary.ContainsKey(column.Name))
{
throw new ArgumentException(string.Format(Strings.DuplicateColumnName, column.Name), nameof(column));
}
column.AddOwner(this);
RowCount = column.Length;
_columnNameToIndexDictionary[column.Name] = columnIndex;
for (int i = columnIndex; i < Count; i++)
{
_columnNameToIndexDictionary[this[i].Name]++;
}
base.InsertItem(columnIndex, column);
ColumnsChanged?.Invoke();
}
protected override void SetItem(int columnIndex, DataFrameColumn column)
{
column = column ?? throw new ArgumentNullException(nameof(column));View on GitHub (pinned to 7b76e69cf9)
Solutions
- Rename the new column to a unique name before inserting (set column.Name or create the column with a distinct name).
- Check existence first with collection.Contains(name) or TryGetColumnIndex and skip/rename/remove the existing column.
- Use DataFrame.AddColumn only when the name is guaranteed unique; dedupe source headers on load (e.g. suffix duplicates).
Example fix
// before
df.Columns.Add(new StringDataFrameColumn("Name", df.Rows.Count)); // 'Name' already exists
// after
if (!df.Columns.Contains("Name"))
df.Columns.Add(new StringDataFrameColumn("Name", df.Rows.Count));
else
df.Columns.Add(new StringDataFrameColumn("Name_2", df.Rows.Count)); Defensive patterns
Strategy: validation
Validate before calling
if (df.Columns.Contains(newColumn.Name))
throw new InvalidOperationException($"Column '{newColumn.Name}' already exists");
df.Columns.Add(newColumn); Type guard
bool CanAdd(DataFrameColumn c, DataFrame df) => c != null && !df.Columns.Contains(c.Name);
Try / catch
try
{
df.Columns.Add(column);
}
catch (ArgumentException ex) when (ex.Message.Contains("already exists"))
{
// rename or skip
} Prevention
- Always check Contains(name) before Add/Insert.
- Rename cloned or joined columns with a suffix before adding.
- Deduplicate headers when loading CSV/parquet sources.
When it happens
Trigger: Calling Insert, Add, or any API that internally inserts a second column whose DataFrameColumn.Name matches an existing column (the check is _columnNameToIndexDictionary.ContainsKey(column.Name)).
Common situations: Renaming a column to a name that already exists before inserting, loading two files/tables with duplicate headers and appending both, cloning a column and adding it back without renaming, joining datasets with overlapping column names.
Related errors
- Strings.DuplicateColumnName (formatted with column.Name)
- Value name '{0}' matches an existing column name
- {0} and {1} must be different
- Strings.InvalidColumnName (formatted with columnName)
- Strings.BadColumnCast (formatted with column.DataType, typeo
AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11).
Data as JSON: /api/errors/8eb6913e3dd7233e.
Report an issue: GitHub.