dotnet/machinelearning · error · ArgumentException
Strings.MismatchedColumnLengths
Error message
Strings.MismatchedColumnLengths
What it means
When inserting a column into a non-empty DataFrameColumnCollection, the column's Length must equal the collection's RowCount, since all columns of a DataFrame must have the same number of rows. A length mismatch throws ArgumentException(Strings.MismatchedColumnLengths, nameof(column)) from InsertItem.
Source
Thrown at src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs:82
where T : unmanaged
{
DataFrameColumn newColumn = new PrimitiveDataFrameColumn<T>(columnName, column);
Insert(columnIndex, newColumn); // calls InsertItem internally
}
protected override void InsertItem(int columnIndex, DataFrameColumn column)
{
column = column ?? throw new ArgumentNullException(nameof(column));
if (Count == 0)
{
//change RowCount on inserting first row to dataframe
RowCount = column.Length;
}
else if (column.Length != RowCount)
{
//check all columns in the dataframe have the same length (amount of rows)
throw new ArgumentException(Strings.MismatchedColumnLengths, nameof(column));
}
if (_columnNameToIndexDictionary.ContainsKey(column.Name))
{
throw new ArgumentException(string.Format(Strings.DuplicateColumnName, column.Name), nameof(column));
}
column.AddOwner(this);
RowCount = column.Length;
_columnNameToIndexDictionary[column.Name] = columnIndex;
for (int i = columnIndex; i < Count; i++)
{
_columnNameToIndexDictionary[this[i].Name]++;
}
base.InsertItem(columnIndex, column);
ColumnsChanged?.Invoke();View on GitHub (pinned to 7b76e69cf9)
Solutions
- Resize the new column to match df.Columns.RowCount before inserting (recreate PrimitiveDataFrameColumn with the right length or pad/truncate data).
- Check column.Length == df.Columns.RowCount before inserting and fail fast with a clear message.
- If rows are genuinely missing, pad with nulls/default values or fix the upstream data load.
- Create all columns from same-length sources when assembling a DataFrame.
Example fix
// before
df.Columns.Insert(0, new PrimitiveDataFrameColumn<int>("Flag", shortData)); // length mismatch
// after
if (shortData.Length != df.Columns.RowCount)
throw new InvalidOperationException($"Column has {shortData.Length} rows, expected {df.Columns.RowCount}");
df.Columns.Insert(0, new PrimitiveDataFrameColumn<int>("Flag", df.Columns.RowCount)); Defensive patterns
Strategy: validation
Validate before calling
if (column.Length != df.Columns.RowCount)
throw new InvalidOperationException($"Column '{column.Name}' has {column.Length} rows; DataFrame has {df.Columns.RowCount}."); Try / catch
try { df.Columns.Insert(idx, column); }
catch (ArgumentException ex) when (ex.ParamName == "column")
{
// pad/truncate the column to RowCount and retry, or surface a data-loading error
} Prevention
- Check column.Length against RowCount before every insert
- Build all columns from equal-length source collections
- Validate row counts right after loading data from external files
- Decide an explicit padding/truncation policy for mismatched series
When it happens
Trigger: df.Columns.Insert(i, column) or InsertColumn with a column whose Length differs from the existing RowCount — e.g. adding a 3-row column to a DataFrame whose columns have 100 rows, or inserting into an empty-name-collision-free but differently sized collection.
Common situations: Merging columns from DataFrames loaded from different files; building a DataFrame column-by-column where one source array is shorter; off-by-one or filtered series added to an existing table.
Related errors
- Parameter must not be null, empty, or whitespace
- String.Format(Strings.MismatchedColumnValueType, this.DataTy
- ArgumentNullException(nameof(column))
- offsetBuffer
- Strings.MultipleMismatchedValueType (formatted with typeof(l
AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11).
Data as JSON: /api/errors/08147843c23aa8f7.
Report an issue: GitHub.