dotnet/machinelearning · error · ArgumentException
MismatchedColumnLengths
Error message
MismatchedColumnLengths
What it means
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.
Source
Thrown at src/Microsoft.Data.Analysis/DataFrame.BinaryOperations.cs:20
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// Generated from DataFrameBinaryOperations.tt. Do not modify directly
using System;
using System.Collections.Generic;
namespace Microsoft.Data.Analysis
{
public partial class DataFrame
{
public DataFrame Add<T>(IReadOnlyList<T> values, bool inPlace = false)
where T : unmanaged
{
if (values.Count != Columns.Count)
{
throw new ArgumentException(Strings.MismatchedColumnLengths, nameof(values));
}
DataFrame retDataFrame = inPlace ? this : new DataFrame();
for (int i = 0; i < Columns.Count; i++)
{
DataFrameColumn baseColumn = _columnCollection[i];
DataFrameColumn newColumn = baseColumn.Add(values[i], inPlace);
if (inPlace)
retDataFrame.Columns[i] = newColumn;
else
retDataFrame.Columns.Insert(i, newColumn);
}
return retDataFrame;
}
/// <summary>
/// Performs an element-wise addition on each column
/// </summary>
public DataFrame Add<T>(T value, bool inPlace = false)View on GitHub (pinned to 7b76e69cf9)
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.
Example fix
// before
df.Add(new int[] { 1, 2, 3 }); // df has 5 columns -> ArgumentException
// after
if (values.Length == df.Columns.Count)
{
df.Add(values);
} Defensive patterns
Strategy: validation
Validate before calling
if (values.Count != df.Columns.Count)
throw new ArgumentException($"Expected {df.Columns.Count} values, got {values.Count}"); Type guard
static bool MatchesColumnCount<T>(DataFrame df, IReadOnlyList<T> values) => values.Count == df.Columns.Count;
Try / catch
try
{
df.Add(values);
}
catch (ArgumentException ex) when (ex.Message.Contains("MismatchedColumnLengths"))
{
// rebuild the values list from df.Columns.Count and retry
} Prevention
- 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
When it happens
Trigger: Calling df.Add(values) where values.Count != df.Columns.Count; e.g. adding a 3-element list to a 5-column DataFrame.
Common situations: 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.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Parameter must not be null, empty, or whitespace
- Value cannot be null. (Parameter 'idColumns')
- Must provide at least 1 ID column
- Must provide at least 1 value column when specifying value c
- Columns cannot exist in both idColumns and valueColumns
AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11).
Data as JSON: /api/errors/58a2f292df508804.
Report an issue: GitHub.