dotnet/machinelearning · error · ArgumentException
Strings.DuplicateColumnName (formatted with column.Name)
Error message
Strings.DuplicateColumnName (formatted with column.Name)
What it means
When replacing a column, the new name must not collide with any other existing column. If the name is found in the name-to-index dictionary at a different index, ArgumentException(Strings.DuplicateColumnName, column.Name) is thrown. Replacing in place with the same name is allowed.
Source
Thrown at src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs:113
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));
if (RowCount > 0 && column.Length != RowCount)
{
throw new ArgumentException(Strings.MismatchedColumnLengths, nameof(column));
}
bool existingColumn = _columnNameToIndexDictionary.TryGetValue(column.Name, out int existingColumnIndex);
if (existingColumn && existingColumnIndex != columnIndex)
{
throw new ArgumentException(string.Format(Strings.DuplicateColumnName, column.Name), nameof(column));
}
_columnNameToIndexDictionary.Remove(this[columnIndex].Name);
_columnNameToIndexDictionary[column.Name] = columnIndex;
this[columnIndex].RemoveOwner(this);
base.SetItem(columnIndex, column);
ColumnsChanged?.Invoke();
}
protected override void RemoveItem(int columnIndex)
{
_columnNameToIndexDictionary.Remove(this[columnIndex].Name);
for (int i = columnIndex + 1; i < Count; i++)
{
_columnNameToIndexDictionary[this[i].Name]--;
}View on GitHub (pinned to 7b76e69cf9)
Solutions
- Choose a unique replacement name before assigning.
- Remove the conflicting column first, then set the new one.
- Reuse the existing column's name (same-name in-place replacement) if a rename is not intended.
Example fix
// before
df.Columns[0] = new Int32DataFrameColumn("B", df.Rows.Count); // 'B' is column 1
// after
df.Columns[0] = new Int32DataFrameColumn("A_unique", df.Rows.Count); Defensive patterns
Strategy: validation
Validate before calling
var existingIdx = df.Columns.TryGetColumnIndex(newColumn.Name, out int idx);
if (existingIdx && idx != index)
throw new InvalidOperationException($"Name '{newColumn.Name}' belongs to column {idx}");
df.Columns[index] = newColumn; Type guard
bool CanReplaceAt(DataFrameColumn c, int index, DataFrame df) =>
!df.Columns.TryGetColumnIndex(c.Name, out int i) || i == index; Try / catch
try
{
df.Columns[index] = column;
}
catch (ArgumentException ex) when (ex.Message.Contains("already exists") || ex.Message.Contains("Duplicate"))
{
// pick a unique name and retry
} Prevention
- Reuse the existing column's name when replacing in place; only new names must be unique.
- Never rename-via-replacement to a name held by a sibling column.
- Track column names centrally in pipelines that swap columns programmatically.
When it happens
Trigger: df.Columns[i] = column where column.Name matches another column j != i in the collection.
Common situations: Renaming a column via replacement to a name already used by a sibling column, bulk column-swap code that reuses names, template columns copied without renaming.
Related errors
- string.Format(Strings.DuplicateColumnName, 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/08d6c739022cc26a.
Report an issue: GitHub.