dotnet/machinelearning · error · ArgumentException

MismatchedColumnLengths

Error message

MismatchedColumnLengths

What it means

StringDataFrameColumn.Add (and its arithmetic family) requires both columns to have the same Length; element i is combined with element i. When Length != column.Length it throws ArgumentException with the message Strings.MismatchedColumnLengths ('MismatchedColumnLengths'). The library has no broadcasting or alignment for binary column operations.

Solutions

  1. Ensure both columns have the same Length before adding: filter/pad/truncate so counts match
  2. Verify the intended operand — if you meant to append a constant, use the string overload (Add(string value)) instead of a column overload
  3. Re-derive both columns from the same filtered DataFrame so row counts stay in sync
  4. Check for null/missing rows removed on one side only; rebuild with an index-based join

Example fix

// before
var filtered = df["A"].ElementwiseLessThan(10); // shorter after use elsewhere
var result = strColumn.Add(otherStrColumn); // Lengths 100 vs 90 -> ArgumentException
// after
if (strColumn.Length != otherStrColumn.Length)
    throw new InvalidOperationException("Align columns before Add");
var result = strColumn.Add(otherStrColumn);
Defensive patterns

Strategy: validation

Validate before calling

if (left.Length != right.Length)
    throw new InvalidOperationException($"Cannot Add: lengths differ ({left.Length} vs {right.Length})");

Type guard

bool SameLength(DataFrameColumn a, DataFrameColumn b) => a.Length == b.Length;

Try / catch

try { var result = strColumn.Add(otherColumn); }
catch (ArgumentException ex) when (ex.Message.Contains("MismatchedColumnLengths")) {
    // align lengths: filter both columns from the same source or pad/truncate
}

Prevention

When it happens

Trigger: Calling stringColumn.Add(otherColumn) — including the + operator and Add(column, inPlace:true) — where the two columns have different row counts, e.g. Length=100 vs Length=90 after a filter/join on only one side.

Common situations: Filtering one column independently before concatenating; misaligned results from a left join; appending rows to one column only; mixing columns from DataFrames of different sizes.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/36e70145fe9f257b. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.Data.Analysis/StringDataFrameColumn.BinaryOperations.cs:20

// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace Microsoft.Data.Analysis
{
    public partial class StringDataFrameColumn : DataFrameColumn
    {
        /// <inheritdoc/>
        public override DataFrameColumn Add(DataFrameColumn column, bool inPlace = false)
        {
            if (Length != column.Length)
            {
                throw new ArgumentException(Strings.MismatchedColumnLengths, nameof(column));
            }
            StringDataFrameColumn ret = inPlace ? this : Clone();
            for (long i = 0; i < Length; i++)
            {
                ret[i] += column[i].ToString();
            }
            return ret;
        }

        public static StringDataFrameColumn operator +(StringDataFrameColumn column, string value)
        {
            return column.Add(value);
        }

        public static StringDataFrameColumn operator +(string value, StringDataFrameColumn column)
        {
            return Add(value, column);
        }

View on GitHub (pinned to 7b76e69cf9)