{"record":{"id":"8eb6913e3dd7233e","repo":"dotnet/machinelearning","slug":"string-format-strings-duplicatecolumnname-column","errorCode":null,"errorMessage":"string.Format(Strings.DuplicateColumnName, column.Name)","messagePattern":"string\\.Format\\(Strings\\.DuplicateColumnName, column\\.Name\\)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs","lineNumber":87,"sourceCode":"\n        protected override void InsertItem(int columnIndex, DataFrameColumn column)\n        {\n            column = column ?? throw new ArgumentNullException(nameof(column));\n\n            if (Count == 0)\n            {\n                //change RowCount on inserting first row to dataframe\n                RowCount = column.Length;\n            }\n            else if (column.Length != RowCount)\n            {\n                //check all columns in the dataframe have the same length (amount of rows)\n                throw new ArgumentException(Strings.MismatchedColumnLengths, nameof(column));\n            }\n\n            if (_columnNameToIndexDictionary.ContainsKey(column.Name))\n            {\n                throw new ArgumentException(string.Format(Strings.DuplicateColumnName, column.Name), nameof(column));\n            }\n\n            column.AddOwner(this);\n\n            RowCount = column.Length;\n\n            _columnNameToIndexDictionary[column.Name] = columnIndex;\n            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));","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs#L69-L105","documentation":"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.","triggerScenarios":"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)).","commonSituations":"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.","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)."],"exampleFix":"// before\ndf.Columns.Add(new StringDataFrameColumn(\"Name\", df.Rows.Count)); // 'Name' already exists\n// after\nif (!df.Columns.Contains(\"Name\"))\n    df.Columns.Add(new StringDataFrameColumn(\"Name\", df.Rows.Count));\nelse\n    df.Columns.Add(new StringDataFrameColumn(\"Name_2\", df.Rows.Count));","handlingStrategy":"validation","validationCode":"if (df.Columns.Contains(newColumn.Name))\n    throw new InvalidOperationException($\"Column '{newColumn.Name}' already exists\");\ndf.Columns.Add(newColumn);","typeGuard":"bool CanAdd(DataFrameColumn c, DataFrame df) => c != null && !df.Columns.Contains(c.Name);","tryCatchPattern":"try\n{\n    df.Columns.Add(column);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"already exists\"))\n{\n    // rename or skip\n}","preventionTips":["Always check Contains(name) before Add/Insert.","Rename cloned or joined columns with a suffix before adding.","Deduplicate headers when loading CSV/parquet sources."],"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"}