dotnet/machinelearning · error · ArgumentException
Column lengths are mismatched
Error message
Column lengths are mismatched
What it means
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.
Source
Thrown at src/Microsoft.Data.Analysis/PrimitiveDataFrameColumn.cs:957
public override PrimitiveDataFrameColumn<bool> ElementwiseIsNotNull()
{
var ret = new BooleanDataFrameColumn(Name, Length);
for (long i = 0; i < Length; i++)
{
ret[i] = IsValid(i);
}
return ret;
}
internal DataFrameColumn HandleOperationImplementation<U>(BinaryOperation operation, PrimitiveDataFrameColumn<U> column, bool inPlace)
where U : unmanaged
{
if (column.Length != Length)
{
throw new ArgumentException(Strings.MismatchedColumnLengths, nameof(column));
}
switch (typeof(T))
{
case Type boolType when boolType == typeof(bool):
if (typeof(U) == typeof(bool))
{
PrimitiveDataFrameColumn<U> primitiveColumn = this as PrimitiveDataFrameColumn<U>;
var newColumn = inPlace ? primitiveColumn : primitiveColumn.Clone();
newColumn._columnContainer.HandleOperation(operation, column._columnContainer);
return newColumn;
}
throw new NotSupportedException();
case Type decimalType when decimalType == typeof(decimal):
if (typeof(U) == typeof(bool))
{
throw new NotSupportedException();
}
if (typeof(U) == typeof(T))View on GitHub (pinned to 7b76e69cf9)
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
Example fix
// before
var sum = colA + colB; // colA.Length=100, colB.Length=90 -> ArgumentException
// after
if (colA.Length != colB.Length)
throw new InvalidOperationException($"Column length mismatch: {colA.Length} vs {colB.Length}");
var sum = colA + colB; Defensive patterns
Strategy: validation
Validate before calling
if (left.Length != right.Length)
throw new InvalidOperationException($"Column lengths mismatched: {left.Length} vs {right.Length}"); Type guard
bool SameLength<T,U>(PrimitiveDataFrameColumn<T> a, PrimitiveDataFrameColumn<U> b) => a.Length == b.Length;
Try / catch
try
{
var result = left.Add(right);
}
catch (ArgumentException ex) when (ex.Message == Strings.MismatchedColumnLengths)
{
// align/reindex columns and retry
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
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
- Strings.MismatchedColumnLengths
- Expected column to hold values of type {0}
- MismatchedColumnLengths
- MismatchedColumnLengths
- Exception of type 'System.ArgumentException' was thrown.
AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11).
Data as JSON: /api/errors/35e4f8ef7e2da56a.
Report an issue: GitHub.