dotnet/machinelearning · error · System.ArgumentException

Parameter.Count exceeds the number of columns({0}) in the Da

Error message

Parameter.Count exceeds the number of columns({0}) in the DataFrame 

What it means

After enumerating the appended row's values against the DataFrame's columns, Append checks rowMoveNext; if the row still has values left it throws ArgumentException with Strings.ExceedsNumberOfColumns ('Parameter.Count exceeds the number of columns({0}) in the DataFrame'), naming the `row` parameter. The supplied row has more fields than the DataFrame has columns.

Source

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

                            value = null;
                        }
                    }
                    if (value != null)
                    {
                        value = Convert.ChangeType(value, column.DataType, cultureInfo);

                        if (value is null)
                        {
                            throw new ArgumentException(string.Format(Strings.MismatchedValueType, column.DataType), column.Name);
                        }
                    }
                    cachedObjectConversions.Add(value);
                    columnMoveNext = columnEnumerator.MoveNext();
                    rowMoveNext = rowEnumerator.MoveNext();
                }
                if (rowMoveNext)
                {
                    throw new ArgumentException(string.Format(Strings.ExceedsNumberOfColumns, Columns.Count), nameof(row));
                }
                // Reset the enumerators
                columnEnumerator = ret.Columns.GetEnumerator();
                columnMoveNext = columnEnumerator.MoveNext();
                rowEnumerator = row.GetEnumerator();
                rowMoveNext = rowEnumerator.MoveNext();
                int cacheIndex = 0;
                while (columnMoveNext && rowMoveNext)
                {
                    DataFrameColumn column = columnEnumerator.Current;
                    object value = cachedObjectConversions[cacheIndex];
                    ret.ResizeByOneAndAppend(column, value);
                    columnMoveNext = columnEnumerator.MoveNext();
                    rowMoveNext = rowEnumerator.MoveNext();
                    cacheIndex++;
                }
            }
            while (columnMoveNext)

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Ensure each row's keys exactly match df.Columns names — filter or project the row before Append.
  2. Recreate rows from the current DataFrame schema rather than reusing rows from another DataFrame.
  3. Catch ArgumentException and log the row's keys vs df.Columns to spot the extra field.
  4. If extra fields are expected, add the corresponding columns to the DataFrame first (or ignore them explicitly).

Example fix

// before
df.Append(rowWithExtraField);
// after
var trimmed = new DataFrameRow(rowWithExtraField.Where(kv => df.Columns.Contains(kv.Key)));
df.Append(trimmed);
Defensive patterns

Strategy: validation

Validate before calling

var extra = row.Where(kv => !df.Columns.Contains(kv.Key)).ToList();
if (extra.Any())
    throw new InvalidOperationException($"Row has extra fields: {string.Join(',', extra.Select(kv => kv.Key))}");
df.Append(row);

Type guard

bool MatchesSchema(DataFrame df, DataFrameRow row) => row.All(kv => df.Columns.Contains(kv.Key));

Try / catch

try { df.Append(row); }
catch (ArgumentException ex) { logger.LogError(ex, "Row wider than DataFrame ({0} cols)", df.Columns.Count); throw; }

Prevention

When it happens

Trigger: Appending a DataFrameRow created for a wider schema (extra key/value entries) than the target DataFrame; appending after columns were dropped; rows built with both positional and named values.

Common situations: Schema drift between file versions (extra CSV/JSON field); appending rows from an older schema into a trimmed DataFrame; duplicate entries for the same column in the row dictionary.

Related errors


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