{"record":{"id":"69078bb3542ecd71","repo":"dotnet/machinelearning","slug":"exception-of-type-system-argumentexception-was-t","errorCode":null,"errorMessage":"Exception of type 'System.ArgumentException' was thrown.","messagePattern":"Exception of type 'System\\.ArgumentException' was thrown\\.","errorType":"exception","errorClass":"System.ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/DataFrame.IO.cs","lineNumber":160,"sourceCode":"        public void SaveTo(DataTable table)\n        {\n            var columnsCount = Columns.Count;\n\n            if (table.Columns.Count == 0)\n            {\n                foreach (var column in Columns)\n                {\n                    table.Columns.Add(column.Name, column.DataType);\n                }\n            }\n            else\n            {\n                if (table.Columns.Count != columnsCount)\n                    throw new ArgumentException();\n                for (var c = 0; c < columnsCount; c++)\n                {\n                    if (table.Columns[c].DataType != Columns[c].DataType)\n                        throw new ArgumentException();\n                }\n            }\n\n            var items = new object[columnsCount];\n            foreach (var row in Rows)\n            {\n                for (var c = 0; c < columnsCount; c++)\n                {\n                    items[c] = row[c] ?? DBNull.Value;\n                }\n                table.Rows.Add(items);\n            }\n        }\n\n        public DataTable ToTable()\n        {\n            var res = new DataTable();\n            SaveTo(res);","sourceCodeStart":142,"sourceCodeEnd":178,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/DataFrame.IO.cs#L142-L178","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Ensure the target table has exactly as many columns as the DataFrame before calling SaveTo/ToTable","Match each table column's DataType to the corresponding DataFrame column's DataType (cast or recreate columns as needed)","Create a fresh table with the correct schema instead of reusing an existing one","Wrap in try-catch and rebuild the table schema on ArgumentException"],"exampleFix":"// before\nvar table = oldTable; // schema drifted from df\ndf.ToTable(table);\n// after\nif (table.Columns.Count != df.Columns.Count ||\n    table.Columns.Zip(df.Columns, (t, c) => t.DataType != c.DataType).Any(m => m))\n{\n    table = new Table(); // rebuild with matching schema\n}\ndf.ToTable(table);","handlingStrategy":"validation","validationCode":"bool ok = table.Columns.Count == df.Columns.Count && !table.Columns.Zip(df.Columns, (t, c) => t.DataType != c.DataType).Any(m => m);\nif (!ok) throw new InvalidOperationException(\"Table schema does not match DataFrame\");","typeGuard":null,"tryCatchPattern":"try { df.ToTable(table); } catch (ArgumentException) { table = RebuildTableFromSchema(df); df.ToTable(table); }","preventionTips":["Derive the table schema from the DataFrame every time instead of caching tables","Assert column count and DataTypes before SaveTo","Recreate the destination when the DataFrame schema changes"],"tags":["argument","schema","dataframe"],"backgroundTag":"schema-validation-failed","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}