{"record":{"id":"35e4f8ef7e2da56a","repo":"dotnet/machinelearning","slug":"column-lengths-are-mismatched-35e4f8","errorCode":null,"errorMessage":"Column lengths are mismatched","messagePattern":"Column lengths are mismatched","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/PrimitiveDataFrameColumn.cs","lineNumber":957,"sourceCode":"\n        public override PrimitiveDataFrameColumn<bool> ElementwiseIsNotNull()\n        {\n            var ret = new BooleanDataFrameColumn(Name, Length);\n\n            for (long i = 0; i < Length; i++)\n            {\n                ret[i] = IsValid(i);\n            }\n\n            return ret;\n        }\n\n        internal DataFrameColumn HandleOperationImplementation<U>(BinaryOperation operation, PrimitiveDataFrameColumn<U> column, bool inPlace)\n            where U : unmanaged\n        {\n            if (column.Length != Length)\n            {\n                throw new ArgumentException(Strings.MismatchedColumnLengths, nameof(column));\n            }\n            switch (typeof(T))\n            {\n                case Type boolType when boolType == typeof(bool):\n                    if (typeof(U) == typeof(bool))\n                    {\n                        PrimitiveDataFrameColumn<U> primitiveColumn = this as PrimitiveDataFrameColumn<U>;\n                        var newColumn = inPlace ? primitiveColumn : primitiveColumn.Clone();\n                        newColumn._columnContainer.HandleOperation(operation, column._columnContainer);\n                        return newColumn;\n                    }\n                    throw new NotSupportedException();\n                case Type decimalType when decimalType == typeof(decimal):\n                    if (typeof(U) == typeof(bool))\n                    {\n                        throw new NotSupportedException();\n                    }\n                    if (typeof(U) == typeof(T))","sourceCodeStart":939,"sourceCodeEnd":975,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/PrimitiveDataFrameColumn.cs#L939-L975","documentation":"Binary operations between two PrimitiveDataFrameColumn instances require both columns to have the same Length. HandleOperationImplementation<U> validates column.Length == Length before dispatching the typed operation and throws ArgumentException (message 'Column lengths are mismatched') with paramName 'column' when they differ.","triggerScenarios":"Any arithmetic/comparison binary operation (Add, Subtract, Multiply, Divide, comparisons via BinaryOperation) between two columns of different lengths, e.g. col1 + col2 where col1.Length != col2.Length.","commonSituations":"Joining/filtering one column but not the other before a vectorized operation; constructing columns from arrays of different sizes; rows dropped from one column independently.","solutions":["Align the columns: filter/reindex both to the same row set before the operation","Truncate to the shorter length explicitly if that is the intended semantics","Re-create the second column from the same source rows as the first","Check col1.Length == col2.Length before calling the operation and handle mismatches"],"exampleFix":"// before\nvar sum = colA + colB; // colA.Length=100, colB.Length=90 -> ArgumentException\n// after\nif (colA.Length != colB.Length)\n    throw new InvalidOperationException($\"Column length mismatch: {colA.Length} vs {colB.Length}\");\nvar sum = colA + colB;","handlingStrategy":"validation","validationCode":"if (left.Length != right.Length)\n    throw new InvalidOperationException($\"Column lengths mismatched: {left.Length} vs {right.Length}\");","typeGuard":"bool SameLength<T,U>(PrimitiveDataFrameColumn<T> a, PrimitiveDataFrameColumn<U> b) => a.Length == b.Length;","tryCatchPattern":"try\n{\n    var result = left.Add(right);\n}\ncatch (ArgumentException ex) when (ex.Message == Strings.MismatchedColumnLengths)\n{\n    // align/reindex columns and retry\n}","preventionTips":["Derive both columns from the same filtered/aligned DataFrame","Assert column lengths equal before vectorized operations","Avoid mutating one column's length independently of its siblings"],"tags":["dataframe","argument","length-mismatch","dotnet"],"backgroundTag":"schema-validation-failed","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"}