{"record":{"id":"08147843c23aa8f7","repo":"dotnet/machinelearning","slug":"strings-mismatchedcolumnlengths","errorCode":null,"errorMessage":"Strings.MismatchedColumnLengths","messagePattern":"Strings\\.MismatchedColumnLengths","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs","lineNumber":82,"sourceCode":"            where T : unmanaged\n        {\n            DataFrameColumn newColumn = new PrimitiveDataFrameColumn<T>(columnName, column);\n            Insert(columnIndex, newColumn); // calls InsertItem internally\n        }\n\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();","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs#L64-L100","documentation":"When inserting a column into a non-empty DataFrameColumnCollection, the column's Length must equal the collection's RowCount, since all columns of a DataFrame must have the same number of rows. A length mismatch throws ArgumentException(Strings.MismatchedColumnLengths, nameof(column)) from InsertItem.","triggerScenarios":"df.Columns.Insert(i, column) or InsertColumn with a column whose Length differs from the existing RowCount — e.g. adding a 3-row column to a DataFrame whose columns have 100 rows, or inserting into an empty-name-collision-free but differently sized collection.","commonSituations":"Merging columns from DataFrames loaded from different files; building a DataFrame column-by-column where one source array is shorter; off-by-one or filtered series added to an existing table.","solutions":["Resize the new column to match df.Columns.RowCount before inserting (recreate PrimitiveDataFrameColumn with the right length or pad/truncate data).","Check column.Length == df.Columns.RowCount before inserting and fail fast with a clear message.","If rows are genuinely missing, pad with nulls/default values or fix the upstream data load.","Create all columns from same-length sources when assembling a DataFrame."],"exampleFix":"// before\ndf.Columns.Insert(0, new PrimitiveDataFrameColumn<int>(\"Flag\", shortData)); // length mismatch\n// after\nif (shortData.Length != df.Columns.RowCount)\n    throw new InvalidOperationException($\"Column has {shortData.Length} rows, expected {df.Columns.RowCount}\");\ndf.Columns.Insert(0, new PrimitiveDataFrameColumn<int>(\"Flag\", df.Columns.RowCount));","handlingStrategy":"validation","validationCode":"if (column.Length != df.Columns.RowCount)\n    throw new InvalidOperationException($\"Column '{column.Name}' has {column.Length} rows; DataFrame has {df.Columns.RowCount}.\");","typeGuard":null,"tryCatchPattern":"try { df.Columns.Insert(idx, column); }\ncatch (ArgumentException ex) when (ex.ParamName == \"column\")\n{\n    // pad/truncate the column to RowCount and retry, or surface a data-loading error\n}","preventionTips":["Check column.Length against RowCount before every insert","Build all columns from equal-length source collections","Validate row counts right after loading data from external files","Decide an explicit padding/truncation policy for mismatched series"],"tags":["dotnet","dataframe","length-mismatch","argument-validation"],"backgroundTag":"shape-mismatch","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"}