{"record":{"id":"08d6c739022cc26a","repo":"dotnet/machinelearning","slug":"strings-duplicatecolumnname-formatted-with-column","errorCode":null,"errorMessage":"Strings.DuplicateColumnName (formatted with column.Name)","messagePattern":"Strings\\.DuplicateColumnName \\(formatted with column\\.Name\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs","lineNumber":113,"sourceCode":"            for (int i = columnIndex; i < Count; i++)\n            {\n                _columnNameToIndexDictionary[this[i].Name]++;\n            }\n            base.InsertItem(columnIndex, column);\n            ColumnsChanged?.Invoke();\n        }\n\n        protected override void SetItem(int columnIndex, DataFrameColumn column)\n        {\n            column = column ?? throw new ArgumentNullException(nameof(column));\n            if (RowCount > 0 && column.Length != RowCount)\n            {\n                throw new ArgumentException(Strings.MismatchedColumnLengths, nameof(column));\n            }\n            bool existingColumn = _columnNameToIndexDictionary.TryGetValue(column.Name, out int existingColumnIndex);\n            if (existingColumn && existingColumnIndex != columnIndex)\n            {\n                throw new ArgumentException(string.Format(Strings.DuplicateColumnName, column.Name), nameof(column));\n            }\n\n            _columnNameToIndexDictionary.Remove(this[columnIndex].Name);\n            _columnNameToIndexDictionary[column.Name] = columnIndex;\n\n            this[columnIndex].RemoveOwner(this);\n            base.SetItem(columnIndex, column);\n\n            ColumnsChanged?.Invoke();\n        }\n\n        protected override void RemoveItem(int columnIndex)\n        {\n            _columnNameToIndexDictionary.Remove(this[columnIndex].Name);\n            for (int i = columnIndex + 1; i < Count; i++)\n            {\n                _columnNameToIndexDictionary[this[i].Name]--;\n            }","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs#L95-L131","documentation":"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.","triggerScenarios":"df.Columns[i] = column where column.Name matches another column j != i in the collection.","commonSituations":"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.","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."],"exampleFix":"// before\ndf.Columns[0] = new Int32DataFrameColumn(\"B\", df.Rows.Count); // 'B' is column 1\n// after\ndf.Columns[0] = new Int32DataFrameColumn(\"A_unique\", df.Rows.Count);","handlingStrategy":"validation","validationCode":"var existingIdx = df.Columns.TryGetColumnIndex(newColumn.Name, out int idx);\nif (existingIdx && idx != index)\n    throw new InvalidOperationException($\"Name '{newColumn.Name}' belongs to column {idx}\");\ndf.Columns[index] = newColumn;","typeGuard":"bool CanReplaceAt(DataFrameColumn c, int index, DataFrame df) =>\n    !df.Columns.TryGetColumnIndex(c.Name, out int i) || i == index;","tryCatchPattern":"try\n{\n    df.Columns[index] = column;\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"already exists\") || ex.Message.Contains(\"Duplicate\"))\n{\n    // pick a unique name and retry\n}","preventionTips":["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."],"tags":["dataframe","duplicate-column-name","argument-exception","dotnet"],"backgroundTag":"duplicate-column-name","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}