dotnet/machinelearning · error · System.ArgumentException

Exception of type 'System.ArgumentException' was thrown.

Error message

Exception of type 'System.ArgumentException' was thrown.

What it means

SaveTo validates that the Table object being populated matches the DataFrame's shape before copying rows: the table must have exactly the same number of columns and each column must have the same DataType as the corresponding DataFrame column. If either check fails, a bare ArgumentException with no message is thrown from SaveTo (invoked via ToTable). It signals the destination table schema does not match the DataFrame schema.

Source

Thrown at src/Microsoft.Data.Analysis/DataFrame.IO.cs:160

        public void SaveTo(DataTable table)
        {
            var columnsCount = Columns.Count;

            if (table.Columns.Count == 0)
            {
                foreach (var column in Columns)
                {
                    table.Columns.Add(column.Name, column.DataType);
                }
            }
            else
            {
                if (table.Columns.Count != columnsCount)
                    throw new ArgumentException();
                for (var c = 0; c < columnsCount; c++)
                {
                    if (table.Columns[c].DataType != Columns[c].DataType)
                        throw new ArgumentException();
                }
            }

            var items = new object[columnsCount];
            foreach (var row in Rows)
            {
                for (var c = 0; c < columnsCount; c++)
                {
                    items[c] = row[c] ?? DBNull.Value;
                }
                table.Rows.Add(items);
            }
        }

        public DataTable ToTable()
        {
            var res = new DataTable();
            SaveTo(res);

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Ensure the target table has exactly as many columns as the DataFrame before calling SaveTo/ToTable
  2. Match each table column's DataType to the corresponding DataFrame column's DataType (cast or recreate columns as needed)
  3. Create a fresh table with the correct schema instead of reusing an existing one
  4. Wrap in try-catch and rebuild the table schema on ArgumentException

Example fix

// before
var table = oldTable; // schema drifted from df
df.ToTable(table);
// after
if (table.Columns.Count != df.Columns.Count ||
    table.Columns.Zip(df.Columns, (t, c) => t.DataType != c.DataType).Any(m => m))
{
    table = new Table(); // rebuild with matching schema
}
df.ToTable(table);
Defensive patterns

Strategy: validation

Validate before calling

bool ok = table.Columns.Count == df.Columns.Count && !table.Columns.Zip(df.Columns, (t, c) => t.DataType != c.DataType).Any(m => m);
if (!ok) throw new InvalidOperationException("Table schema does not match DataFrame");

Try / catch

try { df.ToTable(table); } catch (ArgumentException) { table = RebuildTableFromSchema(df); df.ToTable(table); }

Prevention

When it happens

Trigger: Calling df.ToTable()/SaveTo with a table whose Columns.Count differs from the DataFrame's column count, or where table.Columns[c].DataType != Columns[c].DataType for some column c (e.g. column order changed, a column was added/removed, or a type was inferred differently).

Common situations: Reusing a pre-existing table object across calls after the DataFrame was mutated; mapping a DataFrame onto a table built from an earlier schema version; appending columns to one side but not the other; data-type drift after CSV type inference changes between runs.

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


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