dotnet/machinelearning · error · System.ArgumentException
Expected value to be of type {0}
Error message
Expected value to be of type {0} What it means
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.
Source
Thrown at src/Microsoft.Data.Analysis/DataFrame.cs:566
// StringDataFrameColumn can accept empty strings. The other columns interpret empty values as nulls
if (value is string stringValue)
{
if (stringValue.Length == 0 && column.DataType != typeof(string))
{
value = null;
}
else if (stringValue.Equals("null", StringComparison.OrdinalIgnoreCase))
{
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)
{View on GitHub (pinned to 7b76e69cf9)
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).
Example fix
// before
row["Age"] = rawAge; // rawAge is "N/A"
// after
row["Age"] = int.TryParse(rawAge, NumberStyles.Integer, CultureInfo.InvariantCulture, out var age)
? (object)age : null; Defensive patterns
Strategy: validation
Validate before calling
object CastFor(object value, Type colType, CultureInfo ci) =>
value == null ? null : Convert.ChangeType(value, colType, ci) ?? throw new FormatException($"Cannot convert {value} to {colType}"); Type guard
bool Fits<T>(object v) where T : IConvertible => v == null || v is T;
Try / catch
try { df.Append(row); }
catch (ArgumentException ex) { logger.LogError(ex, "Bad value for column '{0}', expected {1}", ex.ParamName, ex.Message); throw; } Prevention
- 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
When it happens
Trigger: 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.
Common situations: User-supplied CSV/JSON rows with unparseable values; nullable/boxed types landing in primitive columns; locale-formatted numbers ('1,5') under a different CultureInfo.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Cannot cast elements of column '{0}' type of {1} to type {2}
- Parameter.Count exceeds the number of columns({0}) in the Da
- Value cannot be null. (Parameter 'row')
- String.Format(Strings.MismatchedColumnValueType, this.DataTy
- Strings.BadColumnCast (formatted with column.DataType, typeo
AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11).
Data as JSON: /api/errors/60d186ed80c769a3.
Report an issue: GitHub.