{"record":{"id":"60d186ed80c769a3","repo":"dotnet/machinelearning","slug":"expected-value-to-be-of-type-0","errorCode":null,"errorMessage":"Expected value to be of type {0}","messagePattern":"Expected value to be of type (.+?)","errorType":"validation","errorClass":"System.ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/DataFrame.cs","lineNumber":566,"sourceCode":"                    // StringDataFrameColumn can accept empty strings. The other columns interpret empty values as nulls\n                    if (value is string stringValue)\n                    {\n                        if (stringValue.Length == 0 && column.DataType != typeof(string))\n                        {\n                            value = null;\n                        }\n                        else if (stringValue.Equals(\"null\", StringComparison.OrdinalIgnoreCase))\n                        {\n                            value = null;\n                        }\n                    }\n                    if (value != null)\n                    {\n                        value = Convert.ChangeType(value, column.DataType, cultureInfo);\n\n                        if (value is null)\n                        {\n                            throw new ArgumentException(string.Format(Strings.MismatchedValueType, column.DataType), column.Name);\n                        }\n                    }\n                    cachedObjectConversions.Add(value);\n                    columnMoveNext = columnEnumerator.MoveNext();\n                    rowMoveNext = rowEnumerator.MoveNext();\n                }\n                if (rowMoveNext)\n                {\n                    throw new ArgumentException(string.Format(Strings.ExceedsNumberOfColumns, Columns.Count), nameof(row));\n                }\n                // Reset the enumerators\n                columnEnumerator = ret.Columns.GetEnumerator();\n                columnMoveNext = columnEnumerator.MoveNext();\n                rowEnumerator = row.GetEnumerator();\n                rowMoveNext = rowEnumerator.MoveNext();\n                int cacheIndex = 0;\n                while (columnMoveNext && rowMoveNext)\n                {","sourceCodeStart":548,"sourceCodeEnd":584,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/DataFrame.cs#L548-L584","documentation":"During Append, each row value is converted with Convert.ChangeType to the target column's DataType; if the conversion returns null (value not convertible to the column type), the method throws ArgumentException with Strings.MismatchedValueType ('Expected value to be of type {0}'), using the column's name as the param name. It signals a row value whose runtime type is incompatible with the column it lands in.","triggerScenarios":"Appending a DataFrameRow whose value for a numeric column is a non-convertible string (e.g. \"abc\" into an int column), or a value that Convert.ChangeType cannot handle (e.g. Guid into double), possibly after culture-sensitive parsing issues.","commonSituations":"User-supplied CSV/JSON rows with unparseable values; nullable/boxed types landing in primitive columns; locale-formatted numbers ('1,5') under a different CultureInfo.","solutions":["Pre-validate and convert each value to the column's DataType before calling Append (TryParse/Convert.ChangeType yourself with explicit culture).","Map bad values to null or a sentinel so Convert.ChangeType is skipped for genuinely missing data.","Catch ArgumentException and read the param name to identify the offending column, then clean that field's data.","Ensure consistent CultureInfo when constructing values (pass cultureInfo explicitly to conversions)."],"exampleFix":"// before\nrow[\"Age\"] = rawAge; // rawAge is \"N/A\"\n// after\nrow[\"Age\"] = int.TryParse(rawAge, NumberStyles.Integer, CultureInfo.InvariantCulture, out var age)\n    ? (object)age : null;","handlingStrategy":"validation","validationCode":"object CastFor(object value, Type colType, CultureInfo ci) =>\n    value == null ? null : Convert.ChangeType(value, colType, ci) ?? throw new FormatException($\"Cannot convert {value} to {colType}\");","typeGuard":"bool Fits<T>(object v) where T : IConvertible => v == null || v is T;","tryCatchPattern":"try { df.Append(row); }\ncatch (ArgumentException ex) { logger.LogError(ex, \"Bad value for column '{0}', expected {1}\", ex.ParamName, ex.Message); throw; }","preventionTips":["TryParse/convert values yourself with explicit CultureInfo before Append","Treat sentinel strings like 'N/A' as null, not as raw values","Validate one sample row against column types before bulk appending"],"tags":["type-mismatch","append","conversion","column-type"],"backgroundTag":"type-mismatch","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}