dotnet/machinelearning · error · System.ArgumentException

{0} {1}

Error message

{0} {1}

What it means

SetTableRowCount validates that every column's Length equals the requested rowCount; if any column disagrees it throws ArgumentException formatted as "{MismatchedRowCount} {columnName}", i.e. '{0} {1}'. This is an internal consistency check run when DataFrame state changes (Count, First, Head, Tail, Max, Min call into it). It indicates columns of unequal length were added to the DataFrame.

Source

Thrown at src/Microsoft.Data.Analysis/DataFrame.cs:403

            GroupBy<TKey> group = GroupBy(columnName) as GroupBy<TKey>;

            if (group == null)
            {
                DataFrameColumn column = this[columnName];
                throw new InvalidCastException(String.Format(Strings.BadColumnCastDuringGrouping, columnName, column.DataType, typeof(TKey)));
            }

            return group;
        }

        // In GroupBy and ReadCsv calls, columns get resized. We need to set the RowCount to reflect the true Length of the DataFrame. This does internal validation
        internal void SetTableRowCount(long rowCount)
        {
            // Even if current RowCount == rowCount, do the validation
            for (int i = 0; i < Columns.Count; i++)
            {
                if (Columns[i].Length != rowCount)
                    throw new ArgumentException(String.Format("{0} {1}", Strings.MismatchedRowCount, Columns[i].Name));
            }
            _columnCollection.RowCount = rowCount;
        }

        /// <summary>
        /// Returns a DataFrame with no missing values
        /// </summary>
        /// <param name="options"></param>
        public DataFrame DropNulls(DropNullOptions options = DropNullOptions.Any)
        {
            var filter = new BooleanDataFrameColumn("Filter");

            if (options == DropNullOptions.Any)
            {
                filter.AppendMany(true, Rows.Count);
                var buffers = filter.ColumnContainer.Buffers;

                foreach (var column in Columns)

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Make all columns the same length before forming the DataFrame — pad shorter sources or truncate longer ones.
  2. Check each column's Length against the first column's Length before adding it to the DataFrame.
  3. Catch ArgumentException and inspect the column named in the message; fix that column's length.
  4. Ensure column mutations (Resize/Append on columns) are applied to every column equally.

Example fix

// before
df.Columns.Add(stringColumnWithDifferentLength);
// after
if (newCol.Length != df.Rows.Count)
    newCol.Resize(df.Rows.Count); // or pad/truncate to match
df.Columns.Add(newCol);
Defensive patterns

Strategy: validation

Validate before calling

long expected = df.Columns[0].Length;
for (int i = 1; i < df.Columns.Count; i++)
    if (df.Columns[i].Length != expected)
        throw new InvalidOperationException($"Column '{df.Columns[i].Name}' length {df.Columns[i].Length} != {expected}");

Type guard

bool IsRectangular(DataFrame df) => df.Columns.All(c => c.Length == df.Columns[0].Length);

Try / catch

try { df.SetTableRowCount(n); }
catch (ArgumentException ex) { logger.LogError(ex, "Column lengths inconsistent: {0}", ex.Message); throw; }

Prevention

When it happens

Trigger: Adding columns of different lengths to a DataFrame (e.g. via column construction or Append paths) and then triggering operations that read/set the row count; mutating a column's Length independently of the others.

Common situations: Building a DataFrame column-by-column from sources of different sizes (shorter CSV column, filtered array); a bug in user code that resized one column after assembly; deserializing columns from mismatched arrays.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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