{"record":{"id":"58a2f292df508804","repo":"dotnet/machinelearning","slug":"mismatchedcolumnlengths","errorCode":null,"errorMessage":"MismatchedColumnLengths","messagePattern":"MismatchedColumnLengths","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/DataFrame.BinaryOperations.cs","lineNumber":20,"sourceCode":"// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT license.\n// See the LICENSE file in the project root for more information.\n\n// Generated from DataFrameBinaryOperations.tt. Do not modify directly\n\nusing System;\nusing System.Collections.Generic;\n\nnamespace Microsoft.Data.Analysis\n{\n    public partial class DataFrame\n    {\n        public DataFrame Add<T>(IReadOnlyList<T> values, bool inPlace = false)\n            where T : unmanaged\n        {\n            if (values.Count != Columns.Count)\n            {\n                throw new ArgumentException(Strings.MismatchedColumnLengths, nameof(values));\n            }\n            DataFrame retDataFrame = inPlace ? this : new DataFrame();\n\n            for (int i = 0; i < Columns.Count; i++)\n            {\n                DataFrameColumn baseColumn = _columnCollection[i];\n                DataFrameColumn newColumn = baseColumn.Add(values[i], inPlace);\n                if (inPlace)\n                    retDataFrame.Columns[i] = newColumn;\n                else\n                    retDataFrame.Columns.Insert(i, newColumn);\n            }\n            return retDataFrame;\n        }\n        /// <summary>\n        /// Performs an element-wise addition on each column\n        /// </summary>\n        public DataFrame Add<T>(T value, bool inPlace = false)","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/DataFrame.BinaryOperations.cs#L2-L38","documentation":"DataFrame.Add<T>(IReadOnlyList<T> values, bool inPlace) throws ArgumentException(Strings.MismatchedColumnLengths) when the values list length differs from the DataFrame's column count. The list must supply one operand per column for the element-wise addition.","triggerScenarios":"Calling df.Add(values) where values.Count != df.Columns.Count; e.g. adding a 3-element list to a 5-column DataFrame.","commonSituations":"Building the values list from a separate schema/row of data; columns added or dropped after the list was created; hardcoded arrays not matching the DataFrame schema.","solutions":["Check values.Count equals df.Columns.Count before calling Add and trim or extend the list to match.","Build the values list dynamically from df.Columns.Count instead of a hardcoded literal.","If column count legitimately differs, construct a new DataFrame with the intended columns and add row-wise/element-wise explicitly."],"exampleFix":"// before\ndf.Add(new int[] { 1, 2, 3 }); // df has 5 columns -> ArgumentException\n// after\nif (values.Length == df.Columns.Count)\n{\n    df.Add(values);\n}","handlingStrategy":"validation","validationCode":"if (values.Count != df.Columns.Count)\n    throw new ArgumentException($\"Expected {df.Columns.Count} values, got {values.Count}\");","typeGuard":"static bool MatchesColumnCount<T>(DataFrame df, IReadOnlyList<T> values) => values.Count == df.Columns.Count;","tryCatchPattern":"try\n{\n    df.Add(values);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"MismatchedColumnLengths\"))\n{\n    // rebuild the values list from df.Columns.Count and retry\n}","preventionTips":["Always build the values list from df.Columns.Count, never a hardcoded literal","Recompute lists after any Add/Remove column operation","Add a debug assertion comparing counts before arithmetic calls"],"tags":["dotnet","dataframe","argument-length"],"backgroundTag":"invalid-argument-value","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"}