dotnet/machinelearning · error · ArgumentException
Strings.BadColumnCast (formatted with column.DataType, typeo
Error message
Strings.BadColumnCast (formatted with column.DataType, typeof(T))
What it means
GetPrimitiveColumn<T> fetches the column by name and throws ArgumentException(Strings.BadColumnCast, column.DataType, typeof(T)) if it is not a PrimitiveDataFrameColumn<T>. This guards against invalid type-based column casts.
Source
Thrown at src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs:223
}
}
/// <summary>
/// Gets the <see cref="PrimitiveDataFrameColumn{T}"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="PrimitiveDataFrameColumn{T}"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public PrimitiveDataFrameColumn<T> GetPrimitiveColumn<T>(string name)
where T : unmanaged
{
DataFrameColumn column = this[name];
if (column is PrimitiveDataFrameColumn<T> ret)
{
return ret;
}
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(T)), nameof(T));
}
/// <summary>
/// Gets the <see cref="DateTimeDataFrameColumn"/> with the specified <paramref name="name"/>.
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns><see cref="DateTimeDataFrameColumn"/>.</returns>
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
public DateTimeDataFrameColumn GetDateTimeColumn(string name)
{
DataFrameColumn column = this[name];
if (column is DateTimeDataFrameColumn ret)
{
return ret;
}
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(DateTime)));
}View on GitHub (pinned to 7b76e69cf9)
Solutions
- Check column.DataType first and request the matching T (e.g. use GetPrimitiveColumn<long> for Int64).
- Inspect df.Columns[name].DataType (or df.Schema) and branch on the actual type.
- Convert the column (Clone/Cast) to the desired primitive type before retrieval.
Example fix
// before
var col = df.Columns.GetPrimitiveColumn<int>(\"Age\"); // Age is Int64
// after
if (df.Columns["Age"].DataType == typeof(long))
var col = df.Columns.GetPrimitiveColumn<long>("Age"); Defensive patterns
Strategy: type-guard
Validate before calling
var type = df.Columns["Age"].DataType;
var col = type == typeof(int)
? df.Columns.GetPrimitiveColumn<int>("Age")
: throw new InvalidOperationException($"'Age' is {type}, expected int"); Type guard
bool IsPrimitive<T>(DataFrameColumn c) => c is PrimitiveDataFrameColumn<T>;
Try / catch
try
{
var col = df.Columns.GetPrimitiveColumn<int>(name);
}
catch (ArgumentException ex) when (ex.ParamName == "T")
{
// inspect column.DataType and retry with matching T
} Prevention
- Inspect column.DataType (or df.Schema) before typed getters.
- Account for loader type inference (int columns often arrive as long/double).
- Prefer pattern matching (is PrimitiveDataFrameColumn<T>) over blind casts.
When it happens
Trigger: df.Columns.GetPrimitiveColumn<int>("name") where the stored column is not PrimitiveDataFrameColumn<int> (wrong primitive type, or a string/datetime/arrow column).
Common situations: CSV inference giving float where int was expected, schema drift across files, assuming int columns that contain nulls are Int32 instead of Nullable Int64 defaults.
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
- Strings.BadColumnCast (formatted with column.DataType, typeo
- Strings.BadColumnCast (formatted with column.DataType, typeo
- Strings.BadColumnCast (formatted with column.DataType, typeo
- 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/6c01dcae0fe135a3.
Report an issue: GitHub.